Skip to contentSkip to conversation

New DRM accel driver for Intel VPU

PATCH Superseded Jacek Lawrynowicz 4 participants 28 messages Jan 9, 2023 – Jan 19, 2023

Subsystems (4)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover New DRM accel 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.

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 was open sourced in December 2022 and it is available at:
https://github.com/intel/linux-vpu-driver

The firmware for the VPU will be distributed as a closed source binary.

There are no major chcnages in this version of the patchset. Detailed changes are listed below.

Regards,
Jacek

Changelog

v5:
- Rename ivpu_drm.h to ivpu_accel.h
- Cleanup ivpu_mmu_config_check()
- Optimize locking in ivpu_mmu_cd_add()
- Invalidate user context if it has MMU faults
- Move ivpu_ipc_match_consumer() outside ivpu_ipc_dispatch()

v4: https://lore.kernel.org/all/[email protected]/
- Switch to the accel framework (DRIVER_COMPUTE_ACCEL)
- Move the driver from drivers/gpu/drm to drivers/accel
- Rename kconfig DRM_IVPU option to DRM_ACCEL_IVPU and update dependencies
- Create context on open() instead of lazy allocating it
- Remove status_offset from submit ioctl, as status is now reported in bo_wait ioctl
- Use managed resources in ivpu_drv.c
- Optimize locking in ivpu_ipc.c - add new rx_msg_lock for consumer msg list
- Refactor vpu_hw_mtl_reg.h to use BIT_MASK() and GENMASK() macros
- Use module_pci_driver() for mudule init
- Remove mutex from "struct ivpu_pm_info"
- Add explicit "vdev" arg to ivpu_dbg()
- Use drm_WARN_ON() instead of WARN_ON() where possible
- Add comments for boot related functions
- Update firmware API headers

v3: https://lore.kernel.org/all/[email protected]/
- Fixed alignment warning in ivpu_ipc.c when building with W=1

v2: https://lore.kernel.org/all/[email protected]/
- Rename the driver from "drm/vpu" to "drm/ivpu"
- Add a TODO file
- Add support for WC buffers

v1: https://lore.kernel.org/all/[email protected]/

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

 MAINTAINERS                           |    9 +
 drivers/Makefile                      |    1 +
 drivers/accel/Kconfig                 |    2 +
 drivers/accel/Makefile                |    3 +
 drivers/accel/ivpu/Kconfig            |   15 +
 drivers/accel/ivpu/Makefile           |   16 +
 drivers/accel/ivpu/TODO               |    9 +
 drivers/accel/ivpu/ivpu_drv.c         |  661 +++++++++++++++
 drivers/accel/ivpu/ivpu_drv.h         |  190 +++++
 drivers/accel/ivpu/ivpu_fw.c          |  423 ++++++++++
 drivers/accel/ivpu/ivpu_fw.h          |   38 +
 drivers/accel/ivpu/ivpu_gem.c         |  846 +++++++++++++++++++
 drivers/accel/ivpu/ivpu_gem.h         |  129 +++
 drivers/accel/ivpu/ivpu_hw.h          |  170 ++++
 drivers/accel/ivpu/ivpu_hw_mtl.c      | 1084 +++++++++++++++++++++++++
 drivers/accel/ivpu/ivpu_hw_mtl_reg.h  |  280 +++++++
 drivers/accel/ivpu/ivpu_hw_reg_io.h   |  115 +++
 drivers/accel/ivpu/ivpu_ipc.c         |  508 ++++++++++++
 drivers/accel/ivpu/ivpu_ipc.h         |   93 +++
 drivers/accel/ivpu/ivpu_job.c         |  614 ++++++++++++++
 drivers/accel/ivpu/ivpu_job.h         |   67 ++
 drivers/accel/ivpu/ivpu_jsm_msg.c     |  170 ++++
 drivers/accel/ivpu/ivpu_jsm_msg.h     |   23 +
 drivers/accel/ivpu/ivpu_mmu.c         |  883 ++++++++++++++++++++
 drivers/accel/ivpu/ivpu_mmu.h         |   50 ++
 drivers/accel/ivpu/ivpu_mmu_context.c |  398 +++++++++
 drivers/accel/ivpu/ivpu_mmu_context.h |   50 ++
 drivers/accel/ivpu/ivpu_pm.c          |  329 ++++++++
 drivers/accel/ivpu/ivpu_pm.h          |   38 +
 drivers/accel/ivpu/vpu_boot_api.h     |  349 ++++++++
 drivers/accel/ivpu/vpu_jsm_api.h      |  999 +++++++++++++++++++++++
 include/uapi/drm/ivpu_accel.h         |  339 ++++++++
 32 files changed, 8901 insertions(+)
 create mode 100644 drivers/accel/Makefile
 create mode 100644 drivers/accel/ivpu/Kconfig
 create mode 100644 drivers/accel/ivpu/Makefile
 create mode 100644 drivers/accel/ivpu/TODO
 create mode 100644 drivers/accel/ivpu/ivpu_drv.c
 create mode 100644 drivers/accel/ivpu/ivpu_drv.h
 create mode 100644 drivers/accel/ivpu/ivpu_fw.c
 create mode 100644 drivers/accel/ivpu/ivpu_fw.h
 create mode 100644 drivers/accel/ivpu/ivpu_gem.c
 create mode 100644 drivers/accel/ivpu/ivpu_gem.h
 create mode 100644 drivers/accel/ivpu/ivpu_hw.h
 create mode 100644 drivers/accel/ivpu/ivpu_hw_mtl.c
 create mode 100644 drivers/accel/ivpu/ivpu_hw_mtl_reg.h
 create mode 100644 drivers/accel/ivpu/ivpu_hw_reg_io.h
 create mode 100644 drivers/accel/ivpu/ivpu_ipc.c
 create mode 100644 drivers/accel/ivpu/ivpu_ipc.h
 create mode 100644 drivers/accel/ivpu/ivpu_job.c
 create mode 100644 drivers/accel/ivpu/ivpu_job.h
 create mode 100644 drivers/accel/ivpu/ivpu_jsm_msg.c
 create mode 100644 drivers/accel/ivpu/ivpu_jsm_msg.h
 create mode 100644 drivers/accel/ivpu/ivpu_mmu.c
 create mode 100644 drivers/accel/ivpu/ivpu_mmu.h
 create mode 100644 drivers/accel/ivpu/ivpu_mmu_context.c
 create mode 100644 drivers/accel/ivpu/ivpu_mmu_context.h
 create mode 100644 drivers/accel/ivpu/ivpu_pm.c
 create mode 100644 drivers/accel/ivpu/ivpu_pm.h
 create mode 100644 drivers/accel/ivpu/vpu_boot_api.h
 create mode 100644 drivers/accel/ivpu/vpu_jsm_api.h
 create mode 100644 include/uapi/drm/ivpu_accel.h

base-commit: 03a0a1040895711e12c15ab28d4d1812928e171d
prerequisite-patch-id: 1283b537f4791511769ad3446d0eafd98193e86f --
2.34.1

#1 lore raw

PATCH v5 1/7 accel/ivpu: 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 components:
- 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/accel/ivpu 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                          |    9 +
 drivers/Makefile                     |    1 +
 drivers/accel/Kconfig                |    2 +
 drivers/accel/Makefile               |    3 +
 drivers/accel/ivpu/Kconfig           |   15 +
 drivers/accel/ivpu/Makefile          |    8 +
 drivers/accel/ivpu/TODO              |    9 +
 drivers/accel/ivpu/ivpu_drv.c        |  359 +++++++++
 drivers/accel/ivpu/ivpu_drv.h        |  162 ++++
 drivers/accel/ivpu/ivpu_hw.h         |  170 +++++
 drivers/accel/ivpu/ivpu_hw_mtl.c     | 1048 ++++++++++++++++++++++++++
 drivers/accel/ivpu/ivpu_hw_mtl_reg.h |  280 +++++++
 drivers/accel/ivpu/ivpu_hw_reg_io.h  |  115 +++
 include/uapi/drm/ivpu_accel.h        |   95 +++
 14 files changed, 2276 insertions(+)
 create mode 100644 drivers/accel/Makefile
 create mode 100644 drivers/accel/ivpu/Kconfig
 create mode 100644 drivers/accel/ivpu/Makefile
 create mode 100644 drivers/accel/ivpu/TODO
 create mode 100644 drivers/accel/ivpu/ivpu_drv.c
 create mode 100644 drivers/accel/ivpu/ivpu_drv.h
 create mode 100644 drivers/accel/ivpu/ivpu_hw.h
 create mode 100644 drivers/accel/ivpu/ivpu_hw_mtl.c
 create mode 100644 drivers/accel/ivpu/ivpu_hw_mtl_reg.h
 create mode 100644 drivers/accel/ivpu/ivpu_hw_reg_io.h
 create mode 100644 include/uapi/drm/ivpu_accel.h
MAINTAINERS +9 -0
index a47d963af3b8..a994c39e1118 100644
@@ -6915,6 +6915,15 @@ T:	git https://git.kernel.org/pub/scm/linux/kernel/git/ogabbay/accel.git
 F:	Documentation/accel/
 F:	drivers/accel/
 
+DRM ACCEL DRIVERS FOR INTEL VPU
+M:	Jacek Lawrynowicz <[email protected]>
+M:	Stanislaw Gruszka <[email protected]>
+L:	[email protected]
+S:	Supported
+T:	git git://anongit.freedesktop.org/drm/drm-misc
+F:	drivers/accel/ivpu/
+F:	include/uapi/drm/ivpu_drm.h
+
 DRM DRIVERS FOR ALLWINNER A10
 M:	Maxime Ripard <[email protected]>
 M:	Chen-Yu Tsai <[email protected]>
drivers/Makefile +1 -0
index bdf1c66141c9..f0972e2226c9 100644
@@ -189,3 +189,4 @@ obj-$(CONFIG_COUNTER)		+= counter/
 obj-$(CONFIG_MOST)		+= most/
 obj-$(CONFIG_PECI)		+= peci/
 obj-$(CONFIG_HTE)		+= hte/
+obj-$(CONFIG_DRM_ACCEL)	+= accel/
\ No newline at end of file
drivers/accel/Kconfig +2 -0
index c9ce849b2984..4989376e5938 100644
@@ -22,3 +22,5 @@ menuconfig DRM_ACCEL
 	  major number than GPUs, and will be exposed to user-space using
 	  different device files, called accel/accel* (in /dev, sysfs
 	  and debugfs).
+
+source "drivers/accel/ivpu/Kconfig"
drivers/accel/Makefile new file +3 -0
new file mode 100644
index 000000000000..b1036dbc0ba4
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+obj-y	+= ivpu/
drivers/accel/ivpu/Kconfig new file +15 -0
new file mode 100644
index 000000000000..110a6b921135
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+config DRM_ACCEL_IVPU
+	tristate "Intel VPU for Meteor Lake and newer"
+	depends on DRM_ACCEL
+	depends on X86_64
+	depends on PCI && PCI_MSI
+	select FW_LOADER
+	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/accel/ivpu/Makefile new file +8 -0
new file mode 100644
index 000000000000..2cc8394101cd
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-only
+# Copyright (C) 2023 Intel Corporation
+
+intel_vpu-y := \
+	ivpu_drv.o \
+	ivpu_hw_mtl.o
+
+obj-$(CONFIG_DRM_ACCEL_IVPU) += intel_vpu.o
\ No newline at end of file
drivers/accel/ivpu/TODO new file +9 -0
new file mode 100644
index 000000000000..be3fec660df9
@@ -0,0 +1,9 @@
+- Implement support for BLOB IDs
+- Add debugfs support to improve debugging and testing
+- Add tracing events for performance debugging
+- Implement HW based scheduling support
+- Use syncobjs for submit/sync
+- Refactor IPC protocol to improve message latency
+- Implement BO cache and MADVISE IOCTL
+- Replace userptr with prime import and dma-buf heaps
+- Refactor struct ivpu_bo to use struct drm_gem_shmem_object
drivers/accel/ivpu/ivpu_drv.c new file +359 -0

359 lines not on this page · raw message

drivers/accel/ivpu/ivpu_drv.h new file +162 -0

162 lines not on this page · raw message

drivers/accel/ivpu/ivpu_hw.h new file +170 -0

170 lines not on this page · raw message

drivers/accel/ivpu/ivpu_hw_mtl.c new file +1048 -0

1,048 lines not on this page · raw message

drivers/accel/ivpu/ivpu_hw_mtl_reg.h new file +280 -0

280 lines not on this page · raw message

drivers/accel/ivpu/ivpu_hw_reg_io.h new file +115 -0

115 lines not on this page · raw message

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

95 lines not on this page · raw message

Signature · 1 line
2.34.1
Reviewed-by on v5 1/7
#2 lore raw

Re: [PATCH v5 1/7] accel/ivpu: Introduce a new DRM driver for Intel VPU

Quoted 27 lines · Jacek Lawrynowicz

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 components:
- 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/accel/ivpu 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

Quoted 3 lines
Reviewed-by Jeffrey Hugo <quic_jhugo2quicinc.com>

drivers/accel/ivpu/ivpu_drv.c line 171

Quoted 11 lines
drivers/accel/ivpu/ivpu_drv.c new file +11 -0
+static const struct file_operations ivpu_fops = {
+	.owner		= THIS_MODULE,
+	.open		= accel_open,
+	.release	= drm_release,
+	.unlocked_ioctl	= drm_ioctl,
+	.compat_ioctl	= drm_compat_ioctl,
+	.poll		= drm_poll,
+	.read		= drm_read,
+	.llseek		= noop_llseek,
+	.mmap           = drm_gem_mmap
+};

Hmm DEFINE_DRM_ACCEL_FOPS is not usable here because it doesn't define .mmap Feels like we should fix that and then simplify this. Seems like a good todo item.

#3 lore raw

Re: [PATCH v5 1/7] accel/ivpu: Introduce a new DRM driver for Intel VPU

Hi,

Quoted 34 lines · Jeffrey Hugo
Quoted 27 lines · Jacek Lawrynowicz

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 components:
   - 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/accel/ivpu 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

Quoted 3 lines
Reviewed-by Jeffrey Hugo <quic_jhugo2quicinc.com>
Quoted 14 lines

drivers/accel/ivpu/ivpu_drv.c line 171

Quoted 11 lines
drivers/accel/ivpu/ivpu_drv.c new file +11 -0
+static const struct file_operations ivpu_fops = {
+    .owner        = THIS_MODULE,
+    .open        = accel_open,
+    .release    = drm_release,
+    .unlocked_ioctl    = drm_ioctl,
+    .compat_ioctl    = drm_compat_ioctl,
+    .poll        = drm_poll,
+    .read        = drm_read,
+    .llseek        = noop_llseek,
+    .mmap           = drm_gem_mmap
+};

Hmm DEFINE_DRM_ACCEL_FOPS is not usable here because it doesn't define .mmap Feels like we should fix that and then simplify this.  Seems like a good todo item.

I think this should rather be on accel/drm TODO and not ivpu TODO list. For the moment I can simplify the code to this:

static const struct file_operations ivpu_fops = {
	.owner		= THIS_MODULE,
	.mmap           = drm_gem_mmap,
	DRM_ACCEL_FOPS,
};

Still not perfect but nicer.

Regards,
Jacek

#4 lore raw

Re: [PATCH v5 1/7] accel/ivpu: Introduce a new DRM driver for Intel VPU

Quoted 60 lines · Jacek Lawrynowicz

Hi,

Quoted 34 lines · Jeffrey Hugo
Quoted 27 lines · Jacek Lawrynowicz

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 components:
   - 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/accel/ivpu 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

Quoted 3 lines
Reviewed-by Jeffrey Hugo <quic_jhugo2quicinc.com>
Quoted 14 lines

drivers/accel/ivpu/ivpu_drv.c line 171

Quoted 11 lines
drivers/accel/ivpu/ivpu_drv.c new file +11 -0
+static const struct file_operations ivpu_fops = {
+    .owner        = THIS_MODULE,
+    .open        = accel_open,
+    .release    = drm_release,
+    .unlocked_ioctl    = drm_ioctl,
+    .compat_ioctl    = drm_compat_ioctl,
+    .poll        = drm_poll,
+    .read        = drm_read,
+    .llseek        = noop_llseek,
+    .mmap           = drm_gem_mmap
+};

Hmm DEFINE_DRM_ACCEL_FOPS is not usable here because it doesn't define .mmap Feels like we should fix that and then simplify this.  Seems like a good todo item.

I think this should rather be on accel/drm TODO and not ivpu TODO list. For the moment I can simplify the code to this:

static const struct file_operations ivpu_fops = {
	.owner		= THIS_MODULE,
	.mmap           = drm_gem_mmap,
	DRM_ACCEL_FOPS,
};
Quoted 5 lines

Still not perfect but nicer.

Regards,
Jacek

Yep, it looked like you could do that simplification but it didn't seem worth it to spin a v6 for this. However since you seem to be planning a v6 anyways, rolling in this simplification would be nice.

Also, I'm sorry, I didn't mean to imply that the DEFINE_DRM_ACCEL_FOPS in on the ivpu todo. I was thinking more in general terms.

Actually, I'll take that up and post a patch next week. We'll see what the reaction is. I don't intend for it to be a dependency for you. Feels like a quick thing to get done so it is not something the next person trips over (probably me) and we can link up all the pieces when both FOPS and ivpu are available.

-Jeff

#5 lore raw

PATCH v5 2/7 accel/ivpu: Add Intel VPU MMU support

In reply to Jacek Lawrynowicz

VPU Memory Management Unit is based on ARM MMU-600.
It allows the creation of 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 ivpu_mmu_context, it has an ID, drm_mm allocator for VPU addresses and struct ivpu_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 two main files:
- ivpu_mmu_context.c handles MMU page tables and memory mapping
- ivpu_mmu.c implements a driver that programs the MMU device

---
 drivers/accel/ivpu/Makefile           |   4 +-
 drivers/accel/ivpu/ivpu_drv.c         |  83 ++-
 drivers/accel/ivpu/ivpu_drv.h         |   7 +
 drivers/accel/ivpu/ivpu_hw_mtl.c      |  10 +
 drivers/accel/ivpu/ivpu_mmu.c         | 876 ++++++++++++++++++++++++++
 drivers/accel/ivpu/ivpu_mmu.h         |  50 ++
 drivers/accel/ivpu/ivpu_mmu_context.c | 398 ++++++++++++
 drivers/accel/ivpu/ivpu_mmu_context.h |  50 ++
 include/uapi/drm/ivpu_accel.h         |   4 +
 9 files changed, 1479 insertions(+), 3 deletions(-)
 create mode 100644 drivers/accel/ivpu/ivpu_mmu.c
 create mode 100644 drivers/accel/ivpu/ivpu_mmu.h
 create mode 100644 drivers/accel/ivpu/ivpu_mmu_context.c
 create mode 100644 drivers/accel/ivpu/ivpu_mmu_context.h
drivers/accel/ivpu/Makefile +3 -1
index 2cc8394101cd..59cd7843b218 100644
@@ -3,6 +3,8 @@
 
 intel_vpu-y := \
 	ivpu_drv.o \
-	ivpu_hw_mtl.o
+	ivpu_hw_mtl.o \
+	ivpu_mmu.o \
+	ivpu_mmu_context.o
 
 obj-$(CONFIG_DRM_ACCEL_IVPU) += intel_vpu.o
\ No newline at end of file
drivers/accel/ivpu/ivpu_drv.c +81 -2

164 lines not on this page · raw message

drivers/accel/ivpu/ivpu_drv.h +7 -0
index 6c5cc8c1da01..a749a0b97703 100644
@@ -15,6 +15,8 @@
 #include <linux/xarray.h>
 #include <uapi/drm/ivpu_accel.h>
 
+#include "ivpu_mmu_context.h"
+
 #define DRIVER_NAME "intel_vpu"
 #define DRIVER_DESC "Driver for Intel Versatile Processing Unit (VPU)"
 #define DRIVER_DATE "20230109"
@@ -71,6 +73,7 @@ struct ivpu_wa_table {
 };
 
 struct ivpu_hw_info;
+struct ivpu_mmu_info;
 
 struct ivpu_device {
 	struct drm_device drm;
@@ -81,7 +84,9 @@ struct ivpu_device {
 
 	struct ivpu_wa_table wa;
 	struct ivpu_hw_info *hw;
+	struct ivpu_mmu_info *mmu;
 
+	struct ivpu_mmu_context gctx;
 	struct xarray context_xa;
 	struct xa_limit context_xa_limit;
 
@@ -100,7 +105,9 @@ struct ivpu_device {
 struct ivpu_file_priv {
 	struct kref ref;
 	struct ivpu_device *vdev;
+	struct ivpu_mmu_context ctx;
 	u32 priority;
+	bool has_mmu_faults;
 };
 
 extern int ivpu_dbg_mask;
drivers/accel/ivpu/ivpu_hw_mtl.c +10 -0
index 8a1b1c03f77b..eaba2b8248b8 100644
@@ -7,6 +7,7 @@
 #include "ivpu_hw_mtl_reg.h"
 #include "ivpu_hw_reg_io.h"
 #include "ivpu_hw.h"
+#include "ivpu_mmu.h"
 
 #define TILE_FUSE_ENABLE_BOTH	     0x0
 #define TILE_FUSE_ENABLE_UPPER	     0x1
@@ -930,6 +931,15 @@ static u32 ivpu_hw_mtl_irqv_handler(struct ivpu_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))
+		ivpu_mmu_irq_evtq_handler(vdev);
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_1_INT, status))
+		ivpu_dbg(vdev, IRQ, "MMU sync complete\n");
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_2_INT, status))
+		ivpu_mmu_irq_gerr_handler(vdev);
+
 	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, CPU_INT_REDIRECT_0_INT, status))
 		ivpu_hw_mtl_irq_wdt_mss_handler(vdev);
 
drivers/accel/ivpu/ivpu_mmu.c new file +876 -0

876 lines not on this page · raw message

drivers/accel/ivpu/ivpu_mmu.h new file +50 -0

50 lines not on this page · raw message

drivers/accel/ivpu/ivpu_mmu_context.c new file +398 -0

398 lines not on this page · raw message

drivers/accel/ivpu/ivpu_mmu_context.h new file +50 -0

50 lines not on this page · raw message

include/uapi/drm/ivpu_accel.h +4 -0
index c6a98977eb8e..543347df51a1 100644
@@ -38,6 +38,7 @@ extern "C" {
 #define DRM_IVPU_PARAM_NUM_CONTEXTS	    4
 #define DRM_IVPU_PARAM_CONTEXT_BASE_ADDRESS 5
 #define DRM_IVPU_PARAM_CONTEXT_PRIORITY	    6
+#define DRM_IVPU_PARAM_CONTEXT_ID	    7
 
 #define DRM_IVPU_PLATFORM_TYPE_SILICON	    0
 
@@ -78,6 +79,9 @@ struct drm_ivpu_param {
 	 * Value of current context scheduling priority (read-write).
 	 * See DRM_IVPU_CONTEXT_PRIORITY_* for possible values.
 	 *
+	 * %DRM_IVPU_PARAM_CONTEXT_ID:
+	 * Current context ID, always greater than 0 (read-only)
+	 *
 	 */
 	__u32 param;
 
Signature · 1 line
2.34.1
Reviewed-by on v5 2/7
#6 lore raw

Re: [PATCH v5 2/7] accel/ivpu: Add Intel VPU MMU support

Quoted 21 lines · Jacek Lawrynowicz

VPU Memory Management Unit is based on ARM MMU-600.
It allows the creation of 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 ivpu_mmu_context, it has an ID, drm_mm allocator for VPU addresses and struct ivpu_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 two main files:
- ivpu_mmu_context.c handles MMU page tables and memory mapping
- ivpu_mmu.c implements a driver that programs the MMU device

Quoted 5 lines
#7 lore raw

PATCH v5 3/7 accel/ivpu: 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 ivpu_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 released when the buffer is destroyed.
Eviction mechism is planned for future versions.

Add three new IOCTLs: BO_CREATE, BO_INFO, BO_USERPTR

---
 drivers/accel/ivpu/Makefile   |   1 +
 drivers/accel/ivpu/ivpu_drv.c |  31 +-
 drivers/accel/ivpu/ivpu_drv.h |   1 +
 drivers/accel/ivpu/ivpu_gem.c | 820 ++++++++++++++++++++++++++++++++++
 drivers/accel/ivpu/ivpu_gem.h | 128 ++++++
 include/uapi/drm/ivpu_accel.h | 127 ++++++
 6 files changed, 1106 insertions(+), 2 deletions(-)
 create mode 100644 drivers/accel/ivpu/ivpu_gem.c
 create mode 100644 drivers/accel/ivpu/ivpu_gem.h
drivers/accel/ivpu/Makefile +1 -0
index 59cd7843b218..5d7c5862399c 100644
@@ -3,6 +3,7 @@
 
 intel_vpu-y := \
 	ivpu_drv.o \
+	ivpu_gem.o \
 	ivpu_hw_mtl.o \
 	ivpu_mmu.o \
 	ivpu_mmu_context.o
drivers/accel/ivpu/ivpu_drv.c +29 -2
index d7982f451781..0b9034499c4c 100644
@@ -12,8 +12,10 @@
 #include <drm/drm_file.h>
 #include <drm/drm_gem.h>
 #include <drm/drm_ioctl.h>
+#include <drm/drm_prime.h>
 
 #include "ivpu_drv.h"
+#include "ivpu_gem.h"
 #include "ivpu_hw.h"
 #include "ivpu_mmu.h"
 #include "ivpu_mmu_context.h"
@@ -49,6 +51,24 @@ struct ivpu_file_priv *ivpu_file_priv_get(struct ivpu_file_priv *file_priv)
 	return file_priv;
 }
 
+struct ivpu_file_priv *ivpu_file_priv_get_by_ctx_id(struct ivpu_device *vdev, unsigned long id)
+{
+	struct ivpu_file_priv *file_priv;
+
+	xa_lock_irq(&vdev->context_xa);
+	file_priv = xa_load(&vdev->context_xa, id);
+	/* file_priv may still be in context_xa during file_priv_release() */
+	if (file_priv && !kref_get_unless_zero(&file_priv->ref))
+		file_priv = NULL;
+	xa_unlock_irq(&vdev->context_xa);
+
+	if (file_priv)
+		ivpu_dbg(vdev, KREF, "file_priv get by id: ctx %u refcount %u\n",
+			 file_priv->ctx.id, kref_read(&file_priv->ref));
+
+	return file_priv;
+}
+
 static void file_priv_release(struct kref *ref)
 {
 	struct ivpu_file_priv *file_priv = container_of(ref, struct ivpu_file_priv, ref);
@@ -57,7 +77,7 @@ static void file_priv_release(struct kref *ref)
 	ivpu_dbg(vdev, FILE, "file_priv release: ctx %u\n", file_priv->ctx.id);
 
 	ivpu_mmu_user_context_fini(vdev, &file_priv->ctx);
-	WARN_ON(xa_erase_irq(&vdev->context_xa, file_priv->ctx.id) != file_priv);
+	drm_WARN_ON(&vdev->drm, xa_erase_irq(&vdev->context_xa, file_priv->ctx.id) != file_priv);
 	kfree(file_priv);
 }
 
@@ -66,7 +86,7 @@ void ivpu_file_priv_put(struct ivpu_file_priv **link)
 	struct ivpu_file_priv *file_priv = *link;
 	struct ivpu_device *vdev = file_priv->vdev;
 
-	WARN_ON(!file_priv);
+	drm_WARN_ON(&vdev->drm, !file_priv);
 
 	ivpu_dbg(vdev, KREF, "file_priv put: ctx %u refcount %u\n",
 		 file_priv->ctx.id, kref_read(&file_priv->ref));
@@ -200,6 +220,9 @@ static void ivpu_postclose(struct drm_device *dev, struct drm_file *file)
 static const struct drm_ioctl_desc ivpu_drm_ioctls[] = {
 	DRM_IOCTL_DEF_DRV(IVPU_GET_PARAM, ivpu_get_param_ioctl, 0),
 	DRM_IOCTL_DEF_DRV(IVPU_SET_PARAM, ivpu_set_param_ioctl, 0),
+	DRM_IOCTL_DEF_DRV(IVPU_BO_CREATE, ivpu_bo_create_ioctl, 0),
+	DRM_IOCTL_DEF_DRV(IVPU_BO_INFO, ivpu_bo_info_ioctl, 0),
+	DRM_IOCTL_DEF_DRV(IVPU_BO_USERPTR, ivpu_bo_userptr_ioctl, 0),
 };
 
 int ivpu_shutdown(struct ivpu_device *vdev)
@@ -233,6 +256,10 @@ static const struct drm_driver driver = {
 
 	.open = ivpu_open,
 	.postclose = ivpu_postclose,
+	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
+	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
+	.gem_prime_import = ivpu_gem_prime_import,
+	.gem_prime_mmap = drm_gem_prime_mmap,
 
 	.ioctls = ivpu_drm_ioctls,
 	.num_ioctls = ARRAY_SIZE(ivpu_drm_ioctls),
drivers/accel/ivpu/ivpu_drv.h +1 -0
index a749a0b97703..e8a43dbe5a3a 100644
@@ -115,6 +115,7 @@ extern u8 ivpu_pll_min_ratio;
 extern u8 ivpu_pll_max_ratio;
 
 struct ivpu_file_priv *ivpu_file_priv_get(struct ivpu_file_priv *file_priv);
+struct ivpu_file_priv *ivpu_file_priv_get_by_ctx_id(struct ivpu_device *vdev, unsigned long id);
 void ivpu_file_priv_put(struct ivpu_file_priv **link);
 int ivpu_shutdown(struct ivpu_device *vdev);
 
drivers/accel/ivpu/ivpu_gem.c new file +820 -0

820 lines not on this page · raw message

drivers/accel/ivpu/ivpu_gem.h new file +128 -0

128 lines not on this page · raw message

include/uapi/drm/ivpu_accel.h +127 -0

145 lines not on this page · raw message

Signature · 1 line
2.34.1
#8 lore raw

Re: [PATCH v5 3/7] accel/ivpu: Add GEM buffer object management

Quoted 289 lines · Jacek Lawrynowicz

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

All types are implemented as struct ivpu_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 released when the buffer is destroyed.
Eviction mechism is planned for future versions.

Add three new IOCTLs: BO_CREATE, BO_INFO, BO_USERPTR

---
 drivers/accel/ivpu/Makefile   |   1 +
 drivers/accel/ivpu/ivpu_drv.c |  31 +-
 drivers/accel/ivpu/ivpu_drv.h |   1 +
 drivers/accel/ivpu/ivpu_gem.c | 820 ++++++++++++++++++++++++++++++++++
 drivers/accel/ivpu/ivpu_gem.h | 128 ++++++
 include/uapi/drm/ivpu_accel.h | 127 ++++++
 6 files changed, 1106 insertions(+), 2 deletions(-)
 create mode 100644 drivers/accel/ivpu/ivpu_gem.c
 create mode 100644 drivers/accel/ivpu/ivpu_gem.h
drivers/accel/ivpu/Makefile +1 -0
index 59cd7843b218..5d7c5862399c 100644
@@ -3,6 +3,7 @@
 
 intel_vpu-y := \
        ivpu_drv.o \
+       ivpu_gem.o \
        ivpu_hw_mtl.o \
        ivpu_mmu.o \
        ivpu_mmu_context.o
drivers/accel/ivpu/ivpu_drv.c +29 -2
index d7982f451781..0b9034499c4c 100644
@@ -12,8 +12,10 @@
 #include <drm/drm_file.h>
 #include <drm/drm_gem.h>
 #include <drm/drm_ioctl.h>
+#include <drm/drm_prime.h>
 
 #include "ivpu_drv.h"
+#include "ivpu_gem.h"
 #include "ivpu_hw.h"
 #include "ivpu_mmu.h"
 #include "ivpu_mmu_context.h"
@@ -49,6 +51,24 @@ struct ivpu_file_priv *ivpu_file_priv_get(struct ivpu_file_priv *file_priv)
        return file_priv;
 }
 
+struct ivpu_file_priv *ivpu_file_priv_get_by_ctx_id(struct ivpu_device *vdev, unsigned long id)
+{
+       struct ivpu_file_priv *file_priv;
+
+       xa_lock_irq(&vdev->context_xa);
+       file_priv = xa_load(&vdev->context_xa, id);
+       /* file_priv may still be in context_xa during file_priv_release() */
+       if (file_priv && !kref_get_unless_zero(&file_priv->ref))
+               file_priv = NULL;
+       xa_unlock_irq(&vdev->context_xa);
+
+       if (file_priv)
+               ivpu_dbg(vdev, KREF, "file_priv get by id: ctx %u refcount %u\n",
+                        file_priv->ctx.id, kref_read(&file_priv->ref));
+
+       return file_priv;
+}
+
 static void file_priv_release(struct kref *ref)
 {
        struct ivpu_file_priv *file_priv = container_of(ref, struct ivpu_file_priv, ref);
@@ -57,7 +77,7 @@ static void file_priv_release(struct kref *ref)
        ivpu_dbg(vdev, FILE, "file_priv release: ctx %u\n", file_priv->ctx.id);
 
        ivpu_mmu_user_context_fini(vdev, &file_priv->ctx);
-       WARN_ON(xa_erase_irq(&vdev->context_xa, file_priv->ctx.id) != file_priv);
+       drm_WARN_ON(&vdev->drm, xa_erase_irq(&vdev->context_xa, file_priv->ctx.id) != file_priv);
        kfree(file_priv);
 }
 
@@ -66,7 +86,7 @@ void ivpu_file_priv_put(struct ivpu_file_priv **link)
        struct ivpu_file_priv *file_priv = *link;
        struct ivpu_device *vdev = file_priv->vdev;
 
-       WARN_ON(!file_priv);
+       drm_WARN_ON(&vdev->drm, !file_priv);
 
        ivpu_dbg(vdev, KREF, "file_priv put: ctx %u refcount %u\n",
                 file_priv->ctx.id, kref_read(&file_priv->ref));
@@ -200,6 +220,9 @@ static void ivpu_postclose(struct drm_device *dev, struct drm_file *file)
 static const struct drm_ioctl_desc ivpu_drm_ioctls[] = {
        DRM_IOCTL_DEF_DRV(IVPU_GET_PARAM, ivpu_get_param_ioctl, 0),
        DRM_IOCTL_DEF_DRV(IVPU_SET_PARAM, ivpu_set_param_ioctl, 0),
+       DRM_IOCTL_DEF_DRV(IVPU_BO_CREATE, ivpu_bo_create_ioctl, 0),
+       DRM_IOCTL_DEF_DRV(IVPU_BO_INFO, ivpu_bo_info_ioctl, 0),
+       DRM_IOCTL_DEF_DRV(IVPU_BO_USERPTR, ivpu_bo_userptr_ioctl, 0),
 };
 
 int ivpu_shutdown(struct ivpu_device *vdev)
@@ -233,6 +256,10 @@ static const struct drm_driver driver = {
 
        .open = ivpu_open,
        .postclose = ivpu_postclose,
+       .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
+       .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
+       .gem_prime_import = ivpu_gem_prime_import,
+       .gem_prime_mmap = drm_gem_prime_mmap,
 
        .ioctls = ivpu_drm_ioctls,
        .num_ioctls = ARRAY_SIZE(ivpu_drm_ioctls),
drivers/accel/ivpu/ivpu_drv.h +1 -0
index a749a0b97703..e8a43dbe5a3a 100644
@@ -115,6 +115,7 @@ extern u8 ivpu_pll_min_ratio;
 extern u8 ivpu_pll_max_ratio;
 
 struct ivpu_file_priv *ivpu_file_priv_get(struct ivpu_file_priv *file_priv);
+struct ivpu_file_priv *ivpu_file_priv_get_by_ctx_id(struct ivpu_device *vdev, unsigned long id);
 void ivpu_file_priv_put(struct ivpu_file_priv **link);
 int ivpu_shutdown(struct ivpu_device *vdev);
 
drivers/accel/ivpu/ivpu_gem.c new file +149 -0
new file mode 100644
index 000000000000..f6d1937c798f
@@ -0,0 +1,820 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2020-2023 Intel Corporation
+ */
+
+#include <linux/dma-buf.h>
+#include <linux/highmem.h>
+#include <linux/module.h>
+#include <linux/set_memory.h>
+#include <linux/xarray.h>
+
+#include <drm/drm_cache.h>
+#include <drm/drm_debugfs.h>
+#include <drm/drm_file.h>
+#include <drm/drm_utils.h>
+
+#include "ivpu_drv.h"
+#include "ivpu_gem.h"
+#include "ivpu_hw.h"
+#include "ivpu_mmu.h"
+#include "ivpu_mmu_context.h"
+
+MODULE_IMPORT_NS(DMA_BUF);
+
+static const struct drm_gem_object_funcs ivpu_gem_funcs;
+
+static struct lock_class_key prime_bo_lock_class_key;
+static struct lock_class_key userptr_bo_lock_class_key;
+
+static int __must_check prime_alloc_pages_locked(struct ivpu_bo *bo)
+{
+       /* Pages are managed by the underlying dma-buf */
+       return 0;
+}
+
+static void prime_free_pages_locked(struct ivpu_bo *bo)
+{
+       /* Pages are managed by the underlying dma-buf */
+}
+
+static int prime_map_pages_locked(struct ivpu_bo *bo)
+{
+       struct ivpu_device *vdev = ivpu_bo_to_vdev(bo);
+       struct sg_table *sgt;
+
+       WARN_ON(!bo->base.import_attach);
+
+       sgt = dma_buf_map_attachment(bo->base.import_attach, DMA_BIDIRECTIONAL);
+       if (IS_ERR(sgt)) {
+               ivpu_err(vdev, "Failed to map attachment: %ld\n", PTR_ERR(sgt));
+               return PTR_ERR(sgt);
+       }
+
+       bo->sgt = sgt;
+       return 0;
+}
+
+static void prime_unmap_pages_locked(struct ivpu_bo *bo)
+{
+       WARN_ON(!bo->base.import_attach);
+
+       dma_buf_unmap_attachment(bo->base.import_attach, bo->sgt, DMA_BIDIRECTIONAL);
+       bo->sgt = NULL;
+}
+
+static const struct ivpu_bo_ops prime_ops = {
+       .type = IVPU_BO_TYPE_PRIME,
+       .name = "prime",
+       .alloc_pages = prime_alloc_pages_locked,
+       .free_pages = prime_free_pages_locked,
+       .map_pages = prime_map_pages_locked,
+       .unmap_pages = prime_unmap_pages_locked,
+};
+
+static int __must_check shmem_alloc_pages_locked(struct ivpu_bo *bo)
+{
+       int npages = bo->base.size >> PAGE_SHIFT;
+       struct page **pages;
+
+       pages = drm_gem_get_pages(&bo->base);
+       if (IS_ERR(pages))
+               return PTR_ERR(pages);
+
+       if (bo->flags & DRM_IVPU_BO_WC)
+               set_pages_array_wc(pages, npages);
+       else if (bo->flags & DRM_IVPU_BO_UNCACHED)
+               set_pages_array_uc(pages, npages);
+
+       bo->pages = pages;
+       return 0;
+}
+
+static void shmem_free_pages_locked(struct ivpu_bo *bo)
+{
+       if (ivpu_bo_cache_mode(bo) != DRM_IVPU_BO_CACHED)
+               set_pages_array_wb(bo->pages, bo->base.size >> PAGE_SHIFT);
+
+       drm_gem_put_pages(&bo->base, bo->pages, true, false);
+       bo->pages = NULL;
+}
+
+static int ivpu_bo_map_pages_locked(struct ivpu_bo *bo)
+{
+       int npages = bo->base.size >> PAGE_SHIFT;
+       struct ivpu_device *vdev = ivpu_bo_to_vdev(bo);
+       struct sg_table *sgt;
+       int ret;
+
+       sgt = drm_prime_pages_to_sg(&vdev->drm, bo->pages, npages);
+       if (IS_ERR(sgt)) {
+               ivpu_err(vdev, "Failed to allocate sgtable\n");
+               return PTR_ERR(sgt);
+       }
+
+       ret = dma_map_sgtable(vdev->drm.dev, sgt, DMA_BIDIRECTIONAL, 0);
+       if (ret) {
+               ivpu_err(vdev, "Failed to map BO in IOMMU: %d\n", ret);
+               goto err_free_sgt;
+       }
+
+       bo->sgt = sgt;
+       return 0;
+
+err_free_sgt:
+       kfree(sgt);
+       return ret;
+}
+
+static void ivpu_bo_unmap_pages_locked(struct ivpu_bo *bo)
+{
+       struct ivpu_device *vdev = ivpu_bo_to_vdev(bo);
+
+       dma_unmap_sgtable(vdev->drm.dev, bo->sgt, DMA_BIDIRECTIONAL, 0);
+       sg_free_table(bo->sgt);
+       kfree(bo->sgt);
+       bo->sgt = NULL;
+}
+
+static const struct ivpu_bo_ops shmem_ops = {
+       .type = IVPU_BO_TYPE_SHMEM,
+       .name = "shmem",
+       .alloc_pages = shmem_alloc_pages_locked,
+       .free_pages = shmem_free_pages_locked,
+       .map_pages = ivpu_bo_map_pages_locked,
+       .unmap_pages = ivpu_bo_unmap_pages_locked,
+};
+
+static int __must_check userptr_alloc_pages_locked(struct ivpu_bo *bo)
+{

drivers/accel/ivpu/ivpu_gem.c line 159

Quoted 10 lines
drivers/accel/ivpu/ivpu_gem.c new file +10 -0
+       unsigned int npages = bo->base.size >> PAGE_SHIFT;
+       struct page **pages;
+       int ret;
+
+       pages = kvmalloc_array(npages, sizeof(*bo->pages), GFP_KERNEL);
+       if (!pages)
+               return -ENOMEM;
+
+       ret = pin_user_pages_fast(bo->user_ptr & PAGE_MASK, npages,
+                                 FOLL_FORCE | FOLL_WRITE | FOLL_LONGTERM, pages);

You didn't fix this patch according to my comments in v4. Please remove FOLL_FORCE and
use unpin_user_pages_dirty_lock (with true flag) Thanks,
Oded

Quoted 950 lines
drivers/accel/ivpu/ivpu_gem.c new file +661 -0
+       if (ret != npages) {
+               if (ret > 0)
+                       goto err_unpin_pages;
+               goto err_free_pages;
+       }
+
+       bo->pages = pages;
+       return 0;
+
+err_unpin_pages:
+       unpin_user_pages(pages, ret);
+err_free_pages:
+       kvfree(pages);
+       return ret;
+}
+
+static void userptr_free_pages_locked(struct ivpu_bo *bo)
+{
+       unpin_user_pages(bo->pages, bo->base.size >> PAGE_SHIFT);
+       kvfree(bo->pages);
+       bo->pages = NULL;
+}
+
+static const struct ivpu_bo_ops userptr_ops = {
+       .type = IVPU_BO_TYPE_USERPTR,
+       .name = "userptr",
+       .alloc_pages = userptr_alloc_pages_locked,
+       .free_pages = userptr_free_pages_locked,
+       .map_pages = ivpu_bo_map_pages_locked,
+       .unmap_pages = ivpu_bo_unmap_pages_locked,
+};
+
+static int __must_check internal_alloc_pages_locked(struct ivpu_bo *bo)
+{
+       unsigned int i, npages = bo->base.size >> PAGE_SHIFT;
+       struct page **pages;
+       int ret;
+
+       pages = kvmalloc_array(npages, sizeof(*bo->pages), GFP_KERNEL);
+       if (!pages)
+               return -ENOMEM;
+
+       for (i = 0; i < npages; i++) {
+               pages[i] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
+               if (!pages[i]) {
+                       ret = -ENOMEM;
+                       goto err_free_pages;
+               }
+               cond_resched();
+       }
+
+       bo->pages = pages;
+       return 0;
+
+err_free_pages:
+       while (i--)
+               put_page(pages[i]);
+       kvfree(pages);
+       return ret;
+}
+
+static void internal_free_pages_locked(struct ivpu_bo *bo)
+{
+       unsigned int i, npages = bo->base.size >> PAGE_SHIFT;
+
+       for (i = 0; i < npages; i++)
+               put_page(bo->pages[i]);
+
+       kvfree(bo->pages);
+       bo->pages = NULL;
+}
+
+static const struct ivpu_bo_ops internal_ops = {
+       .type = IVPU_BO_TYPE_INTERNAL,
+       .name = "internal",
+       .alloc_pages = internal_alloc_pages_locked,
+       .free_pages = internal_free_pages_locked,
+       .map_pages = ivpu_bo_map_pages_locked,
+       .unmap_pages = ivpu_bo_unmap_pages_locked,
+};
+
+static int __must_check ivpu_bo_alloc_and_map_pages_locked(struct ivpu_bo *bo)
+{
+       struct ivpu_device *vdev = ivpu_bo_to_vdev(bo);
+       int ret;
+
+       lockdep_assert_held(&bo->lock);
+       drm_WARN_ON(&vdev->drm, bo->sgt);
+
+       ret = bo->ops->alloc_pages(bo);
+       if (ret) {
+               ivpu_err(vdev, "Failed to allocate pages for BO: %d", ret);
+               return ret;
+       }
+
+       ret = bo->ops->map_pages(bo);
+       if (ret) {
+               ivpu_err(vdev, "Failed to map pages for BO: %d", ret);
+               goto err_free_pages;
+       }
+       return ret;
+
+err_free_pages:
+       bo->ops->free_pages(bo);
+       return ret;
+}
+
+static void ivpu_bo_unmap_and_free_pages(struct ivpu_bo *bo)
+{
+       mutex_lock(&bo->lock);
+
+       WARN_ON(!bo->sgt);
+       bo->ops->unmap_pages(bo);
+       WARN_ON(bo->sgt);
+       bo->ops->free_pages(bo);
+       WARN_ON(bo->pages);
+
+       mutex_unlock(&bo->lock);
+}
+
+/*
+ * ivpu_bo_pin() - pin the backing physical pages and map them to VPU.
+ *
+ * This function pins physical memory pages, then maps the physical pages
+ * to IOMMU address space and finally updates the VPU MMU page tables
+ * to allow the VPU to translate VPU address to IOMMU address.
+ */
+int __must_check ivpu_bo_pin(struct ivpu_bo *bo)
+{
+       struct ivpu_device *vdev = ivpu_bo_to_vdev(bo);
+       int ret = 0;
+
+       mutex_lock(&bo->lock);
+
+       if (!bo->vpu_addr) {
+               ivpu_err(vdev, "vpu_addr not set for BO ctx_id: %d handle: %d\n",
+                        bo->ctx->id, bo->handle);
+               ret = -EINVAL;
+               goto unlock;
+       }
+
+       if (!bo->sgt) {
+               ret = ivpu_bo_alloc_and_map_pages_locked(bo);
+               if (ret)
+                       goto unlock;
+       }
+
+       if (!bo->mmu_mapped) {
+               ret = ivpu_mmu_context_map_sgt(vdev, bo->ctx, bo->vpu_addr, bo->sgt,
+                                              ivpu_bo_is_snooped(bo));
+               if (ret) {
+                       ivpu_err(vdev, "Failed to map BO in MMU: %d\n", ret);
+                       goto unlock;
+               }
+               bo->mmu_mapped = true;
+       }
+
+unlock:
+       mutex_unlock(&bo->lock);
+
+       return ret;
+}
+
+static int
+ivpu_bo_alloc_vpu_addr(struct ivpu_bo *bo, struct ivpu_mmu_context *ctx,
+                      const struct ivpu_addr_range *range)
+{
+       struct ivpu_device *vdev = ivpu_bo_to_vdev(bo);
+       int ret;
+
+       if (!range) {
+               if (bo->flags & DRM_IVPU_BO_HIGH_MEM)
+                       range = &vdev->hw->ranges.user_high;
+               else
+                       range = &vdev->hw->ranges.user_low;
+       }
+
+       mutex_lock(&ctx->lock);
+       ret = ivpu_mmu_context_insert_node_locked(ctx, range, bo->base.size, &bo->mm_node);
+       if (!ret) {
+               bo->ctx = ctx;
+               bo->vpu_addr = bo->mm_node.start;
+               list_add_tail(&bo->ctx_node, &ctx->bo_list);
+       }
+       mutex_unlock(&ctx->lock);
+
+       return ret;
+}
+
+static void ivpu_bo_free_vpu_addr(struct ivpu_bo *bo)
+{
+       struct ivpu_device *vdev = ivpu_bo_to_vdev(bo);
+       struct ivpu_mmu_context *ctx = bo->ctx;
+
+       ivpu_dbg(vdev, BO, "remove from ctx: ctx %d vpu_addr 0x%llx allocated %d mmu_mapped %d\n",
+                ctx->id, bo->vpu_addr, (bool)bo->sgt, bo->mmu_mapped);
+
+       mutex_lock(&bo->lock);
+
+       if (bo->mmu_mapped) {
+               drm_WARN_ON(&vdev->drm, !bo->sgt);
+               ivpu_mmu_context_unmap_sgt(vdev, ctx, bo->vpu_addr, bo->sgt);
+               bo->mmu_mapped = false;
+       }
+
+       mutex_lock(&ctx->lock);
+       list_del(&bo->ctx_node);
+       bo->vpu_addr = 0;
+       bo->ctx = NULL;
+       ivpu_mmu_context_remove_node_locked(ctx, &bo->mm_node);
+       mutex_unlock(&ctx->lock);
+
+       mutex_unlock(&bo->lock);
+}
+
+void ivpu_bo_remove_all_bos_from_context(struct ivpu_mmu_context *ctx)
+{
+       struct ivpu_bo *bo, *tmp;
+
+       list_for_each_entry_safe(bo, tmp, &ctx->bo_list, ctx_node)
+               ivpu_bo_free_vpu_addr(bo);
+}
+
+static struct ivpu_bo *
+ivpu_bo_alloc(struct ivpu_device *vdev, struct ivpu_mmu_context *mmu_context,
+             u64 size, u32 flags, const struct ivpu_bo_ops *ops,
+             const struct ivpu_addr_range *range, u64 user_ptr)
+{
+       struct ivpu_bo *bo;
+       int ret = 0;
+
+       if (drm_WARN_ON(&vdev->drm, size == 0 || !PAGE_ALIGNED(size)))
+               return ERR_PTR(-EINVAL);
+
+       switch (flags & DRM_IVPU_BO_CACHE_MASK) {
+       case DRM_IVPU_BO_CACHED:
+       case DRM_IVPU_BO_UNCACHED:
+       case DRM_IVPU_BO_WC:
+               break;
+       default:
+               return ERR_PTR(-EINVAL);
+       }
+
+       bo = kzalloc(sizeof(*bo), GFP_KERNEL);
+       if (!bo)
+               return ERR_PTR(-ENOMEM);
+
+       mutex_init(&bo->lock);
+       bo->base.funcs = &ivpu_gem_funcs;
+       bo->flags = flags;
+       bo->ops = ops;
+       bo->user_ptr = user_ptr;
+
+       if (ops->type == IVPU_BO_TYPE_SHMEM)
+               ret = drm_gem_object_init(&vdev->drm, &bo->base, size);
+       else
+               drm_gem_private_object_init(&vdev->drm, &bo->base, size);
+
+       if (ret) {
+               ivpu_err(vdev, "Failed to initialize drm object\n");
+               goto err_free;
+       }
+
+       if (flags & DRM_IVPU_BO_MAPPABLE) {
+               ret = drm_gem_create_mmap_offset(&bo->base);
+               if (ret) {
+                       ivpu_err(vdev, "Failed to allocate mmap offset\n");
+                       goto err_release;
+               }
+       }
+
+       if (mmu_context) {
+               ret = ivpu_bo_alloc_vpu_addr(bo, mmu_context, range);
+               if (ret) {
+                       ivpu_err(vdev, "Failed to add BO to context: %d\n", ret);
+                       goto err_release;
+               }
+       }
+
+       return bo;
+
+err_release:
+       drm_gem_object_release(&bo->base);
+err_free:
+       kfree(bo);
+       return ERR_PTR(ret);
+}
+
+static void ivpu_bo_free(struct drm_gem_object *obj)
+{
+       struct ivpu_bo *bo = to_ivpu_bo(obj);
+       struct ivpu_device *vdev = ivpu_bo_to_vdev(bo);
+
+       if (bo->ctx)
+               ivpu_dbg(vdev, BO, "free: ctx %d vpu_addr 0x%llx allocated %d mmu_mapped %d\n",
+                        bo->ctx->id, bo->vpu_addr, (bool)bo->sgt, bo->mmu_mapped);
+       else
+               ivpu_dbg(vdev, BO, "free: ctx (released) allocated %d mmu_mapped %d\n",
+                        (bool)bo->sgt, bo->mmu_mapped);
+
+       drm_WARN_ON(&vdev->drm, !dma_resv_test_signaled(obj->resv, DMA_RESV_USAGE_READ));
+
+       vunmap(bo->kvaddr);
+
+       if (bo->ctx)
+               ivpu_bo_free_vpu_addr(bo);
+
+       if (bo->sgt)
+               ivpu_bo_unmap_and_free_pages(bo);
+
+       if (bo->base.import_attach)
+               drm_prime_gem_destroy(&bo->base, bo->sgt);
+
+       drm_gem_object_release(&bo->base);
+
+       mutex_destroy(&bo->lock);
+       kfree(bo);
+}
+
+static int ivpu_bo_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
+{
+       struct ivpu_bo *bo = to_ivpu_bo(obj);
+       struct ivpu_device *vdev = ivpu_bo_to_vdev(bo);
+
+       ivpu_dbg(vdev, BO, "mmap: ctx %u handle %u vpu_addr 0x%llx size %zu type %s",
+                bo->ctx->id, bo->handle, bo->vpu_addr, bo->base.size, bo->ops->name);
+
+       if (obj->import_attach) {
+               /* Drop the reference drm_gem_mmap_obj() acquired.*/
+               drm_gem_object_put(obj);
+               vma->vm_private_data = NULL;
+               return dma_buf_mmap(obj->dma_buf, vma, 0);
+       }
+
+       vma->vm_flags |= VM_PFNMAP | VM_DONTEXPAND;
+       vma->vm_page_prot = ivpu_bo_pgprot(bo, vm_get_page_prot(vma->vm_flags));
+
+       return 0;
+}
+
+static struct sg_table *ivpu_bo_get_sg_table(struct drm_gem_object *obj)
+{
+       struct ivpu_bo *bo = to_ivpu_bo(obj);
+       loff_t npages = obj->size >> PAGE_SHIFT;
+       int ret = 0;
+
+       mutex_lock(&bo->lock);
+
+       if (!bo->sgt)
+               ret = ivpu_bo_alloc_and_map_pages_locked(bo);
+
+       mutex_unlock(&bo->lock);
+
+       if (ret)
+               return ERR_PTR(ret);
+
+       return drm_prime_pages_to_sg(obj->dev, bo->pages, npages);
+}
+
+static vm_fault_t ivpu_vm_fault(struct vm_fault *vmf)
+{
+       struct vm_area_struct *vma = vmf->vma;
+       struct drm_gem_object *obj = vma->vm_private_data;
+       struct ivpu_bo *bo = to_ivpu_bo(obj);
+       loff_t npages = obj->size >> PAGE_SHIFT;
+       pgoff_t page_offset;
+       struct page *page;
+       vm_fault_t ret;
+       int err;
+
+       mutex_lock(&bo->lock);
+
+       if (!bo->sgt) {
+               err = ivpu_bo_alloc_and_map_pages_locked(bo);
+               if (err) {
+                       ret = vmf_error(err);
+                       goto unlock;
+               }
+       }
+
+       /* We don't use vmf->pgoff since that has the fake offset */
+       page_offset = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
+       if (page_offset >= npages) {
+               ret = VM_FAULT_SIGBUS;
+       } else {
+               page = bo->pages[page_offset];
+               ret = vmf_insert_pfn(vma, vmf->address, page_to_pfn(page));
+       }
+
+unlock:
+       mutex_unlock(&bo->lock);
+
+       return ret;
+}
+
+static const struct vm_operations_struct ivpu_vm_ops = {
+       .fault = ivpu_vm_fault,
+       .open = drm_gem_vm_open,
+       .close = drm_gem_vm_close,
+};
+
+static const struct drm_gem_object_funcs ivpu_gem_funcs = {
+       .free = ivpu_bo_free,
+       .mmap = ivpu_bo_mmap,
+       .vm_ops = &ivpu_vm_ops,
+       .get_sg_table = ivpu_bo_get_sg_table,
+};
+
+int
+ivpu_bo_create_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
+{
+       struct ivpu_file_priv *file_priv = file->driver_priv;
+       struct ivpu_device *vdev = file_priv->vdev;
+       struct drm_ivpu_bo_create *args = data;
+       u64 size = PAGE_ALIGN(args->size);
+       struct ivpu_bo *bo;
+       int ret;
+
+       if (args->flags & ~DRM_IVPU_BO_FLAGS)
+               return -EINVAL;
+
+       if (size == 0)
+               return -EINVAL;
+
+       bo = ivpu_bo_alloc(vdev, &file_priv->ctx, size, args->flags, &shmem_ops, NULL, 0);
+       if (IS_ERR(bo)) {
+               ivpu_err(vdev, "Failed to create BO: %pe (ctx %u size %llu flags 0x%x)",
+                        bo, file_priv->ctx.id, args->size, args->flags);
+               return PTR_ERR(bo);
+       }
+
+       ret = drm_gem_handle_create(file, &bo->base, &bo->handle);
+       if (!ret) {
+               args->vpu_addr = bo->vpu_addr;
+               args->handle = bo->handle;
+       }
+
+       drm_gem_object_put(&bo->base);
+
+       ivpu_dbg(vdev, BO, "alloc shmem: ctx %u vpu_addr 0x%llx size %zu flags 0x%x\n",
+                file_priv->ctx.id, bo->vpu_addr, bo->base.size, bo->flags);
+
+       return ret;
+}
+
+int
+ivpu_bo_userptr_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
+{
+       struct ivpu_file_priv *file_priv = file->driver_priv;
+       struct ivpu_device *vdev = file_priv->vdev;
+       struct drm_ivpu_bo_userptr *args = data;
+       struct ivpu_bo *bo;
+       int ret;
+
+       if (args->user_ptr == 0 || !PAGE_ALIGNED(args->user_ptr))
+               return -EINVAL;
+
+       if (args->user_size == 0 || !PAGE_ALIGNED(args->user_size))
+               return -EINVAL;
+
+       if (args->flags & ~DRM_IVPU_BO_HIGH_MEM)
+               return -EINVAL;
+
+       if (!access_ok((const void __user *)args->user_ptr, args->user_size))
+               return -EFAULT;
+
+       bo = ivpu_bo_alloc(vdev, &file_priv->ctx, args->user_size, args->flags,
+                          &userptr_ops, NULL, args->user_ptr);
+       if (IS_ERR(bo)) {
+               ivpu_err(vdev, "Failed to create BO: %pe (ctx %u size %llu flags 0x%x)",
+                        bo, file_priv->ctx.id, args->user_size, args->flags);
+               return PTR_ERR(bo);
+       }
+
+       if (!bo)
+               return -ENOMEM;
+
+       lockdep_set_class(&bo->lock, &userptr_bo_lock_class_key);
+
+       ret = drm_gem_handle_create(file, &bo->base, &bo->handle);
+       if (!ret) {
+               args->vpu_addr = bo->vpu_addr;
+               args->handle = bo->handle;
+       }
+
+       drm_gem_object_put(&bo->base);
+
+       ivpu_dbg(vdev, BO, "alloc userptr: ctx %u vpu_addr 0x%llx size %zu flags 0x%x\n",
+                file_priv->ctx.id, bo->vpu_addr, bo->base.size, args->flags);
+
+       return ret;
+}
+
+struct ivpu_bo *
+ivpu_bo_alloc_internal(struct ivpu_device *vdev, u64 vpu_addr, u64 size, u32 flags)
+{
+       const struct ivpu_addr_range *range;
+       struct ivpu_addr_range fixed_range;
+       struct ivpu_bo *bo;
+       pgprot_t prot;
+       int ret;
+
+       drm_WARN_ON(&vdev->drm, !PAGE_ALIGNED(vpu_addr));
+       drm_WARN_ON(&vdev->drm, !PAGE_ALIGNED(size));
+
+       if (vpu_addr) {
+               fixed_range.start = vpu_addr;
+               fixed_range.end = vpu_addr + size;
+               range = &fixed_range;
+       } else {
+               range = &vdev->hw->ranges.global_low;
+       }
+
+       bo = ivpu_bo_alloc(vdev, &vdev->gctx, size, flags, &internal_ops, range, 0);
+       if (IS_ERR(bo)) {
+               ivpu_err(vdev, "Failed to create BO: %pe (vpu_addr 0x%llx size %llu flags 0x%x)",
+                        bo, vpu_addr, size, flags);
+               return NULL;
+       }
+
+       ret = ivpu_bo_pin(bo);
+       if (ret)
+               goto err_put;
+
+       if (ivpu_bo_cache_mode(bo) != DRM_IVPU_BO_CACHED)
+               drm_clflush_pages(bo->pages, bo->base.size >> PAGE_SHIFT);
+
+       prot = ivpu_bo_pgprot(bo, PAGE_KERNEL);
+       bo->kvaddr = vmap(bo->pages, bo->base.size >> PAGE_SHIFT, VM_MAP, prot);
+       if (!bo->kvaddr) {
+               ivpu_err(vdev, "Failed to map BO into kernel virtual memory\n");
+               goto err_put;
+       }
+
+       ivpu_dbg(vdev, BO, "alloc internal: ctx 0 vpu_addr 0x%llx size %zu flags 0x%x\n",
+                bo->vpu_addr, bo->base.size, flags);
+
+       return bo;
+
+err_put:
+       drm_gem_object_put(&bo->base);
+       return NULL;
+}
+
+void ivpu_bo_free_internal(struct ivpu_bo *bo)
+{
+       drm_gem_object_put(&bo->base);
+}
+
+struct drm_gem_object *ivpu_gem_prime_import(struct drm_device *dev, struct dma_buf *buf)
+{
+       struct ivpu_device *vdev = to_ivpu_device(dev);
+       struct dma_buf_attachment *attach;
+       struct ivpu_bo *bo;
+
+       attach = dma_buf_attach(buf, dev->dev);
+       if (IS_ERR(attach))
+               return ERR_CAST(attach);
+
+       get_dma_buf(buf);
+
+       bo = ivpu_bo_alloc(vdev, NULL, buf->size, DRM_IVPU_BO_MAPPABLE, &prime_ops, NULL, 0);
+       if (IS_ERR(bo)) {
+               ivpu_err(vdev, "Failed to import BO: %pe (size %lu)", bo, buf->size);
+               goto err_detach;
+       }
+
+       lockdep_set_class(&bo->lock, &prime_bo_lock_class_key);
+
+       bo->base.import_attach = attach;
+
+       return &bo->base;
+
+err_detach:
+       dma_buf_detach(buf, attach);
+       dma_buf_put(buf);
+       return ERR_CAST(bo);
+}
+
+int ivpu_bo_info_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
+{
+       struct ivpu_file_priv *file_priv = file->driver_priv;
+       struct ivpu_device *vdev = to_ivpu_device(dev);
+       struct drm_ivpu_bo_info *args = data;
+       struct drm_gem_object *obj;
+       struct ivpu_bo *bo;
+       int ret = 0;
+
+       obj = drm_gem_object_lookup(file, args->handle);
+       if (!obj)
+               return -ENOENT;
+
+       bo = to_ivpu_bo(obj);
+
+       mutex_lock(&bo->lock);
+
+       if (!bo->ctx) {
+               ret = ivpu_bo_alloc_vpu_addr(bo, &file_priv->ctx, NULL);
+               if (ret) {
+                       ivpu_err(vdev, "Failed to allocate vpu_addr: %d\n", ret);
+                       goto unlock;
+               }
+       }
+
+       args->flags = bo->flags;
+       args->mmap_offset = drm_vma_node_offset_addr(&obj->vma_node);
+       args->vpu_addr = bo->vpu_addr;
+       args->size = obj->size;
+unlock:
+       mutex_unlock(&bo->lock);
+       drm_gem_object_put(obj);
+       return ret;
+}
+
+static void ivpu_bo_print_info(struct ivpu_bo *bo, struct drm_printer *p)
+{
+       unsigned long dma_refcount = 0;
+
+       if (bo->base.dma_buf && bo->base.dma_buf->file)
+               dma_refcount = atomic_long_read(&bo->base.dma_buf->file->f_count);
+
+       drm_printf(p, "%5u %6d %16llx %10lu %10u %12lu %14s\n",
+                  bo->ctx->id, bo->handle, bo->vpu_addr, bo->base.size,
+                  kref_read(&bo->base.refcount), dma_refcount, bo->ops->name);
+}
+
+void ivpu_bo_list(struct drm_device *dev, struct drm_printer *p)
+{
+       struct ivpu_device *vdev = to_ivpu_device(dev);
+       struct ivpu_file_priv *file_priv;
+       unsigned long ctx_id;
+       struct ivpu_bo *bo;
+
+       drm_printf(p, "%5s %6s %16s %10s %10s %12s %14s\n",
+                  "ctx", "handle", "vpu_addr", "size", "refcount", "dma_refcount", "type");
+
+       mutex_lock(&vdev->gctx.lock);
+       list_for_each_entry(bo, &vdev->gctx.bo_list, ctx_node)
+               ivpu_bo_print_info(bo, p);
+       mutex_unlock(&vdev->gctx.lock);
+
+       xa_for_each(&vdev->context_xa, ctx_id, file_priv) {
+               file_priv = ivpu_file_priv_get_by_ctx_id(vdev, ctx_id);
+               if (!file_priv)
+                       continue;
+
+               mutex_lock(&file_priv->ctx.lock);
+               list_for_each_entry(bo, &file_priv->ctx.bo_list, ctx_node)
+                       ivpu_bo_print_info(bo, p);
+               mutex_unlock(&file_priv->ctx.lock);
+
+               ivpu_file_priv_put(&file_priv);
+       }
+}
+
+void ivpu_bo_list_print(struct drm_device *dev)
+{
+       struct drm_printer p = drm_info_printer(dev->dev);
+
+       ivpu_bo_list(dev, &p);
+}
drivers/accel/ivpu/ivpu_gem.h new file +128 -0
new file mode 100644
index 000000000000..d71e6fe207d4
@@ -0,0 +1,128 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2020-2023 Intel Corporation
+ */
+#ifndef __IVPU_GEM_H__
+#define __IVPU_GEM_H__
+
+#include <drm/drm_gem.h>
+#include <drm/drm_mm.h>
+
+struct dma_buf;
+struct ivpu_bo_ops;
+struct ivpu_file_priv;
+
+struct ivpu_bo {
+       struct drm_gem_object base;
+       const struct ivpu_bo_ops *ops;
+
+       struct ivpu_mmu_context *ctx;
+       struct list_head ctx_node;
+       struct drm_mm_node mm_node;
+
+       struct mutex lock; /* Protects: pages, sgt, mmu_mapped */
+       struct sg_table *sgt;
+       struct page **pages;
+       bool mmu_mapped;
+
+       void *kvaddr;
+       u64 vpu_addr;
+       u32 handle;
+       u32 flags;
+       uintptr_t user_ptr;
+};
+
+enum ivpu_bo_type {
+       IVPU_BO_TYPE_SHMEM = 1,
+       IVPU_BO_TYPE_USERPTR,
+       IVPU_BO_TYPE_INTERNAL,
+       IVPU_BO_TYPE_PRIME,
+};
+
+struct ivpu_bo_ops {
+       enum ivpu_bo_type type;
+       const char *name;
+       int (*alloc_pages)(struct ivpu_bo *bo);
+       void (*free_pages)(struct ivpu_bo *bo);
+       int (*map_pages)(struct ivpu_bo *bo);
+       void (*unmap_pages)(struct ivpu_bo *bo);
+};
+
+int ivpu_bo_pin(struct ivpu_bo *bo);
+void ivpu_bo_remove_all_bos_from_context(struct ivpu_mmu_context *ctx);
+void ivpu_bo_list(struct drm_device *dev, struct drm_printer *p);
+void ivpu_bo_list_print(struct drm_device *dev);
+
+struct ivpu_bo *
+ivpu_bo_alloc_internal(struct ivpu_device *vdev, u64 vpu_addr, u64 size, u32 flags);
+void ivpu_bo_free_internal(struct ivpu_bo *bo);
+struct drm_gem_object *ivpu_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf);
+void ivpu_bo_unmap_sgt_and_remove_from_context(struct ivpu_bo *bo);
+
+int ivpu_bo_create_ioctl(struct drm_device *dev, void *data, struct drm_file *file);
+int ivpu_bo_info_ioctl(struct drm_device *dev, void *data, struct drm_file *file);
+int ivpu_bo_userptr_ioctl(struct drm_device *dev, void *data, struct drm_file *file);
+int ivpu_bo_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file);
+
+static inline struct ivpu_bo *to_ivpu_bo(struct drm_gem_object *obj)
+{
+       return container_of(obj, struct ivpu_bo, base);
+}
+
+static inline struct page *ivpu_bo_get_page(struct ivpu_bo *bo, u64 offset)
+{
+       if (offset > bo->base.size || !bo->pages)
+               return NULL;
+
+       return bo->pages[offset / PAGE_SIZE];
+}
+
+static inline u32 ivpu_bo_cache_mode(struct ivpu_bo *bo)
+{
+       return bo->flags & DRM_IVPU_BO_CACHE_MASK;
+}
+
+static inline bool ivpu_bo_is_snooped(struct ivpu_bo *bo)
+{
+       return ivpu_bo_cache_mode(bo) == DRM_IVPU_BO_CACHED;
+}
+
+static inline pgprot_t ivpu_bo_pgprot(struct ivpu_bo *bo, pgprot_t prot)
+{
+       if (bo->flags & DRM_IVPU_BO_WC)
+               return pgprot_writecombine(prot);
+
+       if (bo->flags & DRM_IVPU_BO_UNCACHED)
+               return pgprot_noncached(prot);
+
+       return prot;
+}
+
+static inline struct ivpu_device *ivpu_bo_to_vdev(struct ivpu_bo *bo)
+{
+       return to_ivpu_device(bo->base.dev);
+}
+
+static inline void *ivpu_to_cpu_addr(struct ivpu_bo *bo, u32 vpu_addr)
+{
+       if (vpu_addr < bo->vpu_addr)
+               return NULL;
+
+       if (vpu_addr >= (bo->vpu_addr + bo->base.size))
+               return NULL;
+
+       return bo->kvaddr + (vpu_addr - bo->vpu_addr);
+}
+
+static inline u32 cpu_to_vpu_addr(struct ivpu_bo *bo, void *cpu_addr)
+{
+       if (cpu_addr < bo->kvaddr)
+               return 0;
+
+       if (cpu_addr >= (bo->kvaddr + bo->base.size))
+               return 0;
+
+       return bo->vpu_addr + (cpu_addr - bo->kvaddr);
+}
+
+#endif /* __IVPU_GEM_H__ */
include/uapi/drm/ivpu_accel.h +127 -1
index 543347df51a1..5f953ebafc9b 100644
@@ -17,6 +17,9 @@ extern "C" {
 
 #define DRM_IVPU_GET_PARAM               0x00
 #define DRM_IVPU_SET_PARAM               0x01
+#define DRM_IVPU_BO_CREATE               0x02
+#define DRM_IVPU_BO_INFO                 0x03
+#define DRM_IVPU_BO_USERPTR              0x04
 
 #define DRM_IOCTL_IVPU_GET_PARAM                                               \
        DRM_IOWR(DRM_COMMAND_BASE + DRM_IVPU_GET_PARAM, struct drm_ivpu_param)
@@ -24,6 +27,15 @@ extern "C" {
 #define DRM_IOCTL_IVPU_SET_PARAM                                               \
        DRM_IOW(DRM_COMMAND_BASE + DRM_IVPU_SET_PARAM, struct drm_ivpu_param)
 
+#define DRM_IOCTL_IVPU_BO_CREATE                                               \
+       DRM_IOWR(DRM_COMMAND_BASE + DRM_IVPU_BO_CREATE, struct drm_ivpu_bo_create)
+
+#define DRM_IOCTL_IVPU_BO_INFO                                                 \
+       DRM_IOWR(DRM_COMMAND_BASE + DRM_IVPU_BO_INFO, struct drm_ivpu_bo_info)
+
+#define DRM_IOCTL_IVPU_BO_USERPTR                                              \
+       DRM_IOWR(DRM_COMMAND_BASE + DRM_IVPU_BO_USERPTR, struct drm_ivpu_bo_userptr)
+
 /**
  * DOC: contexts
  *
@@ -92,6 +104,121 @@ struct drm_ivpu_param {
        __u64 value;
 };
 
+#define DRM_IVPU_BO_HIGH_MEM   0x00000001
+#define DRM_IVPU_BO_MAPPABLE   0x00000002
+
+#define DRM_IVPU_BO_CACHED     0x00000000
+#define DRM_IVPU_BO_UNCACHED   0x00010000
+#define DRM_IVPU_BO_WC        0x00020000
+#define DRM_IVPU_BO_CACHE_MASK 0x00030000
+
+#define DRM_IVPU_BO_FLAGS \
+       (DRM_IVPU_BO_HIGH_MEM | \
+        DRM_IVPU_BO_MAPPABLE | \
+        DRM_IVPU_BO_CACHE_MASK)
+
+/**
+ * struct drm_ivpu_bo_create - Create BO backed by SHMEM
+ *
+ * Create GEM buffer object allocated in SHMEM memory.
+ */
+struct drm_ivpu_bo_create {
+       /** @size: The size in bytes of the allocated memory */
+       __u64 size;
+
+       /**
+        * @flags:
+        *
+        * Supported flags:
+        *
+        * %DRM_IVPU_BO_HIGH_MEM:
+        *
+        * Allocate VPU address from >4GB range.
+        * Buffer object with vpu address >4GB can be always accessed by the
+        * VPU DMA engine, but some HW generation may not be able to access
+        * this memory from then firmware running on the VPU management processor.
+        * Suitable for input, output and some scratch buffers.
+        *
+        * %DRM_IVPU_BO_MAPPABLE:
+        *
+        * Buffer object can be mapped using mmap().
+        *
+        * %DRM_IVPU_BO_CACHED:
+        *
+        * Allocated BO will be cached on host side (WB) and snooped on the VPU side.
+        * This is the default caching mode.
+        *
+        * %DRM_IVPU_BO_UNCACHED:
+        *
+        * Allocated BO will not be cached on host side nor snooped on the VPU side.
+        *
+        * %DRM_IVPU_BO_WC:
+        *
+        * Allocated BO will use write combining buffer for writes but reads will be
+        * uncached.
+        */
+       __u32 flags;
+
+       /** @handle: Returned GEM object handle */
+       __u32 handle;
+
+       /** @vpu_addr: Returned VPU virtual address */
+       __u64 vpu_addr;
+};
+
+/**
+ * struct drm_ivpu_bo_info - Query buffer object info
+ */
+struct drm_ivpu_bo_info {
+       /** @handle: Handle of the queried BO */
+       __u32 handle;
+
+       /** @flags: Returned flags used to create the BO */
+       __u32 flags;
+
+       /** @vpu_addr: Returned VPU virtual address */
+       __u64 vpu_addr;
+
+       /**
+        * @mmap_offset:
+        *
+        * Returned offset to be used in mmap(). 0 in case the BO is not mappable.
+        */
+       __u64 mmap_offset;
+
+       /** @size: Returned GEM object size, aligned to PAGE_SIZE */
+       __u64 size;
+};
+
+/**
+ * struct drm_ivpu_bo_userptr - Create BO from user memory
+ *
+ * Create GEM buffer object from user allocated memory. The provided @user_ptr
+ * has to be page aligned. BOs created using this ioctl are always cacheable.
+ */
+struct drm_ivpu_bo_userptr {
+       /** @user_ptr: User allocated pointer aligned to PAGE_SIZE */
+       __u64 user_ptr;
+
+       /** @user_size: The size in bytes of the allocated memory */
+       __u64 user_size;
+
+       /**
+        * @flags:
+        *
+        * Supported flags:
+        *
+        * %DRM_IVPU_BO_HIGH_MEM: see &drm_ivpu_bo_create->flags
+        */
+       __u32 flags;
+
+       /** @handle: Returned GEM object handle */
+       __u32 handle;
+
+       /** @vpu_addr: Returned VPU virtual address */
+       __u64 vpu_addr;
+};
+
 #if defined(__cplusplus)
 }
 #endif
--

2.34.1

#9 lore raw

Re: [PATCH v5 3/7] accel/ivpu: Add GEM buffer object management

Hi,

Quoted 295 lines · Oded Gabbay
Quoted 293 lines · Jacek Lawrynowicz

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

All types are implemented as struct ivpu_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 released when the buffer is destroyed.
Eviction mechism is planned for future versions.

Add three new IOCTLs: BO_CREATE, BO_INFO, BO_USERPTR

---
 drivers/accel/ivpu/Makefile   |   1 +
 drivers/accel/ivpu/ivpu_drv.c |  31 +-
 drivers/accel/ivpu/ivpu_drv.h |   1 +
 drivers/accel/ivpu/ivpu_gem.c | 820 ++++++++++++++++++++++++++++++++++
 drivers/accel/ivpu/ivpu_gem.h | 128 ++++++
 include/uapi/drm/ivpu_accel.h | 127 ++++++
 6 files changed, 1106 insertions(+), 2 deletions(-)
 create mode 100644 drivers/accel/ivpu/ivpu_gem.c
 create mode 100644 drivers/accel/ivpu/ivpu_gem.h
drivers/accel/ivpu/Makefile +1 -0
index 59cd7843b218..5d7c5862399c 100644
@@ -3,6 +3,7 @@
 
 intel_vpu-y := \
        ivpu_drv.o \
+       ivpu_gem.o \
        ivpu_hw_mtl.o \
        ivpu_mmu.o \
        ivpu_mmu_context.o
drivers/accel/ivpu/ivpu_drv.c +29 -2
index d7982f451781..0b9034499c4c 100644
@@ -12,8 +12,10 @@
 #include <drm/drm_file.h>
 #include <drm/drm_gem.h>
 #include <drm/drm_ioctl.h>
+#include <drm/drm_prime.h>
 
 #include "ivpu_drv.h"
+#include "ivpu_gem.h"
 #include "ivpu_hw.h"
 #include "ivpu_mmu.h"
 #include "ivpu_mmu_context.h"
@@ -49,6 +51,24 @@ struct ivpu_file_priv *ivpu_file_priv_get(struct ivpu_file_priv *file_priv)
        return file_priv;
 }
 
+struct ivpu_file_priv *ivpu_file_priv_get_by_ctx_id(struct ivpu_device *vdev, unsigned long id)
+{
+       struct ivpu_file_priv *file_priv;
+
+       xa_lock_irq(&vdev->context_xa);
+       file_priv = xa_load(&vdev->context_xa, id);
+       /* file_priv may still be in context_xa during file_priv_release() */
+       if (file_priv && !kref_get_unless_zero(&file_priv->ref))
+               file_priv = NULL;
+       xa_unlock_irq(&vdev->context_xa);
+
+       if (file_priv)
+               ivpu_dbg(vdev, KREF, "file_priv get by id: ctx %u refcount %u\n",
+                        file_priv->ctx.id, kref_read(&file_priv->ref));
+
+       return file_priv;
+}
+
 static void file_priv_release(struct kref *ref)
 {
        struct ivpu_file_priv *file_priv = container_of(ref, struct ivpu_file_priv, ref);
@@ -57,7 +77,7 @@ static void file_priv_release(struct kref *ref)
        ivpu_dbg(vdev, FILE, "file_priv release: ctx %u\n", file_priv->ctx.id);
 
        ivpu_mmu_user_context_fini(vdev, &file_priv->ctx);
-       WARN_ON(xa_erase_irq(&vdev->context_xa, file_priv->ctx.id) != file_priv);
+       drm_WARN_ON(&vdev->drm, xa_erase_irq(&vdev->context_xa, file_priv->ctx.id) != file_priv);
        kfree(file_priv);
 }
 
@@ -66,7 +86,7 @@ void ivpu_file_priv_put(struct ivpu_file_priv **link)
        struct ivpu_file_priv *file_priv = *link;
        struct ivpu_device *vdev = file_priv->vdev;
 
-       WARN_ON(!file_priv);
+       drm_WARN_ON(&vdev->drm, !file_priv);
 
        ivpu_dbg(vdev, KREF, "file_priv put: ctx %u refcount %u\n",
                 file_priv->ctx.id, kref_read(&file_priv->ref));
@@ -200,6 +220,9 @@ static void ivpu_postclose(struct drm_device *dev, struct drm_file *file)
 static const struct drm_ioctl_desc ivpu_drm_ioctls[] = {
        DRM_IOCTL_DEF_DRV(IVPU_GET_PARAM, ivpu_get_param_ioctl, 0),
        DRM_IOCTL_DEF_DRV(IVPU_SET_PARAM, ivpu_set_param_ioctl, 0),
+       DRM_IOCTL_DEF_DRV(IVPU_BO_CREATE, ivpu_bo_create_ioctl, 0),
+       DRM_IOCTL_DEF_DRV(IVPU_BO_INFO, ivpu_bo_info_ioctl, 0),
+       DRM_IOCTL_DEF_DRV(IVPU_BO_USERPTR, ivpu_bo_userptr_ioctl, 0),
 };
 
 int ivpu_shutdown(struct ivpu_device *vdev)
@@ -233,6 +256,10 @@ static const struct drm_driver driver = {
 
        .open = ivpu_open,
        .postclose = ivpu_postclose,
+       .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
+       .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
+       .gem_prime_import = ivpu_gem_prime_import,
+       .gem_prime_mmap = drm_gem_prime_mmap,
 
        .ioctls = ivpu_drm_ioctls,
        .num_ioctls = ARRAY_SIZE(ivpu_drm_ioctls),
drivers/accel/ivpu/ivpu_drv.h +1 -0
index a749a0b97703..e8a43dbe5a3a 100644
@@ -115,6 +115,7 @@ extern u8 ivpu_pll_min_ratio;
 extern u8 ivpu_pll_max_ratio;
 
 struct ivpu_file_priv *ivpu_file_priv_get(struct ivpu_file_priv *file_priv);
+struct ivpu_file_priv *ivpu_file_priv_get_by_ctx_id(struct ivpu_device *vdev, unsigned long id);
 void ivpu_file_priv_put(struct ivpu_file_priv **link);
 int ivpu_shutdown(struct ivpu_device *vdev);
 
drivers/accel/ivpu/ivpu_gem.c new file +154 -0
new file mode 100644
index 000000000000..f6d1937c798f
@@ -0,0 +1,820 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2020-2023 Intel Corporation
+ */
+
+#include <linux/dma-buf.h>
+#include <linux/highmem.h>
+#include <linux/module.h>
+#include <linux/set_memory.h>
+#include <linux/xarray.h>
+
+#include <drm/drm_cache.h>
+#include <drm/drm_debugfs.h>
+#include <drm/drm_file.h>
+#include <drm/drm_utils.h>
+
+#include "ivpu_drv.h"
+#include "ivpu_gem.h"
+#include "ivpu_hw.h"
+#include "ivpu_mmu.h"
+#include "ivpu_mmu_context.h"
+
+MODULE_IMPORT_NS(DMA_BUF);
+
+static const struct drm_gem_object_funcs ivpu_gem_funcs;
+
+static struct lock_class_key prime_bo_lock_class_key;
+static struct lock_class_key userptr_bo_lock_class_key;
+
+static int __must_check prime_alloc_pages_locked(struct ivpu_bo *bo)
+{
+       /* Pages are managed by the underlying dma-buf */
+       return 0;
+}
+
+static void prime_free_pages_locked(struct ivpu_bo *bo)
+{
+       /* Pages are managed by the underlying dma-buf */
+}
+
+static int prime_map_pages_locked(struct ivpu_bo *bo)
+{
+       struct ivpu_device *vdev = ivpu_bo_to_vdev(bo);
+       struct sg_table *sgt;
+
+       WARN_ON(!bo->base.import_attach);
+
+       sgt = dma_buf_map_attachment(bo->base.import_attach, DMA_BIDIRECTIONAL);
+       if (IS_ERR(sgt)) {
+               ivpu_err(vdev, "Failed to map attachment: %ld\n", PTR_ERR(sgt));
+               return PTR_ERR(sgt);
+       }
+
+       bo->sgt = sgt;
+       return 0;
+}
+
+static void prime_unmap_pages_locked(struct ivpu_bo *bo)
+{
+       WARN_ON(!bo->base.import_attach);
+
+       dma_buf_unmap_attachment(bo->base.import_attach, bo->sgt, DMA_BIDIRECTIONAL);
+       bo->sgt = NULL;
+}
+
+static const struct ivpu_bo_ops prime_ops = {
+       .type = IVPU_BO_TYPE_PRIME,
+       .name = "prime",
+       .alloc_pages = prime_alloc_pages_locked,
+       .free_pages = prime_free_pages_locked,
+       .map_pages = prime_map_pages_locked,
+       .unmap_pages = prime_unmap_pages_locked,
+};
+
+static int __must_check shmem_alloc_pages_locked(struct ivpu_bo *bo)
+{
+       int npages = bo->base.size >> PAGE_SHIFT;
+       struct page **pages;
+
+       pages = drm_gem_get_pages(&bo->base);
+       if (IS_ERR(pages))
+               return PTR_ERR(pages);
+
+       if (bo->flags & DRM_IVPU_BO_WC)
+               set_pages_array_wc(pages, npages);
+       else if (bo->flags & DRM_IVPU_BO_UNCACHED)
+               set_pages_array_uc(pages, npages);
+
+       bo->pages = pages;
+       return 0;
+}
+
+static void shmem_free_pages_locked(struct ivpu_bo *bo)
+{
+       if (ivpu_bo_cache_mode(bo) != DRM_IVPU_BO_CACHED)
+               set_pages_array_wb(bo->pages, bo->base.size >> PAGE_SHIFT);
+
+       drm_gem_put_pages(&bo->base, bo->pages, true, false);
+       bo->pages = NULL;
+}
+
+static int ivpu_bo_map_pages_locked(struct ivpu_bo *bo)
+{
+       int npages = bo->base.size >> PAGE_SHIFT;
+       struct ivpu_device *vdev = ivpu_bo_to_vdev(bo);
+       struct sg_table *sgt;
+       int ret;
+
+       sgt = drm_prime_pages_to_sg(&vdev->drm, bo->pages, npages);
+       if (IS_ERR(sgt)) {
+               ivpu_err(vdev, "Failed to allocate sgtable\n");
+               return PTR_ERR(sgt);
+       }
+
+       ret = dma_map_sgtable(vdev->drm.dev, sgt, DMA_BIDIRECTIONAL, 0);
+       if (ret) {
+               ivpu_err(vdev, "Failed to map BO in IOMMU: %d\n", ret);
+               goto err_free_sgt;
+       }
+
+       bo->sgt = sgt;
+       return 0;
+
+err_free_sgt:
+       kfree(sgt);
+       return ret;
+}
+
+static void ivpu_bo_unmap_pages_locked(struct ivpu_bo *bo)
+{
+       struct ivpu_device *vdev = ivpu_bo_to_vdev(bo);
+
+       dma_unmap_sgtable(vdev->drm.dev, bo->sgt, DMA_BIDIRECTIONAL, 0);
+       sg_free_table(bo->sgt);
+       kfree(bo->sgt);
+       bo->sgt = NULL;
+}
+
+static const struct ivpu_bo_ops shmem_ops = {
+       .type = IVPU_BO_TYPE_SHMEM,
+       .name = "shmem",
+       .alloc_pages = shmem_alloc_pages_locked,
+       .free_pages = shmem_free_pages_locked,
+       .map_pages = ivpu_bo_map_pages_locked,
+       .unmap_pages = ivpu_bo_unmap_pages_locked,
+};
+
+static int __must_check userptr_alloc_pages_locked(struct ivpu_bo *bo)
+{
+       unsigned int npages = bo->base.size >> PAGE_SHIFT;
+       struct page **pages;
+       int ret;
+
+       pages = kvmalloc_array(npages, sizeof(*bo->pages), GFP_KERNEL);
Quoted 8 lines

drivers/accel/ivpu/ivpu_gem.c line 159

Quoted 5 lines
drivers/accel/ivpu/ivpu_gem.c new file +5 -0
+       if (!pages)
+               return -ENOMEM;
+
+       ret = pin_user_pages_fast(bo->user_ptr & PAGE_MASK, npages,
+                                 FOLL_FORCE | FOLL_WRITE | FOLL_LONGTERM, pages);

You didn't fix this patch according to my comments in v4. Please remove FOLL_FORCE and
use unpin_user_pages_dirty_lock (with true flag)

Right, sorry. I've added this change to our internal repo but missed it in the patch set.

Regards,
Jacek

Reviewed-by on v5 3/7
#10 lore raw

Re: [PATCH v5 3/7] accel/ivpu: Add GEM buffer object management

In reply to Jacek Lawrynowicz
Quoted 6 lines · Jacek Lawrynowicz

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

Quoted 9 lines

All types are implemented as struct ivpu_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 released when the buffer is destroyed.
Eviction mechism is planned for future versions.

mechism -> mechanism

Hopefully whomever applies this can fix it up inline and this doesn't require a new revision.

Quoted 4 lines

Add three new IOCTLs: BO_CREATE, BO_INFO, BO_USERPTR

#11 lore raw

Re: [PATCH v5 3/7] accel/ivpu: Add GEM buffer object management

Hi,

Quoted 17 lines · Jeffrey Hugo
Quoted 6 lines · Jacek Lawrynowicz

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

Quoted 9 lines

All types are implemented as struct ivpu_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 released when the buffer is destroyed.
Eviction mechism is planned for future versions.

Quoted 4 lines

mechism -> mechanism

Hopefully whomever applies this can fix it up inline and this doesn't require a new revision.

It's fine, I'm planning to send v6 early next week anyway to fix some other stuff as well.

Regards,
Jacek

#12 lore raw

PATCH v5 4/7 accel/ivpu: 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/accel/ivpu/Makefile       |   2 +
 drivers/accel/ivpu/ivpu_drv.c     |  13 +
 drivers/accel/ivpu/ivpu_drv.h     |   2 +
 drivers/accel/ivpu/ivpu_hw_mtl.c  |   4 +
 drivers/accel/ivpu/ivpu_ipc.c     | 505 +++++++++++++++
 drivers/accel/ivpu/ivpu_ipc.h     |  93 +++
 drivers/accel/ivpu/ivpu_jsm_msg.c | 170 +++++
 drivers/accel/ivpu/ivpu_jsm_msg.h |  23 +
 drivers/accel/ivpu/vpu_jsm_api.h  | 999 ++++++++++++++++++++++++++++++
 9 files changed, 1811 insertions(+)
 create mode 100644 drivers/accel/ivpu/ivpu_ipc.c
 create mode 100644 drivers/accel/ivpu/ivpu_ipc.h
 create mode 100644 drivers/accel/ivpu/ivpu_jsm_msg.c
 create mode 100644 drivers/accel/ivpu/ivpu_jsm_msg.h
 create mode 100644 drivers/accel/ivpu/vpu_jsm_api.h
drivers/accel/ivpu/Makefile +2 -0
index 5d7c5862399c..46595f0112e3 100644
@@ -5,6 +5,8 @@ intel_vpu-y := \
 	ivpu_drv.o \
 	ivpu_gem.o \
 	ivpu_hw_mtl.o \
+	ivpu_ipc.o \
+	ivpu_jsm_msg.o \
 	ivpu_mmu.o \
 	ivpu_mmu_context.o
 
drivers/accel/ivpu/ivpu_drv.c +13 -0
index 0b9034499c4c..6643ae6b5a52 100644
@@ -17,6 +17,7 @@
 #include "ivpu_drv.h"
 #include "ivpu_gem.h"
 #include "ivpu_hw.h"
+#include "ivpu_ipc.h"
 #include "ivpu_mmu.h"
 #include "ivpu_mmu_context.h"
 
@@ -230,6 +231,7 @@ int ivpu_shutdown(struct ivpu_device *vdev)
 	int ret;
 
 	ivpu_hw_irq_disable(vdev);
+	ivpu_ipc_disable(vdev);
 	ivpu_mmu_disable(vdev);
 
 	ret = ivpu_hw_power_down(vdev);
@@ -346,6 +348,10 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
 	if (!vdev->mmu)
 		return -ENOMEM;
 
+	vdev->ipc = drmm_kzalloc(&vdev->drm, sizeof(*vdev->ipc), GFP_KERNEL);
+	if (!vdev->ipc)
+		return -ENOMEM;
+
 	vdev->hw->ops = &ivpu_hw_mtl_ops;
 	vdev->platform = IVPU_PLATFORM_INVALID;
 	vdev->context_xa_limit.min = IVPU_GLOBAL_CONTEXT_MMU_SSID + 1;
@@ -390,6 +396,12 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
 		goto err_mmu_gctx_fini;
 	}
 
+	ret = ivpu_ipc_init(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to initialize IPC: %d\n", ret);
+		goto err_mmu_gctx_fini;
+	}
+
 	return 0;
 
 err_mmu_gctx_fini:
@@ -404,6 +416,7 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
 static void ivpu_dev_fini(struct ivpu_device *vdev)
 {
 	ivpu_shutdown(vdev);
+	ivpu_ipc_fini(vdev);
 	ivpu_mmu_global_context_fini(vdev);
 
 	drm_WARN_ON(&vdev->drm, !xa_empty(&vdev->context_xa));
drivers/accel/ivpu/ivpu_drv.h +2 -0
index e8a43dbe5a3a..c1e76d1fb8ba 100644
@@ -74,6 +74,7 @@ struct ivpu_wa_table {
 
 struct ivpu_hw_info;
 struct ivpu_mmu_info;
+struct ivpu_ipc_info;
 
 struct ivpu_device {
 	struct drm_device drm;
@@ -85,6 +86,7 @@ struct ivpu_device {
 	struct ivpu_wa_table wa;
 	struct ivpu_hw_info *hw;
 	struct ivpu_mmu_info *mmu;
+	struct ivpu_ipc_info *ipc;
 
 	struct ivpu_mmu_context gctx;
 	struct xarray context_xa;
drivers/accel/ivpu/ivpu_hw_mtl.c +4 -0
index eaba2b8248b8..0e9ef4c40901 100644
@@ -7,6 +7,7 @@
 #include "ivpu_hw_mtl_reg.h"
 #include "ivpu_hw_reg_io.h"
 #include "ivpu_hw.h"
+#include "ivpu_ipc.h"
 #include "ivpu_mmu.h"
 
 #define TILE_FUSE_ENABLE_BOTH	     0x0
@@ -934,6 +935,9 @@ static u32 ivpu_hw_mtl_irqv_handler(struct ivpu_device *vdev, int irq)
 	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_0_INT, status))
 		ivpu_mmu_irq_evtq_handler(vdev);
 
+	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, HOST_IPC_FIFO_INT, status))
+		ivpu_ipc_irq_handler(vdev);
+
 	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_1_INT, status))
 		ivpu_dbg(vdev, IRQ, "MMU sync complete\n");
 
drivers/accel/ivpu/ivpu_ipc.c new file +505 -0

505 lines not on this page · raw message

drivers/accel/ivpu/ivpu_ipc.h new file +93 -0

93 lines not on this page · raw message

drivers/accel/ivpu/ivpu_jsm_msg.c new file +170 -0

170 lines not on this page · raw message

drivers/accel/ivpu/ivpu_jsm_msg.h new file +23 -0
new file mode 100644
index 000000000000..1a3e2e2740bd
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2020-2023 Intel Corporation
+ */
+
+#ifndef __IVPU_JSM_MSG_H__
+#define __IVPU_JSM_MSG_H__
+
+#include "vpu_jsm_api.h"
+
+int ivpu_jsm_register_db(struct ivpu_device *vdev, u32 ctx_id, u32 db_id,
+			 u64 jobq_base, u32 jobq_size);
+int ivpu_jsm_unregister_db(struct ivpu_device *vdev, u32 db_id);
+int ivpu_jsm_get_heartbeat(struct ivpu_device *vdev, u32 engine, u64 *heartbeat);
+int ivpu_jsm_reset_engine(struct ivpu_device *vdev, u32 engine);
+int ivpu_jsm_preempt_engine(struct ivpu_device *vdev, u32 engine, u32 preempt_id);
+int ivpu_jsm_dyndbg_control(struct ivpu_device *vdev, char *command, size_t size);
+int ivpu_jsm_trace_get_capability(struct ivpu_device *vdev, u32 *trace_destination_mask,
+				  u64 *trace_hw_component_mask);
+int ivpu_jsm_trace_set_config(struct ivpu_device *vdev, u32 trace_level, u32 trace_destination_mask,
+			      u64 trace_hw_component_mask);
+
+#endif
drivers/accel/ivpu/vpu_jsm_api.h new file +999 -0

999 lines not on this page · raw message

Signature · 1 line
2.34.1
Reviewed-by on v5 4/7
#13 lore raw

Re: [PATCH v5 4/7] accel/ivpu: Add IPC driver and JSM messages

Quoted 11 lines · 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.

Quoted 4 lines
drivers/accel/ivpu/ivpu_ipc.c new file +4 -0
+int ivpu_ipc_irq_handler(struct ivpu_device *vdev)
+{
+	struct ivpu_ipc_info *ipc = vdev->ipc;
+	struct ivpu_ipc_consumer *cons;

drivers/accel/ivpu/ivpu_ipc.c line 372

Quoted 10 lines
drivers/accel/ivpu/ivpu_ipc.c new file +10 -0
+	struct ivpu_ipc_hdr *ipc_hdr;
+	struct vpu_jsm_msg *jsm_msg;
+	unsigned long flags;
+	bool dispatched;
+	u32 vpu_addr;
+
+	/* Driver needs to purge all messages from IPC FIFO to clear IPC interrupt.
+	 * Without purge IPC FIFO to 0 next IPC interrupts won't be generated.
+	 */
+	while (ivpu_hw_reg_ipc_rx_count_get(vdev)) {

Ick. Please no in the long term?

This is an infinite loop. In hard IRQ context. Controlled by the device, which you probably shouldn't trust.

However the real fix for this is to move to threaded_irqs. Which is going to be a huge refactor for you. Rate limiting doesn't appear viable.

If I understand things correctly, the chances that the device will generate a large count, or update the count as fast or faster than the driver are low, but it should still be fixed.

How about a high priority todo to convert to threaded irqs? At the same time you can update the return value for this function which seems to not be checked anywhere, and also the comment here which is not proper multi-line style.

#14 lore raw

Re: [PATCH v5 4/7] accel/ivpu: Add IPC driver and JSM messages

Hi,

Quoted 32 lines · Jeffrey Hugo
Quoted 11 lines · 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.

Quoted 4 lines
drivers/accel/ivpu/ivpu_ipc.c new file +4 -0
+int ivpu_ipc_irq_handler(struct ivpu_device *vdev)
+{
+    struct ivpu_ipc_info *ipc = vdev->ipc;
+    struct ivpu_ipc_consumer *cons;

drivers/accel/ivpu/ivpu_ipc.c line 372

Quoted 10 lines
drivers/accel/ivpu/ivpu_ipc.c new file +10 -0
+    struct ivpu_ipc_hdr *ipc_hdr;
+    struct vpu_jsm_msg *jsm_msg;
+    unsigned long flags;
+    bool dispatched;
+    u32 vpu_addr;
+
+    /* Driver needs to purge all messages from IPC FIFO to clear IPC interrupt.
+     * Without purge IPC FIFO to 0 next IPC interrupts won't be generated.
+     */
+    while (ivpu_hw_reg_ipc_rx_count_get(vdev)) {

Ick.  Please no in the long term?

Quoted 7 lines

This is an infinite loop.  In hard IRQ context.  Controlled by the device, which you probably shouldn't trust.

However the real fix for this is to move to threaded_irqs.  Which is going to be a huge refactor for you.  Rate limiting doesn't appear viable.

If I understand things correctly, the chances that the device will generate a large count, or update the count as fast or faster than the driver are low, but it should still be fixed.

How about a high priority todo to convert to threaded irqs?  At the same time you can update the return value for this function which seems to not be checked anywhere, and also the comment here which is not proper multi-line style.

OK, I've added this at the top of the TODO and fixed the comment.
Regarding the ivpu_ipc_irq_handler() return code it is checked in the next patch in ivpu_wait_for_ready().

Regards,
Jacek

#15 lore raw

PATCH v5 5/7 accel/ivpu: Implement firmware parsing and booting

In reply to Jacek Lawrynowicz

Read, parse and boot VPU firmware image.

---
 drivers/accel/ivpu/Makefile       |   1 +
 drivers/accel/ivpu/ivpu_drv.c     | 131 +++++++++-
 drivers/accel/ivpu/ivpu_drv.h     |  11 +
 drivers/accel/ivpu/ivpu_fw.c      | 419 ++++++++++++++++++++++++++++++
 drivers/accel/ivpu/ivpu_fw.h      |  38 +++
 drivers/accel/ivpu/ivpu_hw_mtl.c  |  10 +
 drivers/accel/ivpu/vpu_boot_api.h | 349 +++++++++++++++++++++++++
 include/uapi/drm/ivpu_accel.h     |  21 ++
 8 files changed, 979 insertions(+), 1 deletion(-)
 create mode 100644 drivers/accel/ivpu/ivpu_fw.c
 create mode 100644 drivers/accel/ivpu/ivpu_fw.h
 create mode 100644 drivers/accel/ivpu/vpu_boot_api.h
drivers/accel/ivpu/Makefile +1 -0
index 46595f0112e3..9fa6a76e9d79 100644
@@ -3,6 +3,7 @@
 
 intel_vpu-y := \
 	ivpu_drv.o \
+	ivpu_fw.o \
 	ivpu_gem.o \
 	ivpu_hw_mtl.o \
 	ivpu_ipc.o \
drivers/accel/ivpu/ivpu_drv.c +130 -1

195 lines not on this page · raw message

drivers/accel/ivpu/ivpu_drv.h +11 -0
index c1e76d1fb8ba..317ae61f43bd 100644
@@ -74,6 +74,7 @@ struct ivpu_wa_table {
 
 struct ivpu_hw_info;
 struct ivpu_mmu_info;
+struct ivpu_fw_info;
 struct ivpu_ipc_info;
 
 struct ivpu_device {
@@ -86,12 +87,15 @@ struct ivpu_device {
 	struct ivpu_wa_table wa;
 	struct ivpu_hw_info *hw;
 	struct ivpu_mmu_info *mmu;
+	struct ivpu_fw_info *fw;
 	struct ivpu_ipc_info *ipc;
 
 	struct ivpu_mmu_context gctx;
 	struct xarray context_xa;
 	struct xa_limit context_xa_limit;
 
+	atomic64_t unique_id_counter;
+
 	struct {
 		int boot;
 		int jsm;
@@ -116,9 +120,16 @@ extern int ivpu_dbg_mask;
 extern u8 ivpu_pll_min_ratio;
 extern u8 ivpu_pll_max_ratio;
 
+#define IVPU_TEST_MODE_DISABLED  0
+#define IVPU_TEST_MODE_FW_TEST   1
+#define IVPU_TEST_MODE_NULL_HW   2
+extern int ivpu_test_mode;
+
 struct ivpu_file_priv *ivpu_file_priv_get(struct ivpu_file_priv *file_priv);
 struct ivpu_file_priv *ivpu_file_priv_get_by_ctx_id(struct ivpu_device *vdev, unsigned long id);
 void ivpu_file_priv_put(struct ivpu_file_priv **link);
+
+int ivpu_boot(struct ivpu_device *vdev);
 int ivpu_shutdown(struct ivpu_device *vdev);
 
 static inline bool ivpu_is_mtl(struct ivpu_device *vdev)
drivers/accel/ivpu/ivpu_fw.c new file +419 -0

419 lines not on this page · raw message

drivers/accel/ivpu/ivpu_fw.h new file +38 -0
new file mode 100644
index 000000000000..8d275c802d1c
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2020-2023 Intel Corporation
+ */
+
+#ifndef __IVPU_FW_H__
+#define __IVPU_FW_H__
+
+struct ivpu_device;
+struct ivpu_bo;
+struct vpu_boot_params;
+
+struct ivpu_fw_info {
+	const struct firmware *file;
+	struct ivpu_bo *mem;
+	struct ivpu_bo *mem_shave_nn;
+	struct ivpu_bo *mem_log_crit;
+	struct ivpu_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 ivpu_fw_init(struct ivpu_device *vdev);
+void ivpu_fw_fini(struct ivpu_device *vdev);
+int ivpu_fw_load(struct ivpu_device *vdev);
+void ivpu_fw_boot_params_setup(struct ivpu_device *vdev, struct vpu_boot_params *bp);
+
+static inline bool ivpu_fw_is_cold_boot(struct ivpu_device *vdev)
+{
+	return vdev->fw->entry_point == vdev->fw->cold_boot_entry_point;
+}
+
+#endif /* __IVPU_FW_H__ */
drivers/accel/ivpu/ivpu_hw_mtl.c +10 -0
index 0e9ef4c40901..55caf5479f5f 100644
@@ -4,6 +4,7 @@
  */
 
 #include "ivpu_drv.h"
+#include "ivpu_fw.h"
 #include "ivpu_hw_mtl_reg.h"
 #include "ivpu_hw_reg_io.h"
 #include "ivpu_hw.h"
@@ -588,6 +589,15 @@ static void ivpu_boot_soc_cpu_boot(struct ivpu_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);
+
+	ivpu_dbg(vdev, PM, "Booting firmware, mode: %s\n",
+		 vdev->fw->entry_point == vdev->fw->cold_boot_entry_point ? "cold boot" : "resume");
 }
 
 static int ivpu_boot_d0i3_drive(struct ivpu_device *vdev, bool enable)
drivers/accel/ivpu/vpu_boot_api.h new file +349 -0

349 lines not on this page · raw message

include/uapi/drm/ivpu_accel.h +21 -0
index 5f953ebafc9b..005b40b7326c 100644
@@ -51,6 +51,11 @@ extern "C" {
 #define DRM_IVPU_PARAM_CONTEXT_BASE_ADDRESS 5
 #define DRM_IVPU_PARAM_CONTEXT_PRIORITY	    6
 #define DRM_IVPU_PARAM_CONTEXT_ID	    7
+#define DRM_IVPU_PARAM_FW_API_VERSION	    8
+#define DRM_IVPU_PARAM_ENGINE_HEARTBEAT	    9
+#define DRM_IVPU_PARAM_UNIQUE_INFERENCE_ID  10
+#define DRM_IVPU_PARAM_TILE_CONFIG	    11
+#define DRM_IVPU_PARAM_SKU		    12
 
 #define DRM_IVPU_PLATFORM_TYPE_SILICON	    0
 
@@ -94,6 +99,22 @@ struct drm_ivpu_param {
 	 * %DRM_IVPU_PARAM_CONTEXT_ID:
 	 * Current context ID, always greater than 0 (read-only)
 	 *
+	 * %DRM_IVPU_PARAM_FW_API_VERSION:
+	 * Firmware API version array (read-only)
+	 *
+	 * %DRM_IVPU_PARAM_ENGINE_HEARTBEAT:
+	 * Heartbeat value from an engine (read-only).
+	 * Engine ID (i.e. DRM_IVPU_ENGINE_COMPUTE) is given via index.
+	 *
+	 * %DRM_IVPU_PARAM_UNIQUE_INFERENCE_ID:
+	 * Device-unique inference ID (read-only)
+	 *
+	 * %DRM_IVPU_PARAM_TILE_CONFIG:
+	 * VPU tile configuration  (read-only)
+	 *
+	 * %DRM_IVPU_PARAM_SKU:
+	 * VPU SKU ID (read-only)
+	 *
 	 */
 	__u32 param;
 
Signature · 1 line
2.34.1
#16 lore raw

Re: [PATCH v5 5/7] accel/ivpu: Implement firmware parsing and booting

Quoted 519 lines · Jacek Lawrynowicz

Read, parse and boot VPU firmware image.

---
 drivers/accel/ivpu/Makefile       |   1 +
 drivers/accel/ivpu/ivpu_drv.c     | 131 +++++++++-
 drivers/accel/ivpu/ivpu_drv.h     |  11 +
 drivers/accel/ivpu/ivpu_fw.c      | 419 ++++++++++++++++++++++++++++++
 drivers/accel/ivpu/ivpu_fw.h      |  38 +++
 drivers/accel/ivpu/ivpu_hw_mtl.c  |  10 +
 drivers/accel/ivpu/vpu_boot_api.h | 349 +++++++++++++++++++++++++
 include/uapi/drm/ivpu_accel.h     |  21 ++
 8 files changed, 979 insertions(+), 1 deletion(-)
 create mode 100644 drivers/accel/ivpu/ivpu_fw.c
 create mode 100644 drivers/accel/ivpu/ivpu_fw.h
 create mode 100644 drivers/accel/ivpu/vpu_boot_api.h
drivers/accel/ivpu/Makefile +1 -0
index 46595f0112e3..9fa6a76e9d79 100644
@@ -3,6 +3,7 @@
 
 intel_vpu-y := \
        ivpu_drv.o \
+       ivpu_fw.o \
        ivpu_gem.o \
        ivpu_hw_mtl.o \
        ivpu_ipc.o \
drivers/accel/ivpu/ivpu_drv.c +130 -1
index 6643ae6b5a52..53e103f64832 100644
@@ -14,10 +14,13 @@
 #include <drm/drm_ioctl.h>
 #include <drm/drm_prime.h>
 
+#include "vpu_boot_api.h"
 #include "ivpu_drv.h"
+#include "ivpu_fw.h"
 #include "ivpu_gem.h"
 #include "ivpu_hw.h"
 #include "ivpu_ipc.h"
+#include "ivpu_jsm_msg.h"
 #include "ivpu_mmu.h"
 #include "ivpu_mmu_context.h"
 
@@ -32,6 +35,10 @@ int ivpu_dbg_mask;
 module_param_named(dbg_mask, ivpu_dbg_mask, int, 0644);
 MODULE_PARM_DESC(dbg_mask, "Driver debug mask. See IVPU_DBG_* macros.");
 
+int ivpu_test_mode;
+module_param_named_unsafe(test_mode, ivpu_test_mode, int, 0644);
+MODULE_PARM_DESC(test_mode, "Test mode: 0 - normal operation, 1 - fw unit test, 2 - null hw");
+
 u8 ivpu_pll_min_ratio;
 module_param_named(pll_min_ratio, ivpu_pll_min_ratio, byte, 0644);
 MODULE_PARM_DESC(pll_min_ratio, "Minimum PLL ratio used to set VPU frequency");
@@ -129,6 +136,28 @@ static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_f
        case DRM_IVPU_PARAM_CONTEXT_ID:
                args->value = file_priv->ctx.id;
                break;
+       case DRM_IVPU_PARAM_FW_API_VERSION:
+               if (args->index < VPU_FW_API_VER_NUM) {
+                       struct vpu_firmware_header *fw_hdr;
+
+                       fw_hdr = (struct vpu_firmware_header *)vdev->fw->file->data;
+                       args->value = fw_hdr->api_version[args->index];
+               } else {
+                       ret = -EINVAL;
+               }
+               break;
+       case DRM_IVPU_PARAM_ENGINE_HEARTBEAT:
+               ret = ivpu_jsm_get_heartbeat(vdev, args->index, &args->value);
+               break;
+       case DRM_IVPU_PARAM_UNIQUE_INFERENCE_ID:
+               args->value = (u64)atomic64_inc_return(&vdev->unique_id_counter);
+               break;
+       case DRM_IVPU_PARAM_TILE_CONFIG:
+               args->value = vdev->hw->tile_fuse;
+               break;
+       case DRM_IVPU_PARAM_SKU:
+               args->value = vdev->hw->sku;
+               break;
        default:
                ret = -EINVAL;
                break;
@@ -226,11 +255,85 @@ static const struct drm_ioctl_desc ivpu_drm_ioctls[] = {
        DRM_IOCTL_DEF_DRV(IVPU_BO_USERPTR, ivpu_bo_userptr_ioctl, 0),
 };
 
+static int ivpu_wait_for_ready(struct ivpu_device *vdev)
+{
+       struct ivpu_ipc_consumer cons;
+       struct ivpu_ipc_hdr ipc_hdr;
+       unsigned long timeout;
+       int ret;
+
+       if (ivpu_test_mode == IVPU_TEST_MODE_FW_TEST)
+               return 0;
+
+       ivpu_ipc_consumer_add(vdev, &cons, IVPU_IPC_CHAN_BOOT_MSG);
+
+       timeout = jiffies + msecs_to_jiffies(vdev->timeout.boot);
+       while (1) {
+               ret = ivpu_ipc_irq_handler(vdev);
+               if (ret)
+                       break;
+               ret = ivpu_ipc_receive(vdev, &cons, &ipc_hdr, NULL, 0);
+               if (ret != -ETIMEDOUT || time_after_eq(jiffies, timeout))
+                       break;
+
+               cond_resched();
+       }
+
+       ivpu_ipc_consumer_del(vdev, &cons);
+
+       if (!ret && ipc_hdr.data_addr != IVPU_IPC_BOOT_MSG_DATA_ADDR) {
+               ivpu_err(vdev, "Invalid VPU ready message: 0x%x\n",
+                        ipc_hdr.data_addr);
+               return -EIO;
+       }
+
+       if (!ret)
+               ivpu_info(vdev, "VPU ready message received successfully\n");
+       else
+               ivpu_hw_diagnose_failure(vdev);
+
+       return ret;
+}
+
+/**
+ * ivpu_boot() - Start VPU firmware
+ * @vdev: VPU device
+ *
+ * This function is paired with ivpu_shutdown() but it doesn't power up the
+ * VPU because power up has to be called very early in ivpu_probe().
+ */
+int ivpu_boot(struct ivpu_device *vdev)
+{
+       int ret;
+
+       /* Update boot params located at first 4KB of FW memory */
+       ivpu_fw_boot_params_setup(vdev, vdev->fw->mem->kvaddr);
+
+       ret = ivpu_hw_boot_fw(vdev);
+       if (ret) {
+               ivpu_err(vdev, "Failed to start the firmware: %d\n", ret);
+               return ret;
+       }
+
+       ret = ivpu_wait_for_ready(vdev);
+       if (ret) {
+               ivpu_err(vdev, "Failed to boot the firmware: %d\n", ret);
+               return ret;
+       }
+
+       ivpu_hw_irq_clear(vdev);
+       enable_irq(vdev->irq);
+       ivpu_hw_irq_enable(vdev);
+       ivpu_ipc_enable(vdev);
+       return 0;
+}
+
 int ivpu_shutdown(struct ivpu_device *vdev)
 {
        int ret;
 
        ivpu_hw_irq_disable(vdev);
+       disable_irq(vdev->irq);
        ivpu_ipc_disable(vdev);
        ivpu_mmu_disable(vdev);
 
@@ -348,6 +451,10 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
        if (!vdev->mmu)
                return -ENOMEM;
 
+       vdev->fw = drmm_kzalloc(&vdev->drm, sizeof(*vdev->fw), GFP_KERNEL);
+       if (!vdev->fw)
+               return -ENOMEM;
+
        vdev->ipc = drmm_kzalloc(&vdev->drm, sizeof(*vdev->ipc), GFP_KERNEL);
        if (!vdev->ipc)
                return -ENOMEM;
@@ -356,6 +463,7 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
        vdev->platform = IVPU_PLATFORM_INVALID;
        vdev->context_xa_limit.min = IVPU_GLOBAL_CONTEXT_MMU_SSID + 1;
        vdev->context_xa_limit.max = IVPU_CONTEXT_LIMIT;
+       atomic64_set(&vdev->unique_id_counter, 0);
        xa_init_flags(&vdev->context_xa, XA_FLAGS_ALLOC);
 
        ret = ivpu_pci_init(vdev);
@@ -396,14 +504,34 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
                goto err_mmu_gctx_fini;
        }
 
+       ret = ivpu_fw_init(vdev);
+       if (ret) {
+               ivpu_err(vdev, "Failed to initialize firmware: %d\n", ret);
+               goto err_mmu_gctx_fini;
+       }
+
        ret = ivpu_ipc_init(vdev);
        if (ret) {
                ivpu_err(vdev, "Failed to initialize IPC: %d\n", ret);
-               goto err_mmu_gctx_fini;
+               goto err_fw_fini;
+       }
+
+       ret = ivpu_fw_load(vdev);
+       if (ret) {
+               ivpu_err(vdev, "Failed to load firmware: %d\n", ret);
+               goto err_fw_fini;
+       }
+
+       ret = ivpu_boot(vdev);
+       if (ret) {
+               ivpu_err(vdev, "Failed to boot: %d\n", ret);
+               goto err_fw_fini;
        }
 
        return 0;
 
+err_fw_fini:
+       ivpu_fw_fini(vdev);
 err_mmu_gctx_fini:
        ivpu_mmu_global_context_fini(vdev);
 err_power_down:
@@ -417,6 +545,7 @@ static void ivpu_dev_fini(struct ivpu_device *vdev)
 {
        ivpu_shutdown(vdev);
        ivpu_ipc_fini(vdev);
+       ivpu_fw_fini(vdev);
        ivpu_mmu_global_context_fini(vdev);
 
        drm_WARN_ON(&vdev->drm, !xa_empty(&vdev->context_xa));
drivers/accel/ivpu/ivpu_drv.h +11 -0
index c1e76d1fb8ba..317ae61f43bd 100644
@@ -74,6 +74,7 @@ struct ivpu_wa_table {
 
 struct ivpu_hw_info;
 struct ivpu_mmu_info;
+struct ivpu_fw_info;
 struct ivpu_ipc_info;
 
 struct ivpu_device {
@@ -86,12 +87,15 @@ struct ivpu_device {
        struct ivpu_wa_table wa;
        struct ivpu_hw_info *hw;
        struct ivpu_mmu_info *mmu;
+       struct ivpu_fw_info *fw;
        struct ivpu_ipc_info *ipc;
 
        struct ivpu_mmu_context gctx;
        struct xarray context_xa;
        struct xa_limit context_xa_limit;
 
+       atomic64_t unique_id_counter;
+
        struct {
                int boot;
                int jsm;
@@ -116,9 +120,16 @@ extern int ivpu_dbg_mask;
 extern u8 ivpu_pll_min_ratio;
 extern u8 ivpu_pll_max_ratio;
 
+#define IVPU_TEST_MODE_DISABLED  0
+#define IVPU_TEST_MODE_FW_TEST   1
+#define IVPU_TEST_MODE_NULL_HW   2
+extern int ivpu_test_mode;
+
 struct ivpu_file_priv *ivpu_file_priv_get(struct ivpu_file_priv *file_priv);
 struct ivpu_file_priv *ivpu_file_priv_get_by_ctx_id(struct ivpu_device *vdev, unsigned long id);
 void ivpu_file_priv_put(struct ivpu_file_priv **link);
+
+int ivpu_boot(struct ivpu_device *vdev);
 int ivpu_shutdown(struct ivpu_device *vdev);
 
 static inline bool ivpu_is_mtl(struct ivpu_device *vdev)
drivers/accel/ivpu/ivpu_fw.c new file +227 -0
new file mode 100644
index 000000000000..4baa0767a10d
@@ -0,0 +1,419 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2020-2023 Intel Corporation
+ */
+
+#include <linux/firmware.h>
+#include <linux/highmem.h>
+#include <linux/moduleparam.h>
+#include <linux/pci.h>
+
+#include "vpu_boot_api.h"
+#include "ivpu_drv.h"
+#include "ivpu_fw.h"
+#include "ivpu_gem.h"
+#include "ivpu_hw.h"
+#include "ivpu_ipc.h"
+
+#define FW_GLOBAL_MEM_START    (2ull * SZ_1G)
+#define FW_GLOBAL_MEM_END      (3ull * SZ_1G)
+#define FW_SHARED_MEM_SIZE     SZ_256M /* Must be aligned to FW_SHARED_MEM_ALIGNMENT */
+#define FW_SHARED_MEM_ALIGNMENT        SZ_128K /* VPU MTRR limitation */
+#define FW_RUNTIME_MAX_SIZE    SZ_512M
+#define FW_SHAVE_NN_MAX_SIZE   SZ_2M
+#define FW_RUNTIME_MIN_ADDR    (FW_GLOBAL_MEM_START)
+#define FW_RUNTIME_MAX_ADDR    (FW_GLOBAL_MEM_END - FW_SHARED_MEM_SIZE)
+#define FW_VERSION_HEADER_SIZE SZ_4K
+#define FW_FILE_IMAGE_OFFSET   (VPU_FW_HEADER_SIZE + FW_VERSION_HEADER_SIZE)
+
+#define WATCHDOG_MSS_REDIRECT  32
+#define WATCHDOG_NCE_REDIRECT  33
+
+#define ADDR_TO_L2_CACHE_CFG(addr) ((addr) >> 31)
+
+#define IVPU_FW_CHECK_API(vdev, fw_hdr, name) ivpu_fw_check_api(vdev, fw_hdr, #name, \
+                                                                 VPU_##name##_API_VER_INDEX, \
+                                                                 VPU_##name##_API_VER_MAJOR, \
+                                                                 VPU_##name##_API_VER_MINOR)
+
+static char *ivpu_firmware;
+module_param_named_unsafe(firmware, ivpu_firmware, charp, 0644);
+MODULE_PARM_DESC(firmware, "VPU firmware binary in /lib/firmware/..");
+
+static int ivpu_fw_request(struct ivpu_device *vdev)
+{
+       static const char * const fw_names[] = {
+               "mtl_vpu.bin",
+               "intel/vpu/mtl_vpu_v0.0.bin"
+       };
+       int ret = -ENOENT;
+       int i;
+
+       if (ivpu_firmware)
+               return request_firmware(&vdev->fw->file, ivpu_firmware, vdev->drm.dev);
+
+       for (i = 0; i < ARRAY_SIZE(fw_names); i++) {
+               ret = firmware_request_nowarn(&vdev->fw->file, fw_names[i], vdev->drm.dev);
+               if (!ret)
+                       return 0;
+       }
+
+       ivpu_err(vdev, "Failed to request firmware: %d\n", ret);
+       return ret;
+}
+
+static void
+ivpu_fw_check_api(struct ivpu_device *vdev, const struct vpu_firmware_header *fw_hdr,
+                 const char *str, int index, u16 expected_major, u16 expected_minor)
+{
+       u16 major = (u16)(fw_hdr->api_version[index] >> 16);
+       u16 minor = (u16)(fw_hdr->api_version[index]);
+
+       if (major != expected_major) {
+               ivpu_warn(vdev, "Incompatible FW %s API version: %d.%d (expected %d.%d)\n",
+                         str, major, minor, expected_major, expected_minor);
+       }
+       ivpu_dbg(vdev, FW_BOOT, "FW %s API version: %d.%d (expected %d.%d)\n",
+                str, major, minor, expected_major, expected_minor);
+}
+
+static int ivpu_fw_parse(struct ivpu_device *vdev)
+{
+       struct ivpu_fw_info *fw = vdev->fw;
+       const struct vpu_firmware_header *fw_hdr = (const void *)fw->file->data;
+       u64 runtime_addr, image_load_addr, runtime_size, image_size;
+
+       if (fw->file->size <= FW_FILE_IMAGE_OFFSET) {
+               ivpu_err(vdev, "Firmware file is too small: %zu\n", fw->file->size);
+               return -EINVAL;
+       }
+
+       if (fw_hdr->header_version != VPU_FW_HEADER_VERSION) {
+               ivpu_err(vdev, "Invalid firmware header version: %u\n", fw_hdr->header_version);
+               return -EINVAL;
+       }
+
+       runtime_addr = fw_hdr->boot_params_load_address;
+       runtime_size = fw_hdr->runtime_size;
+       image_load_addr = fw_hdr->image_load_address;
+       image_size = fw_hdr->image_size;
+
+       if (runtime_addr < FW_RUNTIME_MIN_ADDR || runtime_addr > FW_RUNTIME_MAX_ADDR) {
+               ivpu_err(vdev, "Invalid firmware runtime address: 0x%llx\n", runtime_addr);
+               return -EINVAL;
+       }
+
+       if (runtime_size < fw->file->size || runtime_size > FW_RUNTIME_MAX_SIZE) {
+               ivpu_err(vdev, "Invalid firmware runtime size: %llu\n", runtime_size);
+               return -EINVAL;
+       }
+
+       if (FW_FILE_IMAGE_OFFSET + image_size > fw->file->size) {
+               ivpu_err(vdev, "Invalid image size: %llu\n", image_size);
+               return -EINVAL;
+       }
+
+       if (image_load_addr < runtime_addr ||
+           image_load_addr + image_size > runtime_addr + runtime_size) {
+               ivpu_err(vdev, "Invalid firmware load address size: 0x%llx and size %llu\n",
+                        image_load_addr, image_size);
+               return -EINVAL;
+       }
+
+       if (fw_hdr->shave_nn_fw_size > FW_SHAVE_NN_MAX_SIZE) {
+               ivpu_err(vdev, "SHAVE NN firmware is too big: %u\n", fw_hdr->shave_nn_fw_size);
+               return -EINVAL;
+       }
+
+       if (fw_hdr->entry_point < image_load_addr ||
+           fw_hdr->entry_point >= image_load_addr + image_size) {
+               ivpu_err(vdev, "Invalid entry point: 0x%llx\n", fw_hdr->entry_point);
+               return -EINVAL;
+       }
+
+       fw->runtime_addr = runtime_addr;
+       fw->runtime_size = runtime_size;
+       fw->image_load_offset = image_load_addr - runtime_addr;
+       fw->image_size = image_size;
+       fw->shave_nn_size = PAGE_ALIGN(fw_hdr->shave_nn_fw_size);
+
+       fw->cold_boot_entry_point = fw_hdr->entry_point;
+       fw->entry_point = fw->cold_boot_entry_point;
+
+       ivpu_dbg(vdev, FW_BOOT, "Header version: 0x%x, format 0x%x\n",
+                fw_hdr->header_version, fw_hdr->image_format);
+       ivpu_dbg(vdev, FW_BOOT, "Size: file %lu image %u runtime %u shavenn %u\n",
+                fw->file->size, fw->image_size, fw->runtime_size, fw->shave_nn_size);
+       ivpu_dbg(vdev, FW_BOOT, "Address: runtime 0x%llx, load 0x%llx, entry point 0x%llx\n",
+                fw->runtime_addr, image_load_addr, fw->entry_point);
+       ivpu_dbg(vdev, FW_BOOT, "FW version: %s\n", (char *)fw_hdr + VPU_FW_HEADER_SIZE);
+
+       IVPU_FW_CHECK_API(vdev, fw_hdr, BOOT);
+       IVPU_FW_CHECK_API(vdev, fw_hdr, JSM);
+
+       return 0;
+}
+
+static void ivpu_fw_release(struct ivpu_device *vdev)
+{
+       release_firmware(vdev->fw->file);
+}
+
+static int ivpu_fw_update_global_range(struct ivpu_device *vdev)
+{
+       struct ivpu_fw_info *fw = vdev->fw;
+       u64 start = ALIGN(fw->runtime_addr + fw->runtime_size, FW_SHARED_MEM_ALIGNMENT);
+       u64 size = FW_SHARED_MEM_SIZE;
+
+       if (start + size > FW_GLOBAL_MEM_END) {
+               ivpu_err(vdev, "No space for shared region, start %lld, size %lld\n", start, size);
+               return -EINVAL;
+       }
+
+       ivpu_hw_init_range(&vdev->hw->ranges.global_low, start, size);
+       return 0;
+}
+
+static int ivpu_fw_mem_init(struct ivpu_device *vdev)
+{
+       struct ivpu_fw_info *fw = vdev->fw;
+       int ret;
+
+       ret = ivpu_fw_update_global_range(vdev);
+       if (ret)
+               return ret;
+
+       fw->mem = ivpu_bo_alloc_internal(vdev, fw->runtime_addr, fw->runtime_size, DRM_IVPU_BO_WC);
+       if (!fw->mem) {
+               ivpu_err(vdev, "Failed to allocate firmware runtime memory\n");
+               return -ENOMEM;
+       }
+
+       if (fw->shave_nn_size) {
+               fw->mem_shave_nn = ivpu_bo_alloc_internal(vdev, vdev->hw->ranges.global_high.start,
+                                                         fw->shave_nn_size, DRM_IVPU_BO_UNCACHED);
+               if (!fw->mem_shave_nn) {
+                       ivpu_err(vdev, "Failed to allocate shavenn buffer\n");
+                       ivpu_bo_free_internal(fw->mem);
+                       return -ENOMEM;
+               }
+       }
+
+       return 0;
+}
+
+static void ivpu_fw_mem_fini(struct ivpu_device *vdev)
+{
+       struct ivpu_fw_info *fw = vdev->fw;
+
+       if (fw->mem_shave_nn) {
+               ivpu_bo_free_internal(fw->mem_shave_nn);
+               fw->mem_shave_nn = NULL;
+       }
+
+       ivpu_bo_free_internal(fw->mem);
+       fw->mem = NULL;
+}
+
+int ivpu_fw_init(struct ivpu_device *vdev)
+{
+       int ret;
+
+       ret = ivpu_fw_request(vdev);
+       if (ret)
+               return ret;
+
+       ret = ivpu_fw_parse(vdev);
+       if (ret)

drivers/accel/ivpu/ivpu_fw.c line 237

Quoted 10 lines
drivers/accel/ivpu/ivpu_fw.c new file +10 -0
+               goto err_fw_release;
+
+       ret = ivpu_fw_mem_init(vdev);
+       if (ret)
+               goto err_fw_release;
+
+       return 0;
+
+err_fw_release:
+       ivpu_fw_release(vdev);

Why do you release the firmware only on error ?
Why maintain the hold on the firmware file for as long as the device lives ? Can't you release the file once you loaded the firmware to the device's memory ?

Oded

Quoted 651 lines
drivers/accel/ivpu/ivpu_fw.c new file +182 -0
+       return ret;
+}
+
+void ivpu_fw_fini(struct ivpu_device *vdev)
+{
+       ivpu_fw_mem_fini(vdev);
+       ivpu_fw_release(vdev);
+}
+
+int ivpu_fw_load(struct ivpu_device *vdev)
+{
+       struct ivpu_fw_info *fw = vdev->fw;
+       u64 image_end_offset = fw->image_load_offset + fw->image_size;
+
+       memset(fw->mem->kvaddr, 0, fw->image_load_offset);
+       memcpy(fw->mem->kvaddr + fw->image_load_offset,
+              fw->file->data + FW_FILE_IMAGE_OFFSET, fw->image_size);
+
+       if (IVPU_WA(clear_runtime_mem)) {
+               u8 *start = fw->mem->kvaddr + image_end_offset;
+               u64 size = fw->mem->base.size - image_end_offset;
+
+               memset(start, 0, size);
+       }
+
+       wmb(); /* Flush WC buffers after writing fw->mem */
+
+       return 0;
+}
+
+static void ivpu_fw_boot_params_print(struct ivpu_device *vdev, struct vpu_boot_params *boot_params)
+{
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.magic = 0x%x\n",
+                boot_params->magic);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.vpu_id = 0x%x\n",
+                boot_params->vpu_id);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.vpu_count = 0x%x\n",
+                boot_params->vpu_count);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.frequency = %u\n",
+                boot_params->frequency);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.perf_clk_frequency = %u\n",
+                boot_params->perf_clk_frequency);
+
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.ipc_header_area_start = 0x%llx\n",
+                boot_params->ipc_header_area_start);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.ipc_header_area_size = 0x%x\n",
+                boot_params->ipc_header_area_size);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.shared_region_base = 0x%llx\n",
+                boot_params->shared_region_base);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.shared_region_size = 0x%x\n",
+                boot_params->shared_region_size);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.ipc_payload_area_start = 0x%llx\n",
+                boot_params->ipc_payload_area_start);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.ipc_payload_area_size = 0x%x\n",
+                boot_params->ipc_payload_area_size);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.global_aliased_pio_base = 0x%llx\n",
+                boot_params->global_aliased_pio_base);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.global_aliased_pio_size = 0x%x\n",
+                boot_params->global_aliased_pio_size);
+
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.autoconfig = 0x%x\n",
+                boot_params->autoconfig);
+
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.cache_defaults[VPU_BOOT_L2_CACHE_CFG_NN].use = 0x%x\n",
+                boot_params->cache_defaults[VPU_BOOT_L2_CACHE_CFG_NN].use);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.cache_defaults[VPU_BOOT_L2_CACHE_CFG_NN].cfg = 0x%x\n",
+                boot_params->cache_defaults[VPU_BOOT_L2_CACHE_CFG_NN].cfg);
+
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.global_memory_allocator_base = 0x%llx\n",
+                boot_params->global_memory_allocator_base);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.global_memory_allocator_size = 0x%x\n",
+                boot_params->global_memory_allocator_size);
+
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.shave_nn_fw_base = 0x%llx\n",
+                boot_params->shave_nn_fw_base);
+
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.watchdog_irq_mss = 0x%x\n",
+                boot_params->watchdog_irq_mss);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.watchdog_irq_nce = 0x%x\n",
+                boot_params->watchdog_irq_nce);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.host_to_vpu_irq = 0x%x\n",
+                boot_params->host_to_vpu_irq);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.job_done_irq = 0x%x\n",
+                boot_params->job_done_irq);
+
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.host_version_id = 0x%x\n",
+                boot_params->host_version_id);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.si_stepping = 0x%x\n",
+                boot_params->si_stepping);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.device_id = 0x%llx\n",
+                boot_params->device_id);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.feature_exclusion = 0x%llx\n",
+                boot_params->feature_exclusion);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.sku = 0x%llx\n",
+                boot_params->sku);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.min_freq_pll_ratio = 0x%x\n",
+                boot_params->min_freq_pll_ratio);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.pn_freq_pll_ratio = 0x%x\n",
+                boot_params->pn_freq_pll_ratio);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.max_freq_pll_ratio = 0x%x\n",
+                boot_params->max_freq_pll_ratio);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.default_trace_level = 0x%x\n",
+                boot_params->default_trace_level);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.tracing_buff_message_format_mask = 0x%llx\n",
+                boot_params->tracing_buff_message_format_mask);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.trace_destination_mask = 0x%x\n",
+                boot_params->trace_destination_mask);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.trace_hw_component_mask = 0x%llx\n",
+                boot_params->trace_hw_component_mask);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.boot_type = 0x%x\n",
+                boot_params->boot_type);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.punit_telemetry_sram_base = 0x%llx\n",
+                boot_params->punit_telemetry_sram_base);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.punit_telemetry_sram_size = 0x%llx\n",
+                boot_params->punit_telemetry_sram_size);
+       ivpu_dbg(vdev, FW_BOOT, "boot_params.vpu_telemetry_enable = 0x%x\n",
+                boot_params->vpu_telemetry_enable);
+}
+
+void ivpu_fw_boot_params_setup(struct ivpu_device *vdev, struct vpu_boot_params *boot_params)
+{
+       struct ivpu_bo *ipc_mem_rx = vdev->ipc->mem_rx;
+
+       /* In case of warm boot we only have to reset the entrypoint addr */
+       if (!ivpu_fw_is_cold_boot(vdev)) {
+               boot_params->save_restore_ret_address = 0;
+               return;
+       }
+
+       boot_params->magic = VPU_BOOT_PARAMS_MAGIC;
+       boot_params->vpu_id = to_pci_dev(vdev->drm.dev)->bus->number;
+       boot_params->frequency = ivpu_hw_reg_pll_freq_get(vdev);
+
+       /*
+        * Uncached region of VPU address space, covers IPC buffers, job queues
+        * and log buffers, programmable to L2$ Uncached by VPU MTRR
+        */
+       boot_params->shared_region_base = vdev->hw->ranges.global_low.start;
+       boot_params->shared_region_size = vdev->hw->ranges.global_low.end -
+                                         vdev->hw->ranges.global_low.start;
+
+       boot_params->ipc_header_area_start = ipc_mem_rx->vpu_addr;
+       boot_params->ipc_header_area_size = ipc_mem_rx->base.size / 2;
+
+       boot_params->ipc_payload_area_start = ipc_mem_rx->vpu_addr + ipc_mem_rx->base.size / 2;
+       boot_params->ipc_payload_area_size = ipc_mem_rx->base.size / 2;
+
+       boot_params->global_aliased_pio_base =
+               vdev->hw->ranges.global_aliased_pio.start;
+       boot_params->global_aliased_pio_size =
+               ivpu_hw_range_size(&vdev->hw->ranges.global_aliased_pio);
+
+       /* Allow configuration for L2C_PAGE_TABLE with boot param value */
+       boot_params->autoconfig = 1;
+
+       /* Enable L2 cache for first 2GB of high memory */
+       boot_params->cache_defaults[VPU_BOOT_L2_CACHE_CFG_NN].use = 1;
+       boot_params->cache_defaults[VPU_BOOT_L2_CACHE_CFG_NN].cfg =
+               ADDR_TO_L2_CACHE_CFG(vdev->hw->ranges.global_high.start);
+
+       if (vdev->fw->mem_shave_nn)
+               boot_params->shave_nn_fw_base = vdev->fw->mem_shave_nn->vpu_addr;
+
+       boot_params->watchdog_irq_mss = WATCHDOG_MSS_REDIRECT;
+       boot_params->watchdog_irq_nce = WATCHDOG_NCE_REDIRECT;
+       boot_params->si_stepping = ivpu_revision(vdev);
+       boot_params->device_id = ivpu_device_id(vdev);
+       boot_params->feature_exclusion = vdev->hw->tile_fuse;
+       boot_params->sku = vdev->hw->sku;
+
+       boot_params->min_freq_pll_ratio = vdev->hw->pll.min_ratio;
+       boot_params->pn_freq_pll_ratio = vdev->hw->pll.pn_ratio;
+       boot_params->max_freq_pll_ratio = vdev->hw->pll.max_ratio;
+
+       boot_params->punit_telemetry_sram_base = ivpu_hw_reg_telemetry_offset_get(vdev);
+       boot_params->punit_telemetry_sram_size = ivpu_hw_reg_telemetry_size_get(vdev);
+       boot_params->vpu_telemetry_enable = ivpu_hw_reg_telemetry_enable_get(vdev);
+
+       wmb(); /* Flush WC buffers after writing bootparams */
+
+       ivpu_fw_boot_params_print(vdev, boot_params);
+}
drivers/accel/ivpu/ivpu_fw.h new file +38 -0
new file mode 100644
index 000000000000..8d275c802d1c
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2020-2023 Intel Corporation
+ */
+
+#ifndef __IVPU_FW_H__
+#define __IVPU_FW_H__
+
+struct ivpu_device;
+struct ivpu_bo;
+struct vpu_boot_params;
+
+struct ivpu_fw_info {
+       const struct firmware *file;
+       struct ivpu_bo *mem;
+       struct ivpu_bo *mem_shave_nn;
+       struct ivpu_bo *mem_log_crit;
+       struct ivpu_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 ivpu_fw_init(struct ivpu_device *vdev);
+void ivpu_fw_fini(struct ivpu_device *vdev);
+int ivpu_fw_load(struct ivpu_device *vdev);
+void ivpu_fw_boot_params_setup(struct ivpu_device *vdev, struct vpu_boot_params *bp);
+
+static inline bool ivpu_fw_is_cold_boot(struct ivpu_device *vdev)
+{
+       return vdev->fw->entry_point == vdev->fw->cold_boot_entry_point;
+}
+
+#endif /* __IVPU_FW_H__ */
drivers/accel/ivpu/ivpu_hw_mtl.c +10 -0
index 0e9ef4c40901..55caf5479f5f 100644
@@ -4,6 +4,7 @@
  */
 
 #include "ivpu_drv.h"
+#include "ivpu_fw.h"
 #include "ivpu_hw_mtl_reg.h"
 #include "ivpu_hw_reg_io.h"
 #include "ivpu_hw.h"
@@ -588,6 +589,15 @@ static void ivpu_boot_soc_cpu_boot(struct ivpu_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);
+
+       ivpu_dbg(vdev, PM, "Booting firmware, mode: %s\n",
+                vdev->fw->entry_point == vdev->fw->cold_boot_entry_point ? "cold boot" : "resume");
 }
 
 static int ivpu_boot_d0i3_drive(struct ivpu_device *vdev, bool enable)
drivers/accel/ivpu/vpu_boot_api.h new file +349 -0
new file mode 100644
index 000000000000..6b71be92ba65
@@ -0,0 +1,349 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright (C) 2020-2023 Intel Corporation
+ */
+
+#ifndef VPU_BOOT_API_H
+#define VPU_BOOT_API_H
+
+/*
+ * =========== FW API version information beginning ================
+ *  The bellow values will be used to construct the version info this way:
+ *  fw_bin_header->api_version[VPU_BOOT_API_VER_ID] = (VPU_BOOT_API_VER_MAJOR << 16) |
+ *  VPU_BOOT_API_VER_MINOR;
+ *  VPU_BOOT_API_VER_PATCH will be ignored. KMD and compatibility is not affected if this changes.
+ */
+
+/*
+ * Major version changes that break backward compatibility.
+ * Major version must start from 1 and can only be incremented.
+ */
+#define VPU_BOOT_API_VER_MAJOR 3
+
+/*
+ * Minor version changes when API backward compatibility is preserved.
+ * Resets to 0 if Major version is incremented.
+ */
+#define VPU_BOOT_API_VER_MINOR 12
+
+/*
+ * API header changed (field names, documentation, formatting) but API itself has not been changed
+ */
+#define VPU_BOOT_API_VER_PATCH 2
+
+/*
+ * Index in the API version table
+ * Must be unique for each API
+ */
+#define VPU_BOOT_API_VER_INDEX 0
+/* ------------ FW API version information end ---------------------*/
+
+#pragma pack(push, 1)
+
+/*
+ * Firmware image header format
+ */
+#define VPU_FW_HEADER_SIZE    4096
+#define VPU_FW_HEADER_VERSION 0x1
+#define VPU_FW_VERSION_SIZE   32
+#define VPU_FW_API_VER_NUM    16
+
+struct vpu_firmware_header {
+       u32 header_version;
+       u32 image_format;
+       u64 image_load_address;
+       u32 image_size;
+       u64 entry_point;
+       u8 vpu_version[VPU_FW_VERSION_SIZE];
+       u32 compression_type;
+       u64 firmware_version_load_address;
+       u32 firmware_version_size;
+       u64 boot_params_load_address;
+       u32 api_version[VPU_FW_API_VER_NUM];
+       /* Size of memory require for firmware execution */
+       u32 runtime_size;
+       u32 shave_nn_fw_size;
+};
+
+/*
+ * Firmware boot parameters format
+ */
+
+#define VPU_BOOT_PLL_COUNT     3
+#define VPU_BOOT_PLL_OUT_COUNT 4
+
+/** Values for boot_type field */
+#define VPU_BOOT_TYPE_COLDBOOT 0
+#define VPU_BOOT_TYPE_WARMBOOT 1
+
+/** Value for magic filed */
+#define VPU_BOOT_PARAMS_MAGIC 0x10000
+
+/** VPU scheduling mode. By default, OS scheduling is used. */
+#define VPU_SCHEDULING_MODE_OS 0
+#define VPU_SCHEDULING_MODE_HW 1
+
+enum VPU_BOOT_L2_CACHE_CFG_TYPE {
+       VPU_BOOT_L2_CACHE_CFG_UPA = 0,
+       VPU_BOOT_L2_CACHE_CFG_NN = 1,
+       VPU_BOOT_L2_CACHE_CFG_NUM = 2
+};
+
+/**
+ * Logging destinations.
+ *
+ * Logging output can be directed to different logging destinations. This enum
+ * defines the list of logging destinations supported by the VPU firmware (NOTE:
+ * a specific VPU FW binary may support only a subset of such output
+ * destinations, depending on the target platform and compile options).
+ */
+enum vpu_trace_destination {
+       VPU_TRACE_DESTINATION_PIPEPRINT = 0x1,
+       VPU_TRACE_DESTINATION_VERBOSE_TRACING = 0x2,
+       VPU_TRACE_DESTINATION_NORTH_PEAK = 0x4,
+};
+
+/*
+ * Processor bit shifts (for loggable HW components).
+ */
+#define VPU_TRACE_PROC_BIT_ARM      0
+#define VPU_TRACE_PROC_BIT_LRT      1
+#define VPU_TRACE_PROC_BIT_LNN      2
+#define VPU_TRACE_PROC_BIT_SHV_0     3
+#define VPU_TRACE_PROC_BIT_SHV_1     4
+#define VPU_TRACE_PROC_BIT_SHV_2     5
+#define VPU_TRACE_PROC_BIT_SHV_3     6
+#define VPU_TRACE_PROC_BIT_SHV_4     7
+#define VPU_TRACE_PROC_BIT_SHV_5     8
+#define VPU_TRACE_PROC_BIT_SHV_6     9
+#define VPU_TRACE_PROC_BIT_SHV_7     10
+#define VPU_TRACE_PROC_BIT_SHV_8     11
+#define VPU_TRACE_PROC_BIT_SHV_9     12
+#define VPU_TRACE_PROC_BIT_SHV_10    13
+#define VPU_TRACE_PROC_BIT_SHV_11    14
+#define VPU_TRACE_PROC_BIT_SHV_12    15
+#define VPU_TRACE_PROC_BIT_SHV_13    16
+#define VPU_TRACE_PROC_BIT_SHV_14    17
+#define VPU_TRACE_PROC_BIT_SHV_15    18
+#define VPU_TRACE_PROC_BIT_ACT_SHV_0 19
+#define VPU_TRACE_PROC_BIT_ACT_SHV_1 20
+#define VPU_TRACE_PROC_BIT_ACT_SHV_2 21
+#define VPU_TRACE_PROC_BIT_ACT_SHV_3 22
+#define VPU_TRACE_PROC_NO_OF_HW_DEVS 23
+
+/* KMB HW component IDs are sequential, so define first and last IDs. */
+#define VPU_TRACE_PROC_BIT_KMB_FIRST VPU_TRACE_PROC_BIT_LRT
+#define VPU_TRACE_PROC_BIT_KMB_LAST  VPU_TRACE_PROC_BIT_SHV_15
+
+struct vpu_boot_l2_cache_config {
+       u8 use;
+       u8 cfg;
+};
+
+struct vpu_warm_boot_section {
+       u32 src;
+       u32 dst;
+       u32 size;
+       u32 core_id;
+       u32 is_clear_op;
+};
+
+struct vpu_boot_params {
+       u32 magic;
+       u32 vpu_id;
+       u32 vpu_count;
+       u32 pad0[5];
+       /* Clock frequencies: 0x20 - 0xFF */
+       u32 frequency;
+       u32 pll[VPU_BOOT_PLL_COUNT][VPU_BOOT_PLL_OUT_COUNT];
+       u32 perf_clk_frequency;
+       u32 pad1[42];
+       /* Memory regions: 0x100 - 0x1FF */
+       u64 ipc_header_area_start;
+       u32 ipc_header_area_size;
+       u64 shared_region_base;
+       u32 shared_region_size;
+       u64 ipc_payload_area_start;
+       u32 ipc_payload_area_size;
+       u64 global_aliased_pio_base;
+       u32 global_aliased_pio_size;
+       u32 autoconfig;
+       struct vpu_boot_l2_cache_config cache_defaults[VPU_BOOT_L2_CACHE_CFG_NUM];
+       u64 global_memory_allocator_base;
+       u32 global_memory_allocator_size;
+       /**
+        * ShaveNN FW section VPU base address
+        * On VPU2.7 HW this address must be within 2GB range starting from L2C_PAGE_TABLE base
+        */
+       u64 shave_nn_fw_base;
+       u64 save_restore_ret_address; /* stores the address of FW's restore entry point */
+       u32 pad2[43];
+       /* IRQ re-direct numbers: 0x200 - 0x2FF */
+       s32 watchdog_irq_mss;
+       s32 watchdog_irq_nce;
+       /* ARM -> VPU doorbell interrupt. ARM is notifying VPU of async command or compute job. */
+       u32 host_to_vpu_irq;
+       /* VPU -> ARM job done interrupt. VPU is notifying ARM of compute job completion. */
+       u32 job_done_irq;
+       /* VPU -> ARM IRQ line to use to request MMU update. */
+       u32 mmu_update_request_irq;
+       /* ARM -> VPU IRQ line to use to notify of MMU update completion. */
+       u32 mmu_update_done_irq;
+       /* ARM -> VPU IRQ line to use to request power level change. */
+       u32 set_power_level_irq;
+       /* VPU -> ARM IRQ line to use to notify of power level change completion. */
+       u32 set_power_level_done_irq;
+       /* VPU -> ARM IRQ line to use to notify of VPU idle state change */
+       u32 set_vpu_idle_update_irq;
+       /* VPU -> ARM IRQ line to use to request counter reset. */
+       u32 metric_query_event_irq;
+       /* ARM -> VPU IRQ line to use to notify of counter reset completion. */
+       u32 metric_query_event_done_irq;
+       /* VPU -> ARM IRQ line to use to notify of preemption completion. */
+       u32 preemption_done_irq;
+       /* Padding. */
+       u32 pad3[52];
+       /* Silicon information: 0x300 - 0x3FF */
+       u32 host_version_id;
+       u32 si_stepping;
+       u64 device_id;
+       u64 feature_exclusion;
+       u64 sku;
+       /** PLL ratio for minimum clock frequency */
+       u32 min_freq_pll_ratio;
+       /** PLL ratio for maximum clock frequency */
+       u32 max_freq_pll_ratio;
+       /**
+        * Initial log level threshold (messages with log level severity less than
+        * the threshold will not be logged); applies to every enabled logging
+        * destination and loggable HW component. See 'mvLog_t' enum for acceptable
+        * values.
+        */
+       u32 default_trace_level;
+       u32 boot_type;
+       u64 punit_telemetry_sram_base;
+       u64 punit_telemetry_sram_size;
+       u32 vpu_telemetry_enable;
+       u64 crit_tracing_buff_addr;
+       u32 crit_tracing_buff_size;
+       u64 verbose_tracing_buff_addr;
+       u32 verbose_tracing_buff_size;
+       u64 verbose_tracing_sw_component_mask; /* TO BE REMOVED */
+       /**
+        * Mask of destinations to which logging messages are delivered; bitwise OR
+        * of values defined in vpu_trace_destination enum.
+        */
+       u32 trace_destination_mask;
+       /**
+        * Mask of hardware components for which logging is enabled; bitwise OR of
+        * bits defined by the VPU_TRACE_PROC_BIT_* macros.
+        */
+       u64 trace_hw_component_mask;
+       /** Mask of trace message formats supported by the driver */
+       u64 tracing_buff_message_format_mask;
+       u64 trace_reserved_1[2];
+       /**
+        * Period at which the VPU reads the temp sensor values into MMIO, on
+        * platforms where that is necessary (in ms). 0 to disable reads.
+        */
+       u32 temp_sensor_period_ms;
+       /** PLL ratio for efficient clock frequency */
+       u32 pn_freq_pll_ratio;
+       u32 pad4[28];
+       /* Warm boot information: 0x400 - 0x43F */
+       u32 warm_boot_sections_count;
+       u32 warm_boot_start_address_reference;
+       u32 warm_boot_section_info_address_offset;
+       u32 pad5[13];
+       /* Power States transitions timestamps: 0x440 - 0x46F*/
+       struct {
+               /* VPU_IDLE -> VPU_ACTIVE transition initiated timestamp */
+               u64 vpu_active_state_requested;
+               /* VPU_IDLE -> VPU_ACTIVE transition completed timestamp */
+               u64 vpu_active_state_achieved;
+               /* VPU_ACTIVE -> VPU_IDLE transition initiated timestamp */
+               u64 vpu_idle_state_requested;
+               /* VPU_ACTIVE -> VPU_IDLE transition completed timestamp */
+               u64 vpu_idle_state_achieved;
+               /* VPU_IDLE -> VPU_STANDBY transition initiated timestamp */
+               u64 vpu_standby_state_requested;
+               /* VPU_IDLE -> VPU_STANDBY transition completed timestamp */
+               u64 vpu_standby_state_achieved;
+       } power_states_timestamps;
+       /* VPU scheduling mode. Values defined by VPU_SCHEDULING_MODE_* macros. */
+       u32 vpu_scheduling_mode;
+       /* Present call period in milliseconds. */
+       u32 vpu_focus_present_timer_ms;
+       /* Unused/reserved: 0x478 - 0xFFF */
+       u32 pad6[738];
+};
+
+/*
+ * Magic numbers set between host and vpu to detect corruptio of tracing init
+ */
+
+#define VPU_TRACING_BUFFER_CANARY (0xCAFECAFE)
+
+/* Tracing buffer message format definitions */
+#define VPU_TRACING_FORMAT_STRING 0
+#define VPU_TRACING_FORMAT_MIPI          2
+/*
+ * Header of the tracing buffer.
+ * The below defined header will be stored at the beginning of
+ * each allocated tracing buffer, followed by a series of 256b
+ * of ASCII trace message entries.
+ */
+struct vpu_tracing_buffer_header {
+       /**
+        * Magic number set by host to detect corruption
+        * @see VPU_TRACING_BUFFER_CANARY
+        */
+       u32 host_canary_start;
+       /* offset from start of buffer for trace entries */
+       u32 read_index;
+       u32 pad_to_cache_line_size_0[14];
+       /* End of first cache line */
+
+       /**
+        * Magic number set by host to detect corruption
+        * @see VPU_TRACING_BUFFER_CANARY
+        */
+       u32 vpu_canary_start;
+       /* offset from start of buffer from write start */
+       u32 write_index;
+       /* counter for buffer wrapping */
+       u32 wrap_count;
+       /* legacy field - do not use */
+       u32 reserved_0;
+       /**
+        * Size of the log buffer include this header (@header_size) and space
+        * reserved for all messages. If @alignment` is greater that 0 the @Size
+        * must be multiple of @Alignment.
+        */
+       u32 size;
+       /* Header version */
+       u16 header_version;
+       /* Header size */
+       u16 header_size;
+       /*
+        * Format of the messages in the trace buffer
+        * 0 - null terminated string
+        * 1 - size + null terminated string
+        * 2 - MIPI-SysT encoding
+        */
+       u32 format;
+       /*
+        * Message alignment
+        * 0 - messages are place 1 after another
+        * n - every message starts and multiple on offset
+        */
+       u32 alignment; /* 64, 128, 256 */
+       /* Name of the logging entity, i.e "LRT", "LNN", "SHV0", etc */
+       char name[16];
+       u32 pad_to_cache_line_size_1[4];
+       /* End of second cache line */
+};
+
+#pragma pack(pop)
+
+#endif
include/uapi/drm/ivpu_accel.h +21 -1
index 5f953ebafc9b..005b40b7326c 100644
@@ -51,6 +51,11 @@ extern "C" {
 #define DRM_IVPU_PARAM_CONTEXT_BASE_ADDRESS 5
 #define DRM_IVPU_PARAM_CONTEXT_PRIORITY            6
 #define DRM_IVPU_PARAM_CONTEXT_ID          7
+#define DRM_IVPU_PARAM_FW_API_VERSION      8
+#define DRM_IVPU_PARAM_ENGINE_HEARTBEAT            9
+#define DRM_IVPU_PARAM_UNIQUE_INFERENCE_ID  10
+#define DRM_IVPU_PARAM_TILE_CONFIG         11
+#define DRM_IVPU_PARAM_SKU                 12
 
 #define DRM_IVPU_PLATFORM_TYPE_SILICON     0
 
@@ -94,6 +99,22 @@ struct drm_ivpu_param {
         * %DRM_IVPU_PARAM_CONTEXT_ID:
         * Current context ID, always greater than 0 (read-only)
         *
+        * %DRM_IVPU_PARAM_FW_API_VERSION:
+        * Firmware API version array (read-only)
+        *
+        * %DRM_IVPU_PARAM_ENGINE_HEARTBEAT:
+        * Heartbeat value from an engine (read-only).
+        * Engine ID (i.e. DRM_IVPU_ENGINE_COMPUTE) is given via index.
+        *
+        * %DRM_IVPU_PARAM_UNIQUE_INFERENCE_ID:
+        * Device-unique inference ID (read-only)
+        *
+        * %DRM_IVPU_PARAM_TILE_CONFIG:
+        * VPU tile configuration  (read-only)
+        *
+        * %DRM_IVPU_PARAM_SKU:
+        * VPU SKU ID (read-only)
+        *
         */
        __u32 param;
 
--

2.34.1

#17 lore raw

Re: [PATCH v5 5/7] accel/ivpu: Implement firmware parsing and booting

Hi,

Quoted 525 lines · Oded Gabbay
Quoted 523 lines · Jacek Lawrynowicz

Read, parse and boot VPU firmware image.

---
 drivers/accel/ivpu/Makefile       |   1 +
 drivers/accel/ivpu/ivpu_drv.c     | 131 +++++++++-
 drivers/accel/ivpu/ivpu_drv.h     |  11 +
 drivers/accel/ivpu/ivpu_fw.c      | 419 ++++++++++++++++++++++++++++++
 drivers/accel/ivpu/ivpu_fw.h      |  38 +++
 drivers/accel/ivpu/ivpu_hw_mtl.c  |  10 +
 drivers/accel/ivpu/vpu_boot_api.h | 349 +++++++++++++++++++++++++
 include/uapi/drm/ivpu_accel.h     |  21 ++
 8 files changed, 979 insertions(+), 1 deletion(-)
 create mode 100644 drivers/accel/ivpu/ivpu_fw.c
 create mode 100644 drivers/accel/ivpu/ivpu_fw.h
 create mode 100644 drivers/accel/ivpu/vpu_boot_api.h
drivers/accel/ivpu/Makefile +1 -0
index 46595f0112e3..9fa6a76e9d79 100644
@@ -3,6 +3,7 @@
 
 intel_vpu-y := \
        ivpu_drv.o \
+       ivpu_fw.o \
        ivpu_gem.o \
        ivpu_hw_mtl.o \
        ivpu_ipc.o \
drivers/accel/ivpu/ivpu_drv.c +130 -1
index 6643ae6b5a52..53e103f64832 100644
@@ -14,10 +14,13 @@
 #include <drm/drm_ioctl.h>
 #include <drm/drm_prime.h>
 
+#include "vpu_boot_api.h"
 #include "ivpu_drv.h"
+#include "ivpu_fw.h"
 #include "ivpu_gem.h"
 #include "ivpu_hw.h"
 #include "ivpu_ipc.h"
+#include "ivpu_jsm_msg.h"
 #include "ivpu_mmu.h"
 #include "ivpu_mmu_context.h"
 
@@ -32,6 +35,10 @@ int ivpu_dbg_mask;
 module_param_named(dbg_mask, ivpu_dbg_mask, int, 0644);
 MODULE_PARM_DESC(dbg_mask, "Driver debug mask. See IVPU_DBG_* macros.");
 
+int ivpu_test_mode;
+module_param_named_unsafe(test_mode, ivpu_test_mode, int, 0644);
+MODULE_PARM_DESC(test_mode, "Test mode: 0 - normal operation, 1 - fw unit test, 2 - null hw");
+
 u8 ivpu_pll_min_ratio;
 module_param_named(pll_min_ratio, ivpu_pll_min_ratio, byte, 0644);
 MODULE_PARM_DESC(pll_min_ratio, "Minimum PLL ratio used to set VPU frequency");
@@ -129,6 +136,28 @@ static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_f
        case DRM_IVPU_PARAM_CONTEXT_ID:
                args->value = file_priv->ctx.id;
                break;
+       case DRM_IVPU_PARAM_FW_API_VERSION:
+               if (args->index < VPU_FW_API_VER_NUM) {
+                       struct vpu_firmware_header *fw_hdr;
+
+                       fw_hdr = (struct vpu_firmware_header *)vdev->fw->file->data;
+                       args->value = fw_hdr->api_version[args->index];
+               } else {
+                       ret = -EINVAL;
+               }
+               break;
+       case DRM_IVPU_PARAM_ENGINE_HEARTBEAT:
+               ret = ivpu_jsm_get_heartbeat(vdev, args->index, &args->value);
+               break;
+       case DRM_IVPU_PARAM_UNIQUE_INFERENCE_ID:
+               args->value = (u64)atomic64_inc_return(&vdev->unique_id_counter);
+               break;
+       case DRM_IVPU_PARAM_TILE_CONFIG:
+               args->value = vdev->hw->tile_fuse;
+               break;
+       case DRM_IVPU_PARAM_SKU:
+               args->value = vdev->hw->sku;
+               break;
        default:
                ret = -EINVAL;
                break;
@@ -226,11 +255,85 @@ static const struct drm_ioctl_desc ivpu_drm_ioctls[] = {
        DRM_IOCTL_DEF_DRV(IVPU_BO_USERPTR, ivpu_bo_userptr_ioctl, 0),
 };
 
+static int ivpu_wait_for_ready(struct ivpu_device *vdev)
+{
+       struct ivpu_ipc_consumer cons;
+       struct ivpu_ipc_hdr ipc_hdr;
+       unsigned long timeout;
+       int ret;
+
+       if (ivpu_test_mode == IVPU_TEST_MODE_FW_TEST)
+               return 0;
+
+       ivpu_ipc_consumer_add(vdev, &cons, IVPU_IPC_CHAN_BOOT_MSG);
+
+       timeout = jiffies + msecs_to_jiffies(vdev->timeout.boot);
+       while (1) {
+               ret = ivpu_ipc_irq_handler(vdev);
+               if (ret)
+                       break;
+               ret = ivpu_ipc_receive(vdev, &cons, &ipc_hdr, NULL, 0);
+               if (ret != -ETIMEDOUT || time_after_eq(jiffies, timeout))
+                       break;
+
+               cond_resched();
+       }
+
+       ivpu_ipc_consumer_del(vdev, &cons);
+
+       if (!ret && ipc_hdr.data_addr != IVPU_IPC_BOOT_MSG_DATA_ADDR) {
+               ivpu_err(vdev, "Invalid VPU ready message: 0x%x\n",
+                        ipc_hdr.data_addr);
+               return -EIO;
+       }
+
+       if (!ret)
+               ivpu_info(vdev, "VPU ready message received successfully\n");
+       else
+               ivpu_hw_diagnose_failure(vdev);
+
+       return ret;
+}
+
+/**
+ * ivpu_boot() - Start VPU firmware
+ * @vdev: VPU device
+ *
+ * This function is paired with ivpu_shutdown() but it doesn't power up the
+ * VPU because power up has to be called very early in ivpu_probe().
+ */
+int ivpu_boot(struct ivpu_device *vdev)
+{
+       int ret;
+
+       /* Update boot params located at first 4KB of FW memory */
+       ivpu_fw_boot_params_setup(vdev, vdev->fw->mem->kvaddr);
+
+       ret = ivpu_hw_boot_fw(vdev);
+       if (ret) {
+               ivpu_err(vdev, "Failed to start the firmware: %d\n", ret);
+               return ret;
+       }
+
+       ret = ivpu_wait_for_ready(vdev);
+       if (ret) {
+               ivpu_err(vdev, "Failed to boot the firmware: %d\n", ret);
+               return ret;
+       }
+
+       ivpu_hw_irq_clear(vdev);
+       enable_irq(vdev->irq);
+       ivpu_hw_irq_enable(vdev);
+       ivpu_ipc_enable(vdev);
+       return 0;
+}
+
 int ivpu_shutdown(struct ivpu_device *vdev)
 {
        int ret;
 
        ivpu_hw_irq_disable(vdev);
+       disable_irq(vdev->irq);
        ivpu_ipc_disable(vdev);
        ivpu_mmu_disable(vdev);
 
@@ -348,6 +451,10 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
        if (!vdev->mmu)
                return -ENOMEM;
 
+       vdev->fw = drmm_kzalloc(&vdev->drm, sizeof(*vdev->fw), GFP_KERNEL);
+       if (!vdev->fw)
+               return -ENOMEM;
+
        vdev->ipc = drmm_kzalloc(&vdev->drm, sizeof(*vdev->ipc), GFP_KERNEL);
        if (!vdev->ipc)
                return -ENOMEM;
@@ -356,6 +463,7 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
        vdev->platform = IVPU_PLATFORM_INVALID;
        vdev->context_xa_limit.min = IVPU_GLOBAL_CONTEXT_MMU_SSID + 1;
        vdev->context_xa_limit.max = IVPU_CONTEXT_LIMIT;
+       atomic64_set(&vdev->unique_id_counter, 0);
        xa_init_flags(&vdev->context_xa, XA_FLAGS_ALLOC);
 
        ret = ivpu_pci_init(vdev);
@@ -396,14 +504,34 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
                goto err_mmu_gctx_fini;
        }
 
+       ret = ivpu_fw_init(vdev);
+       if (ret) {
+               ivpu_err(vdev, "Failed to initialize firmware: %d\n", ret);
+               goto err_mmu_gctx_fini;
+       }
+
        ret = ivpu_ipc_init(vdev);
        if (ret) {
                ivpu_err(vdev, "Failed to initialize IPC: %d\n", ret);
-               goto err_mmu_gctx_fini;
+               goto err_fw_fini;
+       }
+
+       ret = ivpu_fw_load(vdev);
+       if (ret) {
+               ivpu_err(vdev, "Failed to load firmware: %d\n", ret);
+               goto err_fw_fini;
+       }
+
+       ret = ivpu_boot(vdev);
+       if (ret) {
+               ivpu_err(vdev, "Failed to boot: %d\n", ret);
+               goto err_fw_fini;
        }
 
        return 0;
 
+err_fw_fini:
+       ivpu_fw_fini(vdev);
 err_mmu_gctx_fini:
        ivpu_mmu_global_context_fini(vdev);
 err_power_down:
@@ -417,6 +545,7 @@ static void ivpu_dev_fini(struct ivpu_device *vdev)
 {
        ivpu_shutdown(vdev);
        ivpu_ipc_fini(vdev);
+       ivpu_fw_fini(vdev);
        ivpu_mmu_global_context_fini(vdev);
 
        drm_WARN_ON(&vdev->drm, !xa_empty(&vdev->context_xa));
drivers/accel/ivpu/ivpu_drv.h +11 -0
index c1e76d1fb8ba..317ae61f43bd 100644
@@ -74,6 +74,7 @@ struct ivpu_wa_table {
 
 struct ivpu_hw_info;
 struct ivpu_mmu_info;
+struct ivpu_fw_info;
 struct ivpu_ipc_info;
 
 struct ivpu_device {
@@ -86,12 +87,15 @@ struct ivpu_device {
        struct ivpu_wa_table wa;
        struct ivpu_hw_info *hw;
        struct ivpu_mmu_info *mmu;
+       struct ivpu_fw_info *fw;
        struct ivpu_ipc_info *ipc;
 
        struct ivpu_mmu_context gctx;
        struct xarray context_xa;
        struct xa_limit context_xa_limit;
 
+       atomic64_t unique_id_counter;
+
        struct {
                int boot;
                int jsm;
@@ -116,9 +120,16 @@ extern int ivpu_dbg_mask;
 extern u8 ivpu_pll_min_ratio;
 extern u8 ivpu_pll_max_ratio;
 
+#define IVPU_TEST_MODE_DISABLED  0
+#define IVPU_TEST_MODE_FW_TEST   1
+#define IVPU_TEST_MODE_NULL_HW   2
+extern int ivpu_test_mode;
+
 struct ivpu_file_priv *ivpu_file_priv_get(struct ivpu_file_priv *file_priv);
 struct ivpu_file_priv *ivpu_file_priv_get_by_ctx_id(struct ivpu_device *vdev, unsigned long id);
 void ivpu_file_priv_put(struct ivpu_file_priv **link);
+
+int ivpu_boot(struct ivpu_device *vdev);
 int ivpu_shutdown(struct ivpu_device *vdev);
 
 static inline bool ivpu_is_mtl(struct ivpu_device *vdev)
drivers/accel/ivpu/ivpu_fw.c new file +232 -0
new file mode 100644
index 000000000000..4baa0767a10d
@@ -0,0 +1,419 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2020-2023 Intel Corporation
+ */
+
+#include <linux/firmware.h>
+#include <linux/highmem.h>
+#include <linux/moduleparam.h>
+#include <linux/pci.h>
+
+#include "vpu_boot_api.h"
+#include "ivpu_drv.h"
+#include "ivpu_fw.h"
+#include "ivpu_gem.h"
+#include "ivpu_hw.h"
+#include "ivpu_ipc.h"
+
+#define FW_GLOBAL_MEM_START    (2ull * SZ_1G)
+#define FW_GLOBAL_MEM_END      (3ull * SZ_1G)
+#define FW_SHARED_MEM_SIZE     SZ_256M /* Must be aligned to FW_SHARED_MEM_ALIGNMENT */
+#define FW_SHARED_MEM_ALIGNMENT        SZ_128K /* VPU MTRR limitation */
+#define FW_RUNTIME_MAX_SIZE    SZ_512M
+#define FW_SHAVE_NN_MAX_SIZE   SZ_2M
+#define FW_RUNTIME_MIN_ADDR    (FW_GLOBAL_MEM_START)
+#define FW_RUNTIME_MAX_ADDR    (FW_GLOBAL_MEM_END - FW_SHARED_MEM_SIZE)
+#define FW_VERSION_HEADER_SIZE SZ_4K
+#define FW_FILE_IMAGE_OFFSET   (VPU_FW_HEADER_SIZE + FW_VERSION_HEADER_SIZE)
+
+#define WATCHDOG_MSS_REDIRECT  32
+#define WATCHDOG_NCE_REDIRECT  33
+
+#define ADDR_TO_L2_CACHE_CFG(addr) ((addr) >> 31)
+
+#define IVPU_FW_CHECK_API(vdev, fw_hdr, name) ivpu_fw_check_api(vdev, fw_hdr, #name, \
+                                                                 VPU_##name##_API_VER_INDEX, \
+                                                                 VPU_##name##_API_VER_MAJOR, \
+                                                                 VPU_##name##_API_VER_MINOR)
+
+static char *ivpu_firmware;
+module_param_named_unsafe(firmware, ivpu_firmware, charp, 0644);
+MODULE_PARM_DESC(firmware, "VPU firmware binary in /lib/firmware/..");
+
+static int ivpu_fw_request(struct ivpu_device *vdev)
+{
+       static const char * const fw_names[] = {
+               "mtl_vpu.bin",
+               "intel/vpu/mtl_vpu_v0.0.bin"
+       };
+       int ret = -ENOENT;
+       int i;
+
+       if (ivpu_firmware)
+               return request_firmware(&vdev->fw->file, ivpu_firmware, vdev->drm.dev);
+
+       for (i = 0; i < ARRAY_SIZE(fw_names); i++) {
+               ret = firmware_request_nowarn(&vdev->fw->file, fw_names[i], vdev->drm.dev);
+               if (!ret)
+                       return 0;
+       }
+
+       ivpu_err(vdev, "Failed to request firmware: %d\n", ret);
+       return ret;
+}
+
+static void
+ivpu_fw_check_api(struct ivpu_device *vdev, const struct vpu_firmware_header *fw_hdr,
+                 const char *str, int index, u16 expected_major, u16 expected_minor)
+{
+       u16 major = (u16)(fw_hdr->api_version[index] >> 16);
+       u16 minor = (u16)(fw_hdr->api_version[index]);
+
+       if (major != expected_major) {
+               ivpu_warn(vdev, "Incompatible FW %s API version: %d.%d (expected %d.%d)\n",
+                         str, major, minor, expected_major, expected_minor);
+       }
+       ivpu_dbg(vdev, FW_BOOT, "FW %s API version: %d.%d (expected %d.%d)\n",
+                str, major, minor, expected_major, expected_minor);
+}
+
+static int ivpu_fw_parse(struct ivpu_device *vdev)
+{
+       struct ivpu_fw_info *fw = vdev->fw;
+       const struct vpu_firmware_header *fw_hdr = (const void *)fw->file->data;
+       u64 runtime_addr, image_load_addr, runtime_size, image_size;
+
+       if (fw->file->size <= FW_FILE_IMAGE_OFFSET) {
+               ivpu_err(vdev, "Firmware file is too small: %zu\n", fw->file->size);
+               return -EINVAL;
+       }
+
+       if (fw_hdr->header_version != VPU_FW_HEADER_VERSION) {
+               ivpu_err(vdev, "Invalid firmware header version: %u\n", fw_hdr->header_version);
+               return -EINVAL;
+       }
+
+       runtime_addr = fw_hdr->boot_params_load_address;
+       runtime_size = fw_hdr->runtime_size;
+       image_load_addr = fw_hdr->image_load_address;
+       image_size = fw_hdr->image_size;
+
+       if (runtime_addr < FW_RUNTIME_MIN_ADDR || runtime_addr > FW_RUNTIME_MAX_ADDR) {
+               ivpu_err(vdev, "Invalid firmware runtime address: 0x%llx\n", runtime_addr);
+               return -EINVAL;
+       }
+
+       if (runtime_size < fw->file->size || runtime_size > FW_RUNTIME_MAX_SIZE) {
+               ivpu_err(vdev, "Invalid firmware runtime size: %llu\n", runtime_size);
+               return -EINVAL;
+       }
+
+       if (FW_FILE_IMAGE_OFFSET + image_size > fw->file->size) {
+               ivpu_err(vdev, "Invalid image size: %llu\n", image_size);
+               return -EINVAL;
+       }
+
+       if (image_load_addr < runtime_addr ||
+           image_load_addr + image_size > runtime_addr + runtime_size) {
+               ivpu_err(vdev, "Invalid firmware load address size: 0x%llx and size %llu\n",
+                        image_load_addr, image_size);
+               return -EINVAL;
+       }
+
+       if (fw_hdr->shave_nn_fw_size > FW_SHAVE_NN_MAX_SIZE) {
+               ivpu_err(vdev, "SHAVE NN firmware is too big: %u\n", fw_hdr->shave_nn_fw_size);
+               return -EINVAL;
+       }
+
+       if (fw_hdr->entry_point < image_load_addr ||
+           fw_hdr->entry_point >= image_load_addr + image_size) {
+               ivpu_err(vdev, "Invalid entry point: 0x%llx\n", fw_hdr->entry_point);
+               return -EINVAL;
+       }
+
+       fw->runtime_addr = runtime_addr;
+       fw->runtime_size = runtime_size;
+       fw->image_load_offset = image_load_addr - runtime_addr;
+       fw->image_size = image_size;
+       fw->shave_nn_size = PAGE_ALIGN(fw_hdr->shave_nn_fw_size);
+
+       fw->cold_boot_entry_point = fw_hdr->entry_point;
+       fw->entry_point = fw->cold_boot_entry_point;
+
+       ivpu_dbg(vdev, FW_BOOT, "Header version: 0x%x, format 0x%x\n",
+                fw_hdr->header_version, fw_hdr->image_format);
+       ivpu_dbg(vdev, FW_BOOT, "Size: file %lu image %u runtime %u shavenn %u\n",
+                fw->file->size, fw->image_size, fw->runtime_size, fw->shave_nn_size);
+       ivpu_dbg(vdev, FW_BOOT, "Address: runtime 0x%llx, load 0x%llx, entry point 0x%llx\n",
+                fw->runtime_addr, image_load_addr, fw->entry_point);
+       ivpu_dbg(vdev, FW_BOOT, "FW version: %s\n", (char *)fw_hdr + VPU_FW_HEADER_SIZE);
+
+       IVPU_FW_CHECK_API(vdev, fw_hdr, BOOT);
+       IVPU_FW_CHECK_API(vdev, fw_hdr, JSM);
+
+       return 0;
+}
+
+static void ivpu_fw_release(struct ivpu_device *vdev)
+{
+       release_firmware(vdev->fw->file);
+}
+
+static int ivpu_fw_update_global_range(struct ivpu_device *vdev)
+{
+       struct ivpu_fw_info *fw = vdev->fw;
+       u64 start = ALIGN(fw->runtime_addr + fw->runtime_size, FW_SHARED_MEM_ALIGNMENT);
+       u64 size = FW_SHARED_MEM_SIZE;
+
+       if (start + size > FW_GLOBAL_MEM_END) {
+               ivpu_err(vdev, "No space for shared region, start %lld, size %lld\n", start, size);
+               return -EINVAL;
+       }
+
+       ivpu_hw_init_range(&vdev->hw->ranges.global_low, start, size);
+       return 0;
+}
+
+static int ivpu_fw_mem_init(struct ivpu_device *vdev)
+{
+       struct ivpu_fw_info *fw = vdev->fw;
+       int ret;
+
+       ret = ivpu_fw_update_global_range(vdev);
+       if (ret)
+               return ret;
+
+       fw->mem = ivpu_bo_alloc_internal(vdev, fw->runtime_addr, fw->runtime_size, DRM_IVPU_BO_WC);
+       if (!fw->mem) {
+               ivpu_err(vdev, "Failed to allocate firmware runtime memory\n");
+               return -ENOMEM;
+       }
+
+       if (fw->shave_nn_size) {
+               fw->mem_shave_nn = ivpu_bo_alloc_internal(vdev, vdev->hw->ranges.global_high.start,
+                                                         fw->shave_nn_size, DRM_IVPU_BO_UNCACHED);
+               if (!fw->mem_shave_nn) {
+                       ivpu_err(vdev, "Failed to allocate shavenn buffer\n");
+                       ivpu_bo_free_internal(fw->mem);
+                       return -ENOMEM;
+               }
+       }
+
+       return 0;
+}
+
+static void ivpu_fw_mem_fini(struct ivpu_device *vdev)
+{
+       struct ivpu_fw_info *fw = vdev->fw;
+
+       if (fw->mem_shave_nn) {
+               ivpu_bo_free_internal(fw->mem_shave_nn);
+               fw->mem_shave_nn = NULL;
+       }
+
+       ivpu_bo_free_internal(fw->mem);
+       fw->mem = NULL;
+}
+
+int ivpu_fw_init(struct ivpu_device *vdev)
+{
+       int ret;
+
+       ret = ivpu_fw_request(vdev);
+       if (ret)
+               return ret;
+
+       ret = ivpu_fw_parse(vdev);
+       if (ret)
+               goto err_fw_release;
+
+       ret = ivpu_fw_mem_init(vdev);
+       if (ret)
+               goto err_fw_release;
Quoted 8 lines

drivers/accel/ivpu/ivpu_fw.c line 237

Quoted 5 lines
drivers/accel/ivpu/ivpu_fw.c new file +5 -0
+
+       return 0;
+
+err_fw_release:
+       ivpu_fw_release(vdev);

Why do you release the firmware only on error ?
Why maintain the hold on the firmware file for as long as the device lives ? Can't you release the file once you loaded the firmware to the device's memory ?

Yes, we keep the fw image in memory for the following reasons:
1. It reduces the time of reset, recovery and resume without warmboot, where we need to overwite the fw.
2. It prevents user space from changing the fw binary while the module is loaded.
If a new version of the fw was installed it will be used on the next insmod which prevents hard to detect bugs.

Regards,
Jacek

#18 lore raw

Re: [PATCH v5 5/7] accel/ivpu: Implement firmware parsing and booting

Quoted 538 lines · Jacek Lawrynowicz

Hi,

Quoted 525 lines · Oded Gabbay
Quoted 523 lines · Jacek Lawrynowicz

Read, parse and boot VPU firmware image.

---
 drivers/accel/ivpu/Makefile       |   1 +
 drivers/accel/ivpu/ivpu_drv.c     | 131 +++++++++-
 drivers/accel/ivpu/ivpu_drv.h     |  11 +
 drivers/accel/ivpu/ivpu_fw.c      | 419 ++++++++++++++++++++++++++++++
 drivers/accel/ivpu/ivpu_fw.h      |  38 +++
 drivers/accel/ivpu/ivpu_hw_mtl.c  |  10 +
 drivers/accel/ivpu/vpu_boot_api.h | 349 +++++++++++++++++++++++++
 include/uapi/drm/ivpu_accel.h     |  21 ++
 8 files changed, 979 insertions(+), 1 deletion(-)
 create mode 100644 drivers/accel/ivpu/ivpu_fw.c
 create mode 100644 drivers/accel/ivpu/ivpu_fw.h
 create mode 100644 drivers/accel/ivpu/vpu_boot_api.h
drivers/accel/ivpu/Makefile +1 -0
index 46595f0112e3..9fa6a76e9d79 100644
@@ -3,6 +3,7 @@
 
 intel_vpu-y := \
        ivpu_drv.o \
+       ivpu_fw.o \
        ivpu_gem.o \
        ivpu_hw_mtl.o \
        ivpu_ipc.o \
drivers/accel/ivpu/ivpu_drv.c +130 -1
index 6643ae6b5a52..53e103f64832 100644
@@ -14,10 +14,13 @@
 #include <drm/drm_ioctl.h>
 #include <drm/drm_prime.h>
 
+#include "vpu_boot_api.h"
 #include "ivpu_drv.h"
+#include "ivpu_fw.h"
 #include "ivpu_gem.h"
 #include "ivpu_hw.h"
 #include "ivpu_ipc.h"
+#include "ivpu_jsm_msg.h"
 #include "ivpu_mmu.h"
 #include "ivpu_mmu_context.h"
 
@@ -32,6 +35,10 @@ int ivpu_dbg_mask;
 module_param_named(dbg_mask, ivpu_dbg_mask, int, 0644);
 MODULE_PARM_DESC(dbg_mask, "Driver debug mask. See IVPU_DBG_* macros.");
 
+int ivpu_test_mode;
+module_param_named_unsafe(test_mode, ivpu_test_mode, int, 0644);
+MODULE_PARM_DESC(test_mode, "Test mode: 0 - normal operation, 1 - fw unit test, 2 - null hw");
+
 u8 ivpu_pll_min_ratio;
 module_param_named(pll_min_ratio, ivpu_pll_min_ratio, byte, 0644);
 MODULE_PARM_DESC(pll_min_ratio, "Minimum PLL ratio used to set VPU frequency");
@@ -129,6 +136,28 @@ static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_f
        case DRM_IVPU_PARAM_CONTEXT_ID:
                args->value = file_priv->ctx.id;
                break;
+       case DRM_IVPU_PARAM_FW_API_VERSION:
+               if (args->index < VPU_FW_API_VER_NUM) {
+                       struct vpu_firmware_header *fw_hdr;
+
+                       fw_hdr = (struct vpu_firmware_header *)vdev->fw->file->data;
+                       args->value = fw_hdr->api_version[args->index];
+               } else {
+                       ret = -EINVAL;
+               }
+               break;
+       case DRM_IVPU_PARAM_ENGINE_HEARTBEAT:
+               ret = ivpu_jsm_get_heartbeat(vdev, args->index, &args->value);
+               break;
+       case DRM_IVPU_PARAM_UNIQUE_INFERENCE_ID:
+               args->value = (u64)atomic64_inc_return(&vdev->unique_id_counter);
+               break;
+       case DRM_IVPU_PARAM_TILE_CONFIG:
+               args->value = vdev->hw->tile_fuse;
+               break;
+       case DRM_IVPU_PARAM_SKU:
+               args->value = vdev->hw->sku;
+               break;
        default:
                ret = -EINVAL;
                break;
@@ -226,11 +255,85 @@ static const struct drm_ioctl_desc ivpu_drm_ioctls[] = {
        DRM_IOCTL_DEF_DRV(IVPU_BO_USERPTR, ivpu_bo_userptr_ioctl, 0),
 };
 
+static int ivpu_wait_for_ready(struct ivpu_device *vdev)
+{
+       struct ivpu_ipc_consumer cons;
+       struct ivpu_ipc_hdr ipc_hdr;
+       unsigned long timeout;
+       int ret;
+
+       if (ivpu_test_mode == IVPU_TEST_MODE_FW_TEST)
+               return 0;
+
+       ivpu_ipc_consumer_add(vdev, &cons, IVPU_IPC_CHAN_BOOT_MSG);
+
+       timeout = jiffies + msecs_to_jiffies(vdev->timeout.boot);
+       while (1) {
+               ret = ivpu_ipc_irq_handler(vdev);
+               if (ret)
+                       break;
+               ret = ivpu_ipc_receive(vdev, &cons, &ipc_hdr, NULL, 0);
+               if (ret != -ETIMEDOUT || time_after_eq(jiffies, timeout))
+                       break;
+
+               cond_resched();
+       }
+
+       ivpu_ipc_consumer_del(vdev, &cons);
+
+       if (!ret && ipc_hdr.data_addr != IVPU_IPC_BOOT_MSG_DATA_ADDR) {
+               ivpu_err(vdev, "Invalid VPU ready message: 0x%x\n",
+                        ipc_hdr.data_addr);
+               return -EIO;
+       }
+
+       if (!ret)
+               ivpu_info(vdev, "VPU ready message received successfully\n");
+       else
+               ivpu_hw_diagnose_failure(vdev);
+
+       return ret;
+}
+
+/**
+ * ivpu_boot() - Start VPU firmware
+ * @vdev: VPU device
+ *
+ * This function is paired with ivpu_shutdown() but it doesn't power up the
+ * VPU because power up has to be called very early in ivpu_probe().
+ */
+int ivpu_boot(struct ivpu_device *vdev)
+{
+       int ret;
+
+       /* Update boot params located at first 4KB of FW memory */
+       ivpu_fw_boot_params_setup(vdev, vdev->fw->mem->kvaddr);
+
+       ret = ivpu_hw_boot_fw(vdev);
+       if (ret) {
+               ivpu_err(vdev, "Failed to start the firmware: %d\n", ret);
+               return ret;
+       }
+
+       ret = ivpu_wait_for_ready(vdev);
+       if (ret) {
+               ivpu_err(vdev, "Failed to boot the firmware: %d\n", ret);
+               return ret;
+       }
+
+       ivpu_hw_irq_clear(vdev);
+       enable_irq(vdev->irq);
+       ivpu_hw_irq_enable(vdev);
+       ivpu_ipc_enable(vdev);
+       return 0;
+}
+
 int ivpu_shutdown(struct ivpu_device *vdev)
 {
        int ret;
 
        ivpu_hw_irq_disable(vdev);
+       disable_irq(vdev->irq);
        ivpu_ipc_disable(vdev);
        ivpu_mmu_disable(vdev);
 
@@ -348,6 +451,10 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
        if (!vdev->mmu)
                return -ENOMEM;
 
+       vdev->fw = drmm_kzalloc(&vdev->drm, sizeof(*vdev->fw), GFP_KERNEL);
+       if (!vdev->fw)
+               return -ENOMEM;
+
        vdev->ipc = drmm_kzalloc(&vdev->drm, sizeof(*vdev->ipc), GFP_KERNEL);
        if (!vdev->ipc)
                return -ENOMEM;
@@ -356,6 +463,7 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
        vdev->platform = IVPU_PLATFORM_INVALID;
        vdev->context_xa_limit.min = IVPU_GLOBAL_CONTEXT_MMU_SSID + 1;
        vdev->context_xa_limit.max = IVPU_CONTEXT_LIMIT;
+       atomic64_set(&vdev->unique_id_counter, 0);
        xa_init_flags(&vdev->context_xa, XA_FLAGS_ALLOC);
 
        ret = ivpu_pci_init(vdev);
@@ -396,14 +504,34 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
                goto err_mmu_gctx_fini;
        }
 
+       ret = ivpu_fw_init(vdev);
+       if (ret) {
+               ivpu_err(vdev, "Failed to initialize firmware: %d\n", ret);
+               goto err_mmu_gctx_fini;
+       }
+
        ret = ivpu_ipc_init(vdev);
        if (ret) {
                ivpu_err(vdev, "Failed to initialize IPC: %d\n", ret);
-               goto err_mmu_gctx_fini;
+               goto err_fw_fini;
+       }
+
+       ret = ivpu_fw_load(vdev);
+       if (ret) {
+               ivpu_err(vdev, "Failed to load firmware: %d\n", ret);
+               goto err_fw_fini;
+       }
+
+       ret = ivpu_boot(vdev);
+       if (ret) {
+               ivpu_err(vdev, "Failed to boot: %d\n", ret);
+               goto err_fw_fini;
        }
 
        return 0;
 
+err_fw_fini:
+       ivpu_fw_fini(vdev);
 err_mmu_gctx_fini:
        ivpu_mmu_global_context_fini(vdev);
 err_power_down:
@@ -417,6 +545,7 @@ static void ivpu_dev_fini(struct ivpu_device *vdev)
 {
        ivpu_shutdown(vdev);
        ivpu_ipc_fini(vdev);
+       ivpu_fw_fini(vdev);
        ivpu_mmu_global_context_fini(vdev);
 
        drm_WARN_ON(&vdev->drm, !xa_empty(&vdev->context_xa));
drivers/accel/ivpu/ivpu_drv.h +11 -0
index c1e76d1fb8ba..317ae61f43bd 100644
@@ -74,6 +74,7 @@ struct ivpu_wa_table {
 
 struct ivpu_hw_info;
 struct ivpu_mmu_info;
+struct ivpu_fw_info;
 struct ivpu_ipc_info;
 
 struct ivpu_device {
@@ -86,12 +87,15 @@ struct ivpu_device {
        struct ivpu_wa_table wa;
        struct ivpu_hw_info *hw;
        struct ivpu_mmu_info *mmu;
+       struct ivpu_fw_info *fw;
        struct ivpu_ipc_info *ipc;
 
        struct ivpu_mmu_context gctx;
        struct xarray context_xa;
        struct xa_limit context_xa_limit;
 
+       atomic64_t unique_id_counter;
+
        struct {
                int boot;
                int jsm;
@@ -116,9 +120,16 @@ extern int ivpu_dbg_mask;
 extern u8 ivpu_pll_min_ratio;
 extern u8 ivpu_pll_max_ratio;
 
+#define IVPU_TEST_MODE_DISABLED  0
+#define IVPU_TEST_MODE_FW_TEST   1
+#define IVPU_TEST_MODE_NULL_HW   2
+extern int ivpu_test_mode;
+
 struct ivpu_file_priv *ivpu_file_priv_get(struct ivpu_file_priv *file_priv);
 struct ivpu_file_priv *ivpu_file_priv_get_by_ctx_id(struct ivpu_device *vdev, unsigned long id);
 void ivpu_file_priv_put(struct ivpu_file_priv **link);
+
+int ivpu_boot(struct ivpu_device *vdev);
 int ivpu_shutdown(struct ivpu_device *vdev);
 
 static inline bool ivpu_is_mtl(struct ivpu_device *vdev)
drivers/accel/ivpu/ivpu_fw.c new file +232 -0
new file mode 100644
index 000000000000..4baa0767a10d
@@ -0,0 +1,419 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2020-2023 Intel Corporation
+ */
+
+#include <linux/firmware.h>
+#include <linux/highmem.h>
+#include <linux/moduleparam.h>
+#include <linux/pci.h>
+
+#include "vpu_boot_api.h"
+#include "ivpu_drv.h"
+#include "ivpu_fw.h"
+#include "ivpu_gem.h"
+#include "ivpu_hw.h"
+#include "ivpu_ipc.h"
+
+#define FW_GLOBAL_MEM_START    (2ull * SZ_1G)
+#define FW_GLOBAL_MEM_END      (3ull * SZ_1G)
+#define FW_SHARED_MEM_SIZE     SZ_256M /* Must be aligned to FW_SHARED_MEM_ALIGNMENT */
+#define FW_SHARED_MEM_ALIGNMENT        SZ_128K /* VPU MTRR limitation */
+#define FW_RUNTIME_MAX_SIZE    SZ_512M
+#define FW_SHAVE_NN_MAX_SIZE   SZ_2M
+#define FW_RUNTIME_MIN_ADDR    (FW_GLOBAL_MEM_START)
+#define FW_RUNTIME_MAX_ADDR    (FW_GLOBAL_MEM_END - FW_SHARED_MEM_SIZE)
+#define FW_VERSION_HEADER_SIZE SZ_4K
+#define FW_FILE_IMAGE_OFFSET   (VPU_FW_HEADER_SIZE + FW_VERSION_HEADER_SIZE)
+
+#define WATCHDOG_MSS_REDIRECT  32
+#define WATCHDOG_NCE_REDIRECT  33
+
+#define ADDR_TO_L2_CACHE_CFG(addr) ((addr) >> 31)
+
+#define IVPU_FW_CHECK_API(vdev, fw_hdr, name) ivpu_fw_check_api(vdev, fw_hdr, #name, \
+                                                                 VPU_##name##_API_VER_INDEX, \
+                                                                 VPU_##name##_API_VER_MAJOR, \
+                                                                 VPU_##name##_API_VER_MINOR)
+
+static char *ivpu_firmware;
+module_param_named_unsafe(firmware, ivpu_firmware, charp, 0644);
+MODULE_PARM_DESC(firmware, "VPU firmware binary in /lib/firmware/..");
+
+static int ivpu_fw_request(struct ivpu_device *vdev)
+{
+       static const char * const fw_names[] = {
+               "mtl_vpu.bin",
+               "intel/vpu/mtl_vpu_v0.0.bin"
+       };
+       int ret = -ENOENT;
+       int i;
+
+       if (ivpu_firmware)
+               return request_firmware(&vdev->fw->file, ivpu_firmware, vdev->drm.dev);
+
+       for (i = 0; i < ARRAY_SIZE(fw_names); i++) {
+               ret = firmware_request_nowarn(&vdev->fw->file, fw_names[i], vdev->drm.dev);
+               if (!ret)
+                       return 0;
+       }
+
+       ivpu_err(vdev, "Failed to request firmware: %d\n", ret);
+       return ret;
+}
+
+static void
+ivpu_fw_check_api(struct ivpu_device *vdev, const struct vpu_firmware_header *fw_hdr,
+                 const char *str, int index, u16 expected_major, u16 expected_minor)
+{
+       u16 major = (u16)(fw_hdr->api_version[index] >> 16);
+       u16 minor = (u16)(fw_hdr->api_version[index]);
+
+       if (major != expected_major) {
+               ivpu_warn(vdev, "Incompatible FW %s API version: %d.%d (expected %d.%d)\n",
+                         str, major, minor, expected_major, expected_minor);
+       }
+       ivpu_dbg(vdev, FW_BOOT, "FW %s API version: %d.%d (expected %d.%d)\n",
+                str, major, minor, expected_major, expected_minor);
+}
+
+static int ivpu_fw_parse(struct ivpu_device *vdev)
+{
+       struct ivpu_fw_info *fw = vdev->fw;
+       const struct vpu_firmware_header *fw_hdr = (const void *)fw->file->data;
+       u64 runtime_addr, image_load_addr, runtime_size, image_size;
+
+       if (fw->file->size <= FW_FILE_IMAGE_OFFSET) {
+               ivpu_err(vdev, "Firmware file is too small: %zu\n", fw->file->size);
+               return -EINVAL;
+       }
+
+       if (fw_hdr->header_version != VPU_FW_HEADER_VERSION) {
+               ivpu_err(vdev, "Invalid firmware header version: %u\n", fw_hdr->header_version);
+               return -EINVAL;
+       }
+
+       runtime_addr = fw_hdr->boot_params_load_address;
+       runtime_size = fw_hdr->runtime_size;
+       image_load_addr = fw_hdr->image_load_address;
+       image_size = fw_hdr->image_size;
+
+       if (runtime_addr < FW_RUNTIME_MIN_ADDR || runtime_addr > FW_RUNTIME_MAX_ADDR) {
+               ivpu_err(vdev, "Invalid firmware runtime address: 0x%llx\n", runtime_addr);
+               return -EINVAL;
+       }
+
+       if (runtime_size < fw->file->size || runtime_size > FW_RUNTIME_MAX_SIZE) {
+               ivpu_err(vdev, "Invalid firmware runtime size: %llu\n", runtime_size);
+               return -EINVAL;
+       }
+
+       if (FW_FILE_IMAGE_OFFSET + image_size > fw->file->size) {
+               ivpu_err(vdev, "Invalid image size: %llu\n", image_size);
+               return -EINVAL;
+       }
+
+       if (image_load_addr < runtime_addr ||
+           image_load_addr + image_size > runtime_addr + runtime_size) {
+               ivpu_err(vdev, "Invalid firmware load address size: 0x%llx and size %llu\n",
+                        image_load_addr, image_size);
+               return -EINVAL;
+       }
+
+       if (fw_hdr->shave_nn_fw_size > FW_SHAVE_NN_MAX_SIZE) {
+               ivpu_err(vdev, "SHAVE NN firmware is too big: %u\n", fw_hdr->shave_nn_fw_size);
+               return -EINVAL;
+       }
+
+       if (fw_hdr->entry_point < image_load_addr ||
+           fw_hdr->entry_point >= image_load_addr + image_size) {
+               ivpu_err(vdev, "Invalid entry point: 0x%llx\n", fw_hdr->entry_point);
+               return -EINVAL;
+       }
+
+       fw->runtime_addr = runtime_addr;
+       fw->runtime_size = runtime_size;
+       fw->image_load_offset = image_load_addr - runtime_addr;
+       fw->image_size = image_size;
+       fw->shave_nn_size = PAGE_ALIGN(fw_hdr->shave_nn_fw_size);
+
+       fw->cold_boot_entry_point = fw_hdr->entry_point;
+       fw->entry_point = fw->cold_boot_entry_point;
+
+       ivpu_dbg(vdev, FW_BOOT, "Header version: 0x%x, format 0x%x\n",
+                fw_hdr->header_version, fw_hdr->image_format);
+       ivpu_dbg(vdev, FW_BOOT, "Size: file %lu image %u runtime %u shavenn %u\n",
+                fw->file->size, fw->image_size, fw->runtime_size, fw->shave_nn_size);
+       ivpu_dbg(vdev, FW_BOOT, "Address: runtime 0x%llx, load 0x%llx, entry point 0x%llx\n",
+                fw->runtime_addr, image_load_addr, fw->entry_point);
+       ivpu_dbg(vdev, FW_BOOT, "FW version: %s\n", (char *)fw_hdr + VPU_FW_HEADER_SIZE);
+
+       IVPU_FW_CHECK_API(vdev, fw_hdr, BOOT);
+       IVPU_FW_CHECK_API(vdev, fw_hdr, JSM);
+
+       return 0;
+}
+
+static void ivpu_fw_release(struct ivpu_device *vdev)
+{
+       release_firmware(vdev->fw->file);
+}
+
+static int ivpu_fw_update_global_range(struct ivpu_device *vdev)
+{
+       struct ivpu_fw_info *fw = vdev->fw;
+       u64 start = ALIGN(fw->runtime_addr + fw->runtime_size, FW_SHARED_MEM_ALIGNMENT);
+       u64 size = FW_SHARED_MEM_SIZE;
+
+       if (start + size > FW_GLOBAL_MEM_END) {
+               ivpu_err(vdev, "No space for shared region, start %lld, size %lld\n", start, size);
+               return -EINVAL;
+       }
+
+       ivpu_hw_init_range(&vdev->hw->ranges.global_low, start, size);
+       return 0;
+}
+
+static int ivpu_fw_mem_init(struct ivpu_device *vdev)
+{
+       struct ivpu_fw_info *fw = vdev->fw;
+       int ret;
+
+       ret = ivpu_fw_update_global_range(vdev);
+       if (ret)
+               return ret;
+
+       fw->mem = ivpu_bo_alloc_internal(vdev, fw->runtime_addr, fw->runtime_size, DRM_IVPU_BO_WC);
+       if (!fw->mem) {
+               ivpu_err(vdev, "Failed to allocate firmware runtime memory\n");
+               return -ENOMEM;
+       }
+
+       if (fw->shave_nn_size) {
+               fw->mem_shave_nn = ivpu_bo_alloc_internal(vdev, vdev->hw->ranges.global_high.start,
+                                                         fw->shave_nn_size, DRM_IVPU_BO_UNCACHED);
+               if (!fw->mem_shave_nn) {
+                       ivpu_err(vdev, "Failed to allocate shavenn buffer\n");
+                       ivpu_bo_free_internal(fw->mem);
+                       return -ENOMEM;
+               }
+       }
+
+       return 0;
+}
+
+static void ivpu_fw_mem_fini(struct ivpu_device *vdev)
+{
+       struct ivpu_fw_info *fw = vdev->fw;
+
+       if (fw->mem_shave_nn) {
+               ivpu_bo_free_internal(fw->mem_shave_nn);
+               fw->mem_shave_nn = NULL;
+       }
+
+       ivpu_bo_free_internal(fw->mem);
+       fw->mem = NULL;
+}
+
+int ivpu_fw_init(struct ivpu_device *vdev)
+{
+       int ret;
+
+       ret = ivpu_fw_request(vdev);
+       if (ret)
+               return ret;
+
+       ret = ivpu_fw_parse(vdev);
+       if (ret)
+               goto err_fw_release;
+
+       ret = ivpu_fw_mem_init(vdev);
+       if (ret)
+               goto err_fw_release;
Quoted 8 lines

drivers/accel/ivpu/ivpu_fw.c line 237

Quoted 5 lines
drivers/accel/ivpu/ivpu_fw.c new file +5 -0
+
+       return 0;
+
+err_fw_release:
+       ivpu_fw_release(vdev);

Why do you release the firmware only on error ?
Why maintain the hold on the firmware file for as long as the device lives ? Can't you release the file once you loaded the firmware to the device's memory ?

Quoted 7 lines

Yes, we keep the fw image in memory for the following reasons:
1. It reduces the time of reset, recovery and resume without warmboot, where we need to overwite the fw.
2. It prevents user space from changing the fw binary while the module is loaded.
If a new version of the fw was installed it will be used on the next insmod which prevents hard to detect bugs.

Regards,
Jacek

ok, sounds fair enough.
So you can add my r-b to this patch as well.
Oded

Reviewed-by on v5 5/7
#19 lore raw

Re: [PATCH v5 5/7] accel/ivpu: Implement firmware parsing and booting

In reply to Jacek Lawrynowicz
Quoted 7 lines · Jacek Lawrynowicz

Read, parse and boot VPU firmware image.

#20 lore raw

PATCH v5 6/7 accel/ivpu: 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 ivpu_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/accel/ivpu/Makefile   |   1 +
 drivers/accel/ivpu/ivpu_drv.c |  34 +-
 drivers/accel/ivpu/ivpu_drv.h |   5 +
 drivers/accel/ivpu/ivpu_gem.c |  26 ++
 drivers/accel/ivpu/ivpu_gem.h |   1 +
 drivers/accel/ivpu/ivpu_job.c | 603 ++++++++++++++++++++++++++++++++++
 drivers/accel/ivpu/ivpu_job.h |  67 ++++
 include/uapi/drm/ivpu_accel.h |  92 ++++++
 8 files changed, 825 insertions(+), 4 deletions(-)
 create mode 100644 drivers/accel/ivpu/ivpu_job.c
 create mode 100644 drivers/accel/ivpu/ivpu_job.h
drivers/accel/ivpu/Makefile +1 -0
index 9fa6a76e9d79..e61ece53a9ae 100644
@@ -7,6 +7,7 @@ intel_vpu-y := \
 	ivpu_gem.o \
 	ivpu_hw_mtl.o \
 	ivpu_ipc.o \
+	ivpu_job.o \
 	ivpu_jsm_msg.o \
 	ivpu_mmu.o \
 	ivpu_mmu_context.o
drivers/accel/ivpu/ivpu_drv.c +30 -4
index 53e103f64832..279ca15bff15 100644
@@ -20,6 +20,7 @@
 #include "ivpu_gem.h"
 #include "ivpu_hw.h"
 #include "ivpu_ipc.h"
+#include "ivpu_job.h"
 #include "ivpu_jsm_msg.h"
 #include "ivpu_mmu.h"
 #include "ivpu_mmu_context.h"
@@ -31,6 +32,8 @@
 
 static const struct drm_driver driver;
 
+static struct lock_class_key submitted_jobs_xa_lock_class_key;
+
 int ivpu_dbg_mask;
 module_param_named(dbg_mask, ivpu_dbg_mask, int, 0644);
 MODULE_PARM_DESC(dbg_mask, "Driver debug mask. See IVPU_DBG_* macros.");
@@ -84,8 +87,11 @@ static void file_priv_release(struct kref *ref)
 
 	ivpu_dbg(vdev, FILE, "file_priv release: ctx %u\n", file_priv->ctx.id);
 
+	ivpu_cmdq_release_all(file_priv);
+	ivpu_bo_remove_all_bos_from_context(&file_priv->ctx);
 	ivpu_mmu_user_context_fini(vdev, &file_priv->ctx);
 	drm_WARN_ON(&vdev->drm, xa_erase_irq(&vdev->context_xa, file_priv->ctx.id) != file_priv);
+	mutex_destroy(&file_priv->lock);
 	kfree(file_priv);
 }
 
@@ -209,10 +215,11 @@ static int ivpu_open(struct drm_device *dev, struct drm_file *file)
 	file_priv->vdev = vdev;
 	file_priv->priority = DRM_IVPU_CONTEXT_PRIORITY_NORMAL;
 	kref_init(&file_priv->ref);
+	mutex_init(&file_priv->lock);
 
 	ret = ivpu_mmu_user_context_init(vdev, &file_priv->ctx, ctx_id);
 	if (ret)
-		goto err_free_file_priv;
+		goto err_mutex_destroy;
 
 	old = xa_store_irq(&vdev->context_xa, ctx_id, file_priv, GFP_KERNEL);
 	if (xa_is_err(old)) {
@@ -229,7 +236,8 @@ static int ivpu_open(struct drm_device *dev, struct drm_file *file)
 
 err_ctx_fini:
 	ivpu_mmu_user_context_fini(vdev, &file_priv->ctx);
-err_free_file_priv:
+err_mutex_destroy:
+	mutex_destroy(&file_priv->lock);
 	kfree(file_priv);
 err_xa_erase:
 	xa_erase_irq(&vdev->context_xa, ctx_id);
@@ -253,6 +261,8 @@ static const struct drm_ioctl_desc ivpu_drm_ioctls[] = {
 	DRM_IOCTL_DEF_DRV(IVPU_BO_CREATE, ivpu_bo_create_ioctl, 0),
 	DRM_IOCTL_DEF_DRV(IVPU_BO_INFO, ivpu_bo_info_ioctl, 0),
 	DRM_IOCTL_DEF_DRV(IVPU_BO_USERPTR, ivpu_bo_userptr_ioctl, 0),
+	DRM_IOCTL_DEF_DRV(IVPU_SUBMIT, ivpu_submit_ioctl, 0),
+	DRM_IOCTL_DEF_DRV(IVPU_BO_WAIT, ivpu_bo_wait_ioctl, 0),
 };
 
 static int ivpu_wait_for_ready(struct ivpu_device *vdev)
@@ -465,6 +475,8 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
 	vdev->context_xa_limit.max = IVPU_CONTEXT_LIMIT;
 	atomic64_set(&vdev->unique_id_counter, 0);
 	xa_init_flags(&vdev->context_xa, XA_FLAGS_ALLOC);
+	xa_init_flags(&vdev->submitted_jobs_xa, XA_FLAGS_ALLOC1);
+	lockdep_set_class(&vdev->submitted_jobs_xa.xa_lock, &submitted_jobs_xa_lock_class_key);
 
 	ret = ivpu_pci_init(vdev);
 	if (ret) {
@@ -516,20 +528,30 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
 		goto err_fw_fini;
 	}
 
+	ret = ivpu_job_done_thread_init(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to initialize job done thread: %d\n", ret);
+		goto err_ipc_fini;
+	}
+
 	ret = ivpu_fw_load(vdev);
 	if (ret) {
 		ivpu_err(vdev, "Failed to load firmware: %d\n", ret);
-		goto err_fw_fini;
+		goto err_job_done_thread_fini;
 	}
 
 	ret = ivpu_boot(vdev);
 	if (ret) {
 		ivpu_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:
+	ivpu_job_done_thread_fini(vdev);
+err_ipc_fini:
+	ivpu_ipc_fini(vdev);
 err_fw_fini:
 	ivpu_fw_fini(vdev);
 err_mmu_gctx_fini:
@@ -537,6 +559,7 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
 err_power_down:
 	ivpu_hw_power_down(vdev);
 err_xa_destroy:
+	xa_destroy(&vdev->submitted_jobs_xa);
 	xa_destroy(&vdev->context_xa);
 	return ret;
 }
@@ -544,10 +567,13 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
 static void ivpu_dev_fini(struct ivpu_device *vdev)
 {
 	ivpu_shutdown(vdev);
+	ivpu_job_done_thread_fini(vdev);
 	ivpu_ipc_fini(vdev);
 	ivpu_fw_fini(vdev);
 	ivpu_mmu_global_context_fini(vdev);
 
+	drm_WARN_ON(&vdev->drm, !xa_empty(&vdev->submitted_jobs_xa));
+	xa_destroy(&vdev->submitted_jobs_xa);
 	drm_WARN_ON(&vdev->drm, !xa_empty(&vdev->context_xa));
 	xa_destroy(&vdev->context_xa);
 }
drivers/accel/ivpu/ivpu_drv.h +5 -0
index 317ae61f43bd..2545a32fe910 100644
@@ -94,6 +94,9 @@ struct ivpu_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 {
@@ -111,6 +114,8 @@ struct ivpu_device {
 struct ivpu_file_priv {
 	struct kref ref;
 	struct ivpu_device *vdev;
+	struct mutex lock; /* Protects cmdq */
+	struct ivpu_cmdq *cmdq[IVPU_NUM_ENGINES];
 	struct ivpu_mmu_context ctx;
 	u32 priority;
 	bool has_mmu_faults;
drivers/accel/ivpu/ivpu_gem.c +26 -0
index f6d1937c798f..ec4248e21443 100644
@@ -771,6 +771,32 @@ int ivpu_bo_info_ioctl(struct drm_device *dev, void *data, struct drm_file *file
 	return ret;
 }
 
+int ivpu_bo_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
+{
+	struct drm_ivpu_bo_wait *args = data;
+	struct drm_gem_object *obj;
+	unsigned long timeout;
+	long ret;
+
+	timeout = drm_timeout_abs_to_jiffies(args->timeout_ns);
+
+	obj = drm_gem_object_lookup(file, args->handle);
+	if (!obj)
+		return -EINVAL;
+
+	ret = dma_resv_wait_timeout(obj->resv, DMA_RESV_USAGE_READ, true, timeout);
+	if (ret == 0) {
+		ret = -ETIMEDOUT;
+	} else if (ret > 0) {
+		ret = 0;
+		args->job_status = to_ivpu_bo(obj)->job_status;
+	}
+
+	drm_gem_object_put(obj);
+
+	return ret;
+}
+
 static void ivpu_bo_print_info(struct ivpu_bo *bo, struct drm_printer *p)
 {
 	unsigned long dma_refcount = 0;
drivers/accel/ivpu/ivpu_gem.h +1 -0
index d71e6fe207d4..e9af4aefdbcf 100644
@@ -30,6 +30,7 @@ struct ivpu_bo {
 	u32 handle;
 	u32 flags;
 	uintptr_t user_ptr;
+	u32 job_status;
 };
 
 enum ivpu_bo_type {
drivers/accel/ivpu/ivpu_job.c new file +603 -0

603 lines not on this page · raw message

drivers/accel/ivpu/ivpu_job.h new file +67 -0

67 lines not on this page · raw message

include/uapi/drm/ivpu_accel.h +92 -0

110 lines not on this page · raw message

Signature · 1 line
2.34.1
Reviewed-by on v5 6/7
#21 lore raw

Re: [PATCH v5 6/7] accel/ivpu: Add command buffer submission logic

Quoted 11 lines · 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 ivpu_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.

Quoted 8 lines

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.

#22 lore raw

PATCH v5 7/7 accel/ivpu: 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/accel/ivpu/Makefile      |   3 +-
 drivers/accel/ivpu/ivpu_drv.c    |  28 +++
 drivers/accel/ivpu/ivpu_drv.h    |   2 +
 drivers/accel/ivpu/ivpu_fw.c     |   4 +
 drivers/accel/ivpu/ivpu_hw_mtl.c |  12 ++
 drivers/accel/ivpu/ivpu_ipc.c    |   5 +-
 drivers/accel/ivpu/ivpu_job.c    |  13 +-
 drivers/accel/ivpu/ivpu_mmu.c    |   9 +-
 drivers/accel/ivpu/ivpu_pm.c     | 329 +++++++++++++++++++++++++++++++
 drivers/accel/ivpu/ivpu_pm.h     |  38 ++++
 10 files changed, 439 insertions(+), 4 deletions(-)
 create mode 100644 drivers/accel/ivpu/ivpu_pm.c
 create mode 100644 drivers/accel/ivpu/ivpu_pm.h
drivers/accel/ivpu/Makefile +2 -1
index e61ece53a9ae..80f1fb3548ae 100644
@@ -10,6 +10,7 @@ intel_vpu-y := \
 	ivpu_job.o \
 	ivpu_jsm_msg.o \
 	ivpu_mmu.o \
-	ivpu_mmu_context.o
+	ivpu_mmu_context.o \
+	ivpu_pm.o
 
 obj-$(CONFIG_DRM_ACCEL_IVPU) += intel_vpu.o
\ No newline at end of file
drivers/accel/ivpu/ivpu_drv.c +28 -0
index 279ca15bff15..b765f9260c90 100644
@@ -24,6 +24,7 @@
 #include "ivpu_jsm_msg.h"
 #include "ivpu_mmu.h"
 #include "ivpu_mmu_context.h"
+#include "ivpu_pm.h"
 
 #ifndef DRIVER_VERSION_STR
 #define DRIVER_VERSION_STR __stringify(DRM_IVPU_DRIVER_MAJOR) "." \
@@ -469,6 +470,10 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
 	if (!vdev->ipc)
 		return -ENOMEM;
 
+	vdev->pm = drmm_kzalloc(&vdev->drm, sizeof(*vdev->pm), GFP_KERNEL);
+	if (!vdev->pm)
+		return -ENOMEM;
+
 	vdev->hw->ops = &ivpu_hw_mtl_ops;
 	vdev->platform = IVPU_PLATFORM_INVALID;
 	vdev->context_xa_limit.min = IVPU_GLOBAL_CONTEXT_MMU_SSID + 1;
@@ -528,6 +533,12 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
 		goto err_fw_fini;
 	}
 
+	ret = ivpu_pm_init(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to initialize PM: %d\n", ret);
+		goto err_ipc_fini;
+	}
+
 	ret = ivpu_job_done_thread_init(vdev);
 	if (ret) {
 		ivpu_err(vdev, "Failed to initialize job done thread: %d\n", ret);
@@ -546,6 +557,8 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
 		goto err_job_done_thread_fini;
 	}
 
+	ivpu_pm_enable(vdev);
+
 	return 0;
 
 err_job_done_thread_fini:
@@ -566,6 +579,7 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
 
 static void ivpu_dev_fini(struct ivpu_device *vdev)
 {
+	ivpu_pm_disable(vdev);
 	ivpu_shutdown(vdev);
 	ivpu_job_done_thread_fini(vdev);
 	ivpu_ipc_fini(vdev);
@@ -618,11 +632,25 @@ static void ivpu_remove(struct pci_dev *pdev)
 	ivpu_dev_fini(vdev);
 }
 
+static const struct dev_pm_ops ivpu_drv_pci_pm = {
+	SET_SYSTEM_SLEEP_PM_OPS(ivpu_pm_suspend_cb, ivpu_pm_resume_cb)
+	SET_RUNTIME_PM_OPS(ivpu_pm_runtime_suspend_cb, ivpu_pm_runtime_resume_cb, NULL)
+};
+
+static const struct pci_error_handlers ivpu_drv_pci_err = {
+	.reset_prepare = ivpu_pm_reset_prepare_cb,
+	.reset_done = ivpu_pm_reset_done_cb,
+};
+
 static struct pci_driver ivpu_pci_driver = {
 	.name = KBUILD_MODNAME,
 	.id_table = ivpu_pci_ids,
 	.probe = ivpu_probe,
 	.remove = ivpu_remove,
+	.driver = {
+		.pm = &ivpu_drv_pci_pm,
+	},
+	.err_handler = &ivpu_drv_pci_err,
 };
 
 module_pci_driver(ivpu_pci_driver);
drivers/accel/ivpu/ivpu_drv.h +2 -0
index 2545a32fe910..51b7dae3cd17 100644
@@ -76,6 +76,7 @@ struct ivpu_hw_info;
 struct ivpu_mmu_info;
 struct ivpu_fw_info;
 struct ivpu_ipc_info;
+struct ivpu_pm_info;
 
 struct ivpu_device {
 	struct drm_device drm;
@@ -89,6 +90,7 @@ struct ivpu_device {
 	struct ivpu_mmu_info *mmu;
 	struct ivpu_fw_info *fw;
 	struct ivpu_ipc_info *ipc;
+	struct ivpu_pm_info *pm;
 
 	struct ivpu_mmu_context gctx;
 	struct xarray context_xa;
drivers/accel/ivpu/ivpu_fw.c +4 -0
index 4baa0767a10d..b463c24adb70 100644
@@ -14,6 +14,7 @@
 #include "ivpu_gem.h"
 #include "ivpu_hw.h"
 #include "ivpu_ipc.h"
+#include "ivpu_pm.h"
 
 #define FW_GLOBAL_MEM_START	(2ull * SZ_1G)
 #define FW_GLOBAL_MEM_END	(3ull * SZ_1G)
@@ -361,9 +362,12 @@ void ivpu_fw_boot_params_setup(struct ivpu_device *vdev, struct vpu_boot_params
 	/* In case of warm boot we only have to reset the entrypoint addr */
 	if (!ivpu_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 = ivpu_hw_reg_pll_freq_get(vdev);
drivers/accel/ivpu/ivpu_hw_mtl.c +12 -0
index 55caf5479f5f..b59b1f472b40 100644
@@ -10,6 +10,7 @@
 #include "ivpu_hw.h"
 #include "ivpu_ipc.h"
 #include "ivpu_mmu.h"
+#include "ivpu_pm.h"
 
 #define TILE_FUSE_ENABLE_BOTH	     0x0
 #define TILE_FUSE_ENABLE_UPPER	     0x1
@@ -921,6 +922,8 @@ static void ivpu_hw_mtl_irq_disable(struct ivpu_device *vdev)
 static void ivpu_hw_mtl_irq_wdt_nce_handler(struct ivpu_device *vdev)
 {
 	ivpu_err_ratelimited(vdev, "WDT NCE irq\n");
+
+	ivpu_pm_schedule_recovery(vdev);
 }
 
 static void ivpu_hw_mtl_irq_wdt_mss_handler(struct ivpu_device *vdev)
@@ -928,11 +931,14 @@ static void ivpu_hw_mtl_irq_wdt_mss_handler(struct ivpu_device *vdev)
 	ivpu_err_ratelimited(vdev, "WDT MSS irq\n");
 
 	ivpu_hw_wdt_disable(vdev);
+	ivpu_pm_schedule_recovery(vdev);
 }
 
 static void ivpu_hw_mtl_irq_noc_firewall_handler(struct ivpu_device *vdev)
 {
 	ivpu_err_ratelimited(vdev, "NOC Firewall irq\n");
+
+	ivpu_pm_schedule_recovery(vdev);
 }
 
 /* Handler for IRQs from VPU core (irqV) */
@@ -970,6 +976,7 @@ static u32 ivpu_hw_mtl_irqv_handler(struct ivpu_device *vdev, int irq)
 static u32 ivpu_hw_mtl_irqb_handler(struct ivpu_device *vdev, int irq)
 {
 	u32 status = REGB_RD32(MTL_BUTTRESS_INTERRUPT_STAT) & BUTTRESS_IRQ_MASK;
+	bool schedule_recovery = false;
 
 	if (status == 0)
 		return 0;
@@ -983,6 +990,7 @@ static u32 ivpu_hw_mtl_irqb_handler(struct ivpu_device *vdev, int irq)
 	if (REG_TEST_FLD(MTL_BUTTRESS_INTERRUPT_STAT, ATS_ERR, status)) {
 		ivpu_err(vdev, "ATS_ERR irq 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)) {
@@ -993,6 +1001,7 @@ static u32 ivpu_hw_mtl_irqb_handler(struct ivpu_device *vdev, int irq)
 			 REG_GET_FLD(MTL_BUTTRESS_UFI_ERR_LOG, AXI_ID, ufi_log),
 			 REG_GET_FLD(MTL_BUTTRESS_UFI_ERR_LOG, CQ_ID, ufi_log));
 		REGB_WR32(MTL_BUTTRESS_UFI_ERR_CLEAR, 0x1);
+		schedule_recovery = true;
 	}
 
 	/*
@@ -1005,6 +1014,9 @@ static u32 ivpu_hw_mtl_irqb_handler(struct ivpu_device *vdev, int irq)
 	/* Re-enable global interrupt */
 	REGB_WR32(MTL_BUTTRESS_GLOBAL_INT_MASK, 0x0);
 
+	if (schedule_recovery)
+		ivpu_pm_schedule_recovery(vdev);
+
 	return status;
 }
 
drivers/accel/ivpu/ivpu_ipc.c +4 -1
index 4c477743189a..9970e25ec2f7 100644
@@ -14,6 +14,7 @@
 #include "ivpu_hw_reg_io.h"
 #include "ivpu_ipc.h"
 #include "ivpu_jsm_msg.h"
+#include "ivpu_pm.h"
 
 #define IPC_MAX_RX_MSG	128
 #define IS_KTHREAD()	(get_current()->flags & PF_KTHREAD)
@@ -307,8 +308,10 @@ int ivpu_ipc_send_receive(struct ivpu_device *vdev, struct vpu_jsm_msg *req,
 
 	ret = ivpu_ipc_send_receive_internal(vdev, &hb_req, VPU_JSM_MSG_QUERY_ENGINE_HB_DONE,
 					     &hb_resp, VPU_IPC_CHAN_ASYNC_CMD, vdev->timeout.jsm);
-	if (ret == -ETIMEDOUT)
+	if (ret == -ETIMEDOUT) {
 		ivpu_hw_diagnose_failure(vdev);
+		ivpu_pm_schedule_recovery(vdev);
+	}
 
 rpm_put:
 	ivpu_rpm_put(vdev);
drivers/accel/ivpu/ivpu_job.c +12 -1
index 41181e3e4e3f..3276bd9107b4 100644
@@ -17,6 +17,7 @@
 #include "ivpu_ipc.h"
 #include "ivpu_job.h"
 #include "ivpu_jsm_msg.h"
+#include "ivpu_pm.h"
 
 #define CMD_BUF_IDX	     0
 #define JOB_ID_JOB_MASK	     GENMASK(7, 0)
@@ -270,6 +271,9 @@ static void job_release(struct kref *ref)
 
 	ivpu_dbg(vdev, KREF, "Job released: id %u\n", job->job_id);
 	kfree(job);
+
+	/* Allow the VPU to get suspended, must be called after ivpu_file_priv_put() */
+	ivpu_rpm_put(vdev);
 }
 
 static void job_put(struct ivpu_job *job)
@@ -286,11 +290,16 @@ ivpu_create_job(struct ivpu_file_priv *file_priv, u32 engine_idx, u32 bo_count)
 	struct ivpu_device *vdev = file_priv->vdev;
 	struct ivpu_job *job;
 	size_t buf_size;
+	int ret;
+
+	ret = ivpu_rpm_get(vdev);
+	if (ret < 0)
+		return NULL;
 
 	buf_size = sizeof(*job) + bo_count * sizeof(struct ivpu_bo *);
 	job = kzalloc(buf_size, GFP_KERNEL);
 	if (!job)
-		return NULL;
+		goto err_rpm_put;
 
 	kref_init(&job->ref);
 
@@ -311,6 +320,8 @@ ivpu_create_job(struct ivpu_file_priv *file_priv, u32 engine_idx, u32 bo_count)
 
 err_free_job:
 	kfree(job);
+err_rpm_put:
+	ivpu_rpm_put(vdev);
 	return NULL;
 }
 
drivers/accel/ivpu/ivpu_mmu.c +8 -1
index ff5ef6da1fd3..694e978aba66 100644
@@ -11,6 +11,7 @@
 #include "ivpu_hw_reg_io.h"
 #include "ivpu_mmu.h"
 #include "ivpu_mmu_context.h"
+#include "ivpu_pm.h"
 
 #define IVPU_MMU_IDR0_REF		0x080f3e0f
 #define IVPU_MMU_IDR0_REF_SIMICS	0x080f3e1f
@@ -814,6 +815,7 @@ static u32 *ivpu_mmu_get_event(struct ivpu_device *vdev)
 
 void ivpu_mmu_irq_evtq_handler(struct ivpu_device *vdev)
 {
+	bool schedule_recovery = false;
 	u32 *event;
 	u32 ssid;
 
@@ -823,9 +825,14 @@ void ivpu_mmu_irq_evtq_handler(struct ivpu_device *vdev)
 		ivpu_mmu_dump_event(vdev, event);
 
 		ssid = FIELD_GET(IVPU_MMU_EVT_SSID_MASK, event[0]);
-		if (ssid != IVPU_GLOBAL_CONTEXT_MMU_SSID)
+		if (ssid == IVPU_GLOBAL_CONTEXT_MMU_SSID)
+			schedule_recovery = true;
+		else
 			ivpu_mmu_user_context_mark_invalid(vdev, ssid);
 	}
+
+	if (schedule_recovery)
+		ivpu_pm_schedule_recovery(vdev);
 }
 
 void ivpu_mmu_irq_gerr_handler(struct ivpu_device *vdev)
drivers/accel/ivpu/ivpu_pm.c new file +329 -0

329 lines not on this page · raw message

drivers/accel/ivpu/ivpu_pm.h new file +38 -0
new file mode 100644
index 000000000000..dc1b3758e13f
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2020-2023 Intel Corporation
+ */
+
+#ifndef __IVPU_PM_H__
+#define __IVPU_PM_H__
+
+#include <linux/types.h>
+
+struct ivpu_device;
+
+struct ivpu_pm_info {
+	struct ivpu_device *vdev;
+	struct work_struct recovery_work;
+	atomic_t in_reset;
+	bool is_warmboot;
+	u32 suspend_reschedule_counter;
+};
+
+int ivpu_pm_init(struct ivpu_device *vdev);
+void ivpu_pm_enable(struct ivpu_device *vdev);
+void ivpu_pm_disable(struct ivpu_device *vdev);
+
+int ivpu_pm_suspend_cb(struct device *dev);
+int ivpu_pm_resume_cb(struct device *dev);
+int ivpu_pm_runtime_suspend_cb(struct device *dev);
+int ivpu_pm_runtime_resume_cb(struct device *dev);
+
+void ivpu_pm_reset_prepare_cb(struct pci_dev *pdev);
+void ivpu_pm_reset_done_cb(struct pci_dev *pdev);
+
+int __must_check ivpu_rpm_get(struct ivpu_device *vdev);
+void ivpu_rpm_put(struct ivpu_device *vdev);
+
+void ivpu_pm_schedule_recovery(struct ivpu_device *vdev);
+
+#endif /* __IVPU_PM_H__ */
Signature · 1 line
2.34.1
Reviewed-by on v5 7/7
#23 lore raw

Re: [PATCH v5 7/7] accel/ivpu: Add PM support

Quoted 7 lines · Jacek Lawrynowicz

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

I don't claim to be a PM expert, but I have had some experience with it.
This looks sane to me.

Reviewed-by on the v5 series
#24 lore raw
In reply to Jacek Lawrynowicz
Quoted 16 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.

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 was open sourced in December 2022 and it is available at:
https://github.com/intel/linux-vpu-driver

Quoted 7 lines

The firmware for the VPU will be distributed as a closed source binary.

There are no major chcnages in this version of the patchset. Detailed changes are listed below.

Regards,
Jacek

Hi Jacek,
For patches 1-4 and 6 (assuming you fix the small issue in patch 3) you can add

For patch 5 I had a comment/question, please reply to it. Once we sort that thing that patch is also r-b by me.

Patch 7 - I prefer someone else who knows about PM to look at it. In habana we don't support power management (in the driver) so I have never dealt with that and therefore I have no idea if you use the kapi correctly.

Thanks,
Oded

Quoted 113 lines

Changelog

v5:
- Rename ivpu_drm.h to ivpu_accel.h
- Cleanup ivpu_mmu_config_check()
- Optimize locking in ivpu_mmu_cd_add()
- Invalidate user context if it has MMU faults
- Move ivpu_ipc_match_consumer() outside ivpu_ipc_dispatch()

v4: https://lore.kernel.org/all/[email protected]/
- Switch to the accel framework (DRIVER_COMPUTE_ACCEL)
- Move the driver from drivers/gpu/drm to drivers/accel
- Rename kconfig DRM_IVPU option to DRM_ACCEL_IVPU and update dependencies
- Create context on open() instead of lazy allocating it
- Remove status_offset from submit ioctl, as status is now reported in bo_wait ioctl
- Use managed resources in ivpu_drv.c
- Optimize locking in ivpu_ipc.c - add new rx_msg_lock for consumer msg list
- Refactor vpu_hw_mtl_reg.h to use BIT_MASK() and GENMASK() macros
- Use module_pci_driver() for mudule init
- Remove mutex from "struct ivpu_pm_info"
- Add explicit "vdev" arg to ivpu_dbg()
- Use drm_WARN_ON() instead of WARN_ON() where possible
- Add comments for boot related functions
- Update firmware API headers

v3: https://lore.kernel.org/all/[email protected]/
- Fixed alignment warning in ivpu_ipc.c when building with W=1

v2: https://lore.kernel.org/all/[email protected]/
- Rename the driver from "drm/vpu" to "drm/ivpu"
- Add a TODO file
- Add support for WC buffers

v1: https://lore.kernel.org/all/[email protected]/

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

 MAINTAINERS                           |    9 +
 drivers/Makefile                      |    1 +
 drivers/accel/Kconfig                 |    2 +
 drivers/accel/Makefile                |    3 +
 drivers/accel/ivpu/Kconfig            |   15 +
 drivers/accel/ivpu/Makefile           |   16 +
 drivers/accel/ivpu/TODO               |    9 +
 drivers/accel/ivpu/ivpu_drv.c         |  661 +++++++++++++++
 drivers/accel/ivpu/ivpu_drv.h         |  190 +++++
 drivers/accel/ivpu/ivpu_fw.c          |  423 ++++++++++
 drivers/accel/ivpu/ivpu_fw.h          |   38 +
 drivers/accel/ivpu/ivpu_gem.c         |  846 +++++++++++++++++++
 drivers/accel/ivpu/ivpu_gem.h         |  129 +++
 drivers/accel/ivpu/ivpu_hw.h          |  170 ++++
 drivers/accel/ivpu/ivpu_hw_mtl.c      | 1084 +++++++++++++++++++++++++
 drivers/accel/ivpu/ivpu_hw_mtl_reg.h  |  280 +++++++
 drivers/accel/ivpu/ivpu_hw_reg_io.h   |  115 +++
 drivers/accel/ivpu/ivpu_ipc.c         |  508 ++++++++++++
 drivers/accel/ivpu/ivpu_ipc.h         |   93 +++
 drivers/accel/ivpu/ivpu_job.c         |  614 ++++++++++++++
 drivers/accel/ivpu/ivpu_job.h         |   67 ++
 drivers/accel/ivpu/ivpu_jsm_msg.c     |  170 ++++
 drivers/accel/ivpu/ivpu_jsm_msg.h     |   23 +
 drivers/accel/ivpu/ivpu_mmu.c         |  883 ++++++++++++++++++++
 drivers/accel/ivpu/ivpu_mmu.h         |   50 ++
 drivers/accel/ivpu/ivpu_mmu_context.c |  398 +++++++++
 drivers/accel/ivpu/ivpu_mmu_context.h |   50 ++
 drivers/accel/ivpu/ivpu_pm.c          |  329 ++++++++
 drivers/accel/ivpu/ivpu_pm.h          |   38 +
 drivers/accel/ivpu/vpu_boot_api.h     |  349 ++++++++
 drivers/accel/ivpu/vpu_jsm_api.h      |  999 +++++++++++++++++++++++
 include/uapi/drm/ivpu_accel.h         |  339 ++++++++
 32 files changed, 8901 insertions(+)
 create mode 100644 drivers/accel/Makefile
 create mode 100644 drivers/accel/ivpu/Kconfig
 create mode 100644 drivers/accel/ivpu/Makefile
 create mode 100644 drivers/accel/ivpu/TODO
 create mode 100644 drivers/accel/ivpu/ivpu_drv.c
 create mode 100644 drivers/accel/ivpu/ivpu_drv.h
 create mode 100644 drivers/accel/ivpu/ivpu_fw.c
 create mode 100644 drivers/accel/ivpu/ivpu_fw.h
 create mode 100644 drivers/accel/ivpu/ivpu_gem.c
 create mode 100644 drivers/accel/ivpu/ivpu_gem.h
 create mode 100644 drivers/accel/ivpu/ivpu_hw.h
 create mode 100644 drivers/accel/ivpu/ivpu_hw_mtl.c
 create mode 100644 drivers/accel/ivpu/ivpu_hw_mtl_reg.h
 create mode 100644 drivers/accel/ivpu/ivpu_hw_reg_io.h
 create mode 100644 drivers/accel/ivpu/ivpu_ipc.c
 create mode 100644 drivers/accel/ivpu/ivpu_ipc.h
 create mode 100644 drivers/accel/ivpu/ivpu_job.c
 create mode 100644 drivers/accel/ivpu/ivpu_job.h
 create mode 100644 drivers/accel/ivpu/ivpu_jsm_msg.c
 create mode 100644 drivers/accel/ivpu/ivpu_jsm_msg.h
 create mode 100644 drivers/accel/ivpu/ivpu_mmu.c
 create mode 100644 drivers/accel/ivpu/ivpu_mmu.h
 create mode 100644 drivers/accel/ivpu/ivpu_mmu_context.c
 create mode 100644 drivers/accel/ivpu/ivpu_mmu_context.h
 create mode 100644 drivers/accel/ivpu/ivpu_pm.c
 create mode 100644 drivers/accel/ivpu/ivpu_pm.h
 create mode 100644 drivers/accel/ivpu/vpu_boot_api.h
 create mode 100644 drivers/accel/ivpu/vpu_jsm_api.h
 create mode 100644 include/uapi/drm/ivpu_accel.h

base-commit: 03a0a1040895711e12c15ab28d4d1812928e171d
prerequisite-patch-id: 1283b537f4791511769ad3446d0eafd98193e86f --
2.34.1

#25 lore raw
Quoted 30 lines · Oded Gabbay
Quoted 16 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.

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 was open sourced in December 2022 and it is available at:
https://github.com/intel/linux-vpu-driver

Quoted 7 lines

The firmware for the VPU will be distributed as a closed source binary.

There are no major chcnages in this version of the patchset. Detailed changes are listed below.

Regards,
Jacek

Hi Jacek,
For patches 1-4 and 6 (assuming you fix the small issue in patch 3) you can add

Quoted 7 lines

For patch 5 I had a comment/question, please reply to it. Once we sort that thing that patch is also r-b by me.

Patch 7 - I prefer someone else who knows about PM to look at it. In habana we don't support power management (in the driver) so I have never dealt with that and therefore I have no idea if you use the kapi correctly.

Usually the hard part is deadlocks between runtime_resume calls and the rpm suspend callback. I'm not sure whether lockdep catches these I'm not sure whether lockdep catches these.

From a very quick look it looks reasonable.
-Daniel

Quoted 118 lines

Thanks,
Oded

Quoted 113 lines

Changelog

v5:
- Rename ivpu_drm.h to ivpu_accel.h
- Cleanup ivpu_mmu_config_check()
- Optimize locking in ivpu_mmu_cd_add()
- Invalidate user context if it has MMU faults
- Move ivpu_ipc_match_consumer() outside ivpu_ipc_dispatch()

v4: https://lore.kernel.org/all/[email protected]/
- Switch to the accel framework (DRIVER_COMPUTE_ACCEL)
- Move the driver from drivers/gpu/drm to drivers/accel
- Rename kconfig DRM_IVPU option to DRM_ACCEL_IVPU and update dependencies
- Create context on open() instead of lazy allocating it
- Remove status_offset from submit ioctl, as status is now reported in bo_wait ioctl
- Use managed resources in ivpu_drv.c
- Optimize locking in ivpu_ipc.c - add new rx_msg_lock for consumer msg list
- Refactor vpu_hw_mtl_reg.h to use BIT_MASK() and GENMASK() macros
- Use module_pci_driver() for mudule init
- Remove mutex from "struct ivpu_pm_info"
- Add explicit "vdev" arg to ivpu_dbg()
- Use drm_WARN_ON() instead of WARN_ON() where possible
- Add comments for boot related functions
- Update firmware API headers

v3: https://lore.kernel.org/all/[email protected]/
- Fixed alignment warning in ivpu_ipc.c when building with W=1

v2: https://lore.kernel.org/all/[email protected]/
- Rename the driver from "drm/vpu" to "drm/ivpu"
- Add a TODO file
- Add support for WC buffers

v1: https://lore.kernel.org/all/[email protected]/

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

 MAINTAINERS                           |    9 +
 drivers/Makefile                      |    1 +
 drivers/accel/Kconfig                 |    2 +
 drivers/accel/Makefile                |    3 +
 drivers/accel/ivpu/Kconfig            |   15 +
 drivers/accel/ivpu/Makefile           |   16 +
 drivers/accel/ivpu/TODO               |    9 +
 drivers/accel/ivpu/ivpu_drv.c         |  661 +++++++++++++++
 drivers/accel/ivpu/ivpu_drv.h         |  190 +++++
 drivers/accel/ivpu/ivpu_fw.c          |  423 ++++++++++
 drivers/accel/ivpu/ivpu_fw.h          |   38 +
 drivers/accel/ivpu/ivpu_gem.c         |  846 +++++++++++++++++++
 drivers/accel/ivpu/ivpu_gem.h         |  129 +++
 drivers/accel/ivpu/ivpu_hw.h          |  170 ++++
 drivers/accel/ivpu/ivpu_hw_mtl.c      | 1084 +++++++++++++++++++++++++
 drivers/accel/ivpu/ivpu_hw_mtl_reg.h  |  280 +++++++
 drivers/accel/ivpu/ivpu_hw_reg_io.h   |  115 +++
 drivers/accel/ivpu/ivpu_ipc.c         |  508 ++++++++++++
 drivers/accel/ivpu/ivpu_ipc.h         |   93 +++
 drivers/accel/ivpu/ivpu_job.c         |  614 ++++++++++++++
 drivers/accel/ivpu/ivpu_job.h         |   67 ++
 drivers/accel/ivpu/ivpu_jsm_msg.c     |  170 ++++
 drivers/accel/ivpu/ivpu_jsm_msg.h     |   23 +
 drivers/accel/ivpu/ivpu_mmu.c         |  883 ++++++++++++++++++++
 drivers/accel/ivpu/ivpu_mmu.h         |   50 ++
 drivers/accel/ivpu/ivpu_mmu_context.c |  398 +++++++++
 drivers/accel/ivpu/ivpu_mmu_context.h |   50 ++
 drivers/accel/ivpu/ivpu_pm.c          |  329 ++++++++
 drivers/accel/ivpu/ivpu_pm.h          |   38 +
 drivers/accel/ivpu/vpu_boot_api.h     |  349 ++++++++
 drivers/accel/ivpu/vpu_jsm_api.h      |  999 +++++++++++++++++++++++
 include/uapi/drm/ivpu_accel.h         |  339 ++++++++
 32 files changed, 8901 insertions(+)
 create mode 100644 drivers/accel/Makefile
 create mode 100644 drivers/accel/ivpu/Kconfig
 create mode 100644 drivers/accel/ivpu/Makefile
 create mode 100644 drivers/accel/ivpu/TODO
 create mode 100644 drivers/accel/ivpu/ivpu_drv.c
 create mode 100644 drivers/accel/ivpu/ivpu_drv.h
 create mode 100644 drivers/accel/ivpu/ivpu_fw.c
 create mode 100644 drivers/accel/ivpu/ivpu_fw.h
 create mode 100644 drivers/accel/ivpu/ivpu_gem.c
 create mode 100644 drivers/accel/ivpu/ivpu_gem.h
 create mode 100644 drivers/accel/ivpu/ivpu_hw.h
 create mode 100644 drivers/accel/ivpu/ivpu_hw_mtl.c
 create mode 100644 drivers/accel/ivpu/ivpu_hw_mtl_reg.h
 create mode 100644 drivers/accel/ivpu/ivpu_hw_reg_io.h
 create mode 100644 drivers/accel/ivpu/ivpu_ipc.c
 create mode 100644 drivers/accel/ivpu/ivpu_ipc.h
 create mode 100644 drivers/accel/ivpu/ivpu_job.c
 create mode 100644 drivers/accel/ivpu/ivpu_job.h
 create mode 100644 drivers/accel/ivpu/ivpu_jsm_msg.c
 create mode 100644 drivers/accel/ivpu/ivpu_jsm_msg.h
 create mode 100644 drivers/accel/ivpu/ivpu_mmu.c
 create mode 100644 drivers/accel/ivpu/ivpu_mmu.h
 create mode 100644 drivers/accel/ivpu/ivpu_mmu_context.c
 create mode 100644 drivers/accel/ivpu/ivpu_mmu_context.h
 create mode 100644 drivers/accel/ivpu/ivpu_pm.c
 create mode 100644 drivers/accel/ivpu/ivpu_pm.h
 create mode 100644 drivers/accel/ivpu/vpu_boot_api.h
 create mode 100644 drivers/accel/ivpu/vpu_jsm_api.h
 create mode 100644 include/uapi/drm/ivpu_accel.h

base-commit: 03a0a1040895711e12c15ab28d4d1812928e171d
prerequisite-patch-id: 1283b537f4791511769ad3446d0eafd98193e86f --
2.34.1

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#26 lore raw
In reply to Jacek Lawrynowicz
Quoted 9 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.

Quoted 5 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 was open sourced in December 2022 and it is available at:
https://github.com/intel/linux-vpu-driver

I feel like I forgot to mention this earlier because I can't find a reference to it in my mails. I would like to see something in Documentation for this driver/device. Would be nice to get an overview of how it works (system block diagram?), how it is intended to be used, etc. Include relevant references. This would be a great place to document the UMD and the compiler (I am positive you have mentioned the compiler before, but I am currently failing to find a reference to it).

I feel that traditional DRM gets away from not needing this since their stuff is pretty well established. Everyone uses Mesa/igt and so how things are structured/used is fairly well implied. Accel is brand new and doesn't have that yet so I suspect we'll be well situated if we take the extra effort to spell out these things which might be just assumed elsewhere. Hopefully, over time, such documentation helps in identifying useful areas to build up the common code of the subsystem.

I can't justify holding up this series for this though. Please put it on some todo list :)

-Jeff

#27 lore raw

Hi,

Quoted 13 lines · Jeffrey Hugo
Quoted 12 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.

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

Quoted 8 lines
Quoted 2 lines

User mode driver was open sourced in December 2022 and it is available at:
https://github.com/intel/linux-vpu-driver

I feel like I forgot to mention this earlier because I can't find a reference to it in my mails.  I would like to see something in Documentation for this driver/device.  Would be nice to get an overview of how it works (system block diagram?), how it is intended to be used, etc.  Include relevant references.  This would be a great place to document the UMD and the compiler (I am positive you have mentioned the compiler before, but I am currently failing to find a reference to it).

I feel that traditional DRM gets away from not needing this since their stuff is pretty well established.  Everyone uses Mesa/igt and so how things are structured/used is fairly well implied.  Accel is brand new and doesn't have that yet so I suspect we'll be well situated if we take the extra effort to spell out these things which might be just assumed elsewhere.  Hopefully, over time, such documentation helps in identifying useful areas to build up the common code of the subsystem.

I can't justify holding up this series for this though.  Please put it on some todo list  :)

OK, I've added the documentation to TODO.
For now I will add info about the compiler to v6 cover letter. It is available at https://github.com/openvinotoolkit/vpux-plugin.

Regards,
Jacek