Skip to contentSkip to conversation

New DRM driver for Intel VPU

PATCH RESEND Superseded Jacek Lawrynowicz 3 participants 35 messages Sep 24, 2022 – Dec 7, 2022 Page 1 of 2

Subsystems (2)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover New DRM driver for Intel VPU

Hi,

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

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

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

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

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

I'm resending v3 patchset with correct Dave's email.

Regards,
Jacek

v3:
- 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):
drm/ivpu: Introduce a new DRM driver for Intel VPU drm/ivpu: Add Intel VPU MMU support drm/ivpu: Add GEM buffer object management drm/ivpu: Add IPC driver and JSM messages drm/ivpu: Implement firmware parsing and booting drm/ivpu: Add command buffer submission logic drm/ivpu: Add PM support

 MAINTAINERS                             |    8 +
 drivers/gpu/drm/Kconfig                 |    2 +
 drivers/gpu/drm/Makefile                |    1 +
 drivers/gpu/drm/ivpu/Kconfig            |   12 +
 drivers/gpu/drm/ivpu/Makefile           |   16 +
 drivers/gpu/drm/ivpu/TODO               |    7 +
 drivers/gpu/drm/ivpu/ivpu_drv.c         |  643 ++++++++++++++
 drivers/gpu/drm/ivpu/ivpu_drv.h         |  178 ++++
 drivers/gpu/drm/ivpu/ivpu_fw.c          |  426 +++++++++
 drivers/gpu/drm/ivpu/ivpu_fw.h          |   38 +
 drivers/gpu/drm/ivpu/ivpu_gem.c         |  836 ++++++++++++++++++
 drivers/gpu/drm/ivpu/ivpu_gem.h         |  128 +++
 drivers/gpu/drm/ivpu/ivpu_hw.h          |  169 ++++
 drivers/gpu/drm/ivpu/ivpu_hw_mtl.c      | 1060 +++++++++++++++++++++++
 drivers/gpu/drm/ivpu/ivpu_hw_mtl_reg.h  |  468 ++++++++++
 drivers/gpu/drm/ivpu/ivpu_hw_reg_io.h   |  115 +++
 drivers/gpu/drm/ivpu/ivpu_ipc.c         |  508 +++++++++++
 drivers/gpu/drm/ivpu/ivpu_ipc.h         |   90 ++
 drivers/gpu/drm/ivpu/ivpu_job.c         |  629 ++++++++++++++
 drivers/gpu/drm/ivpu/ivpu_job.h         |   73 ++
 drivers/gpu/drm/ivpu/ivpu_jsm_msg.c     |  220 +++++
 drivers/gpu/drm/ivpu/ivpu_jsm_msg.h     |   25 +
 drivers/gpu/drm/ivpu/ivpu_mmu.c         |  888 +++++++++++++++++++
 drivers/gpu/drm/ivpu/ivpu_mmu.h         |   53 ++
 drivers/gpu/drm/ivpu/ivpu_mmu_context.c |  419 +++++++++
 drivers/gpu/drm/ivpu/ivpu_mmu_context.h |   49 ++
 drivers/gpu/drm/ivpu/ivpu_pm.c          |  352 ++++++++
 drivers/gpu/drm/ivpu/ivpu_pm.h          |   38 +
 drivers/gpu/drm/ivpu/vpu_boot_api.h     |  241 ++++++
 drivers/gpu/drm/ivpu/vpu_jsm_api.h      |  616 +++++++++++++
 include/uapi/drm/ivpu_drm.h             |  343 ++++++++
 31 files changed, 8651 insertions(+)
 create mode 100644 drivers/gpu/drm/ivpu/Kconfig
 create mode 100644 drivers/gpu/drm/ivpu/Makefile
 create mode 100644 drivers/gpu/drm/ivpu/TODO
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_drv.c
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_drv.h
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_fw.c
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_fw.h
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_gem.c
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_gem.h
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw.h
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_mtl.c
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_mtl_reg.h
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_reg_io.h
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_ipc.c
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_ipc.h
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_job.c
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_job.h
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_jsm_msg.c
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_jsm_msg.h
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu.c
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu.h
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu_context.c
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu_context.h
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_pm.c
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_pm.h
 create mode 100644 drivers/gpu/drm/ivpu/vpu_boot_api.h
 create mode 100644 drivers/gpu/drm/ivpu/vpu_jsm_api.h
 create mode 100644 include/uapi/drm/ivpu_drm.h

--
2.34.1

#1 lore raw

PATCH v3 1/7 drm/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 componensts:
- Buttress - provides CPU to VPU integration, interrupt, frequency and power management.
- Memory Management Unit (based on ARM MMU-600) - translates VPU to host DMA addresses, isolates user workloads.
- RISC based microcontroller - executes firmware that provides job execution API for the kernel-mode driver
- Neural Compute Subsystem (NCS) - does the actual work, provides Compute and Copy engines.
- Network on Chip (NoC) - network fabric connecting all the components

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

Module sources are at drivers/gpu/drm/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                            |    8 +
 drivers/gpu/drm/Kconfig                |    2 +
 drivers/gpu/drm/Makefile               |    1 +
 drivers/gpu/drm/ivpu/Kconfig           |   12 +
 drivers/gpu/drm/ivpu/Makefile          |    8 +
 drivers/gpu/drm/ivpu/TODO              |    7 +
 drivers/gpu/drm/ivpu/ivpu_drv.c        |  392 +++++++++
 drivers/gpu/drm/ivpu/ivpu_drv.h        |  153 ++++
 drivers/gpu/drm/ivpu/ivpu_hw.h         |  169 ++++
 drivers/gpu/drm/ivpu/ivpu_hw_mtl.c     | 1021 ++++++++++++++++++++++++
 drivers/gpu/drm/ivpu/ivpu_hw_mtl_reg.h |  468 +++++++++++
 drivers/gpu/drm/ivpu/ivpu_hw_reg_io.h  |  115 +++
 include/uapi/drm/ivpu_drm.h            |   95 +++
 13 files changed, 2451 insertions(+)
 create mode 100644 drivers/gpu/drm/ivpu/Kconfig
 create mode 100644 drivers/gpu/drm/ivpu/Makefile
 create mode 100644 drivers/gpu/drm/ivpu/TODO
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_drv.c
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_drv.h
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw.h
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_mtl.c
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_mtl_reg.h
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_reg_io.h
 create mode 100644 include/uapi/drm/ivpu_drm.h
MAINTAINERS +8 -0
index 9475aa701a3f..d72ceef107e6 100644
@@ -7046,6 +7046,14 @@ F:	Documentation/devicetree/bindings/gpu/vivante,gc.yaml
 F:	drivers/gpu/drm/etnaviv/
 F:	include/uapi/drm/etnaviv_drm.h
 
+DRM DRIVERS FOR VPU
+M:	Jacek Lawrynowicz <[email protected]>
+M:	Stanislaw Gruszka <[email protected]>
+S:	Supported
+T:	git git://anongit.freedesktop.org/drm/drm-misc
+F:	drivers/gpu/drm/ivpu/
+F:	include/uapi/drm/ivpu_drm.h
+
 DRM DRIVERS FOR XEN
 M:	Oleksandr Andrushchenko <[email protected]>
 L:	[email protected]
drivers/gpu/drm/Kconfig +2 -0
index 198ba846d34b..0aaac0e5366f 100644
@@ -364,6 +364,8 @@ source "drivers/gpu/drm/v3d/Kconfig"
 
 source "drivers/gpu/drm/vc4/Kconfig"
 
+source "drivers/gpu/drm/ivpu/Kconfig"
+
 source "drivers/gpu/drm/etnaviv/Kconfig"
 
 source "drivers/gpu/drm/hisilicon/Kconfig"
drivers/gpu/drm/Makefile +1 -0
index 25d0ba310509..1bfd7415c2d0 100644
@@ -94,6 +94,7 @@ obj-$(CONFIG_DRM_KMB_DISPLAY)  += kmb/
 obj-$(CONFIG_DRM_MGAG200) += mgag200/
 obj-$(CONFIG_DRM_V3D)  += v3d/
 obj-$(CONFIG_DRM_VC4)  += vc4/
+obj-$(CONFIG_DRM_IVPU)  += ivpu/
 obj-$(CONFIG_DRM_SIS)   += sis/
 obj-$(CONFIG_DRM_SAVAGE)+= savage/
 obj-$(CONFIG_DRM_VMWGFX)+= vmwgfx/
drivers/gpu/drm/ivpu/Kconfig new file +12 -0
new file mode 100644
index 000000000000..30af359c52f2
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: GPL-2.0-only
+config DRM_IVPU
+	tristate "Intel VPU for Meteor Lake and newer"
+	depends on DRM
+	depends on X86_64 && PCI
+	select SHMEM
+	help
+	  Choose this option if you have a system that has an 14th generation Intel CPU
+	  or newer. VPU stands for Versatile Processing Unit and it's a CPU-integrated
+	  inference accelerator for Computer Vision and Deep Learning applications.
+
+	  If "M" is selected, the module will be called intel_vpu.
drivers/gpu/drm/ivpu/Makefile new file +8 -0
new file mode 100644
index 000000000000..e59dc65abe6a
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-only
+# Copyright © 2022 Intel Corporation
+
+intel_vpu-y := \
+	ivpu_drv.o \
+	ivpu_hw_mtl.o
+
+obj-$(CONFIG_DRM_IVPU) += intel_vpu.o
drivers/gpu/drm/ivpu/TODO new file +7 -0
new file mode 100644
index 000000000000..4c8aa7b96024
@@ -0,0 +1,7 @@
+- 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
\ No newline at end of file
drivers/gpu/drm/ivpu/ivpu_drv.c new file +392 -0

392 lines not on this page · raw message

drivers/gpu/drm/ivpu/ivpu_drv.h new file +153 -0

153 lines not on this page · raw message

drivers/gpu/drm/ivpu/ivpu_hw.h new file +169 -0

169 lines not on this page · raw message

drivers/gpu/drm/ivpu/ivpu_hw_mtl.c new file +1021 -0

1,021 lines not on this page · raw message

drivers/gpu/drm/ivpu/ivpu_hw_mtl_reg.h new file +468 -0

468 lines not on this page · raw message

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

115 lines not on this page · raw message

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

95 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw

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

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

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

Module sources are at drivers/gpu/drm/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                            |    8 +
  drivers/gpu/drm/Kconfig                |    2 +
  drivers/gpu/drm/Makefile               |    1 +
  drivers/gpu/drm/ivpu/Kconfig           |   12 +
  drivers/gpu/drm/ivpu/Makefile          |    8 +
  drivers/gpu/drm/ivpu/TODO              |    7 +
  drivers/gpu/drm/ivpu/ivpu_drv.c        |  392 +++++++++
  drivers/gpu/drm/ivpu/ivpu_drv.h        |  153 ++++
  drivers/gpu/drm/ivpu/ivpu_hw.h         |  169 ++++
  drivers/gpu/drm/ivpu/ivpu_hw_mtl.c     | 1021 ++++++++++++++++++++++++
  drivers/gpu/drm/ivpu/ivpu_hw_mtl_reg.h |  468 +++++++++++
  drivers/gpu/drm/ivpu/ivpu_hw_reg_io.h  |  115 +++
  include/uapi/drm/ivpu_drm.h            |   95 +++
  13 files changed, 2451 insertions(+)

create mode 100644 drivers/gpu/drm/ivpu/Kconfig create mode 100644 drivers/gpu/drm/ivpu/Makefile create mode 100644 drivers/gpu/drm/ivpu/TODO create mode 100644 drivers/gpu/drm/ivpu/ivpu_drv.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_drv.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_mtl.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_mtl_reg.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_reg_io.h create mode 100644 include/uapi/drm/ivpu_drm.h

MAINTAINERS +0 -0
index 9475aa701a3f..d72ceef107e6 100644

MAINTAINERS F: Documentation/devicetree/bindings/gpu/vivante,gc.yaml · line 7055

Quoted 10 lines
MAINTAINERS +7 -0
@@ -7046,6 +7046,14 @@ F:	Documentation/devicetree/bindings/gpu/vivante,gc.yaml
  F:	drivers/gpu/drm/etnaviv/
  F:	include/uapi/drm/etnaviv_drm.h
 
+DRM DRIVERS FOR VPU
+M:	Jacek Lawrynowicz <[email protected]>
+M:	Stanislaw Gruszka <[email protected]>
+S:	Supported
+T:	git git://anongit.freedesktop.org/drm/drm-misc
+F:	drivers/gpu/drm/ivpu/
+F:	include/uapi/drm/ivpu_drm.h

No mail list?

Quoted 9 lines
MAINTAINERS +1 -0
+
  DRM DRIVERS FOR XEN
  M:	Oleksandr Andrushchenko <[email protected]>
  L:	[email protected]
drivers/gpu/drm/Kconfig +0 -0
index 198ba846d34b..0aaac0e5366f 100644

drivers/gpu/drm/Kconfig source "drivers/gpu/drm/v3d/Kconfig" · line 368

Quoted 5 lines
drivers/gpu/drm/Kconfig +2 -0
@@ -364,6 +364,8 @@ source "drivers/gpu/drm/v3d/Kconfig"
 
  source "drivers/gpu/drm/vc4/Kconfig"
 
+source "drivers/gpu/drm/ivpu/Kconfig"
+

Why here of all places? Just randomly in the middle of the list of sourced Kconfigs?

drivers/gpu/drm/Makefile obj-$(CONFIG_DRM_KMB_DISPLAY) += kmb/ · line 97

Quoted 12 lines
drivers/gpu/drm/Kconfig +0 -0
  source "drivers/gpu/drm/etnaviv/Kconfig"
 
  source "drivers/gpu/drm/hisilicon/Kconfig"
drivers/gpu/drm/Makefile +1 -0
index 25d0ba310509..1bfd7415c2d0 100644
@@ -94,6 +94,7 @@ obj-$(CONFIG_DRM_KMB_DISPLAY)  += kmb/
  obj-$(CONFIG_DRM_MGAG200) += mgag200/
  obj-$(CONFIG_DRM_V3D)  += v3d/
  obj-$(CONFIG_DRM_VC4)  += vc4/
+obj-$(CONFIG_DRM_IVPU)  += ivpu/

Again, why here?

Quoted 27 lines
drivers/gpu/drm/Makefile +0 -0
  obj-$(CONFIG_DRM_SIS)   += sis/
  obj-$(CONFIG_DRM_SAVAGE)+= savage/
  obj-$(CONFIG_DRM_VMWGFX)+= vmwgfx/
drivers/gpu/drm/ivpu/Kconfig new file +12 -0
new file mode 100644
index 000000000000..30af359c52f2
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: GPL-2.0-only
+config DRM_IVPU
+	tristate "Intel VPU for Meteor Lake and newer"
+	depends on DRM
+	depends on X86_64 && PCI
+	select SHMEM
+	help
+	  Choose this option if you have a system that has an 14th generation Intel CPU
+	  or newer. VPU stands for Versatile Processing Unit and it's a CPU-integrated
+	  inference accelerator for Computer Vision and Deep Learning applications.
+
+	  If "M" is selected, the module will be called intel_vpu.
drivers/gpu/drm/ivpu/Makefile new file +0 -0
new file mode 100644
index 000000000000..e59dc65abe6a

drivers/gpu/drm/ivpu/Makefile line 2

Quoted 2 lines
drivers/gpu/drm/ivpu/Makefile new file +2 -0
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-only
+# Copyright © 2022 Intel Corporation

I'm pretty sure (C) is preferred. Looks like you do this in multiple places. I'm only going to mention it here.

Quoted 43 lines
drivers/gpu/drm/ivpu/Makefile new file +6 -0
+
+intel_vpu-y := \
+	ivpu_drv.o \
+	ivpu_hw_mtl.o
+
+obj-$(CONFIG_DRM_IVPU) += intel_vpu.o
drivers/gpu/drm/ivpu/TODO new file +7 -0
new file mode 100644
index 000000000000..4c8aa7b96024
@@ -0,0 +1,7 @@
+- 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
\ No newline at end of file
drivers/gpu/drm/ivpu/ivpu_drv.c new file +17 -0
new file mode 100644
index 000000000000..a01c7244f6e5
@@ -0,0 +1,392 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#include <linux/firmware.h>
+#include <linux/module.h>
+#include <linux/pci.h>
+
+#include <drm/drm_drv.h>
+#include <drm/drm_file.h>
+#include <drm/drm_gem.h>
+#include <drm/drm_ioctl.h>
+
+#include "ivpu_drv.h"
+#include "ivpu_hw.h"
+

drivers/gpu/drm/ivpu/ivpu_drv.c line 27

Quoted 10 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +10 -0
+#ifndef DRIVER_VERSION_STR
+#define DRIVER_VERSION_STR __stringify(DRM_IVPU_DRIVER_MAJOR) "." \
+			   __stringify(DRM_IVPU_DRIVER_MINOR) "."
+#endif
+
+static const struct drm_driver driver;
+
+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.");

Shouldn't this be unnecessary with the DRM_DEBUG levels, or making the things tracepoints?

Quoted 12 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +12 -0
+
+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");
+
+u8 ivpu_pll_max_ratio = U8_MAX;
+module_param_named(pll_max_ratio, ivpu_pll_max_ratio, byte, 0644);
+MODULE_PARM_DESC(pll_max_ratio, "Maximum PLL ratio used to set VPU frequency");
+
+char *ivpu_platform_to_str(u32 platform)
+{
+	switch (platform) {

drivers/gpu/drm/ivpu/ivpu_drv.c line 49

Quoted 10 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +10 -0
+	case IVPU_PLATFORM_SILICON:
+		return "IVPU_PLATFORM_SILICON";
+	case IVPU_PLATFORM_SIMICS:
+		return "IVPU_PLATFORM_SIMICS";
+	case IVPU_PLATFORM_FPGA:
+		return "IVPU_PLATFORM_FPGA";
+	default:
+		return "Invalid platform";
+	}
+}

In this entire series, this is only used in this patch, and only in one file. Seems pointless to define it here, and have it in the header. Why shouldn't this be moved to the file it is used in, and made static?

Quoted 43 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +43 -0
+
+void ivpu_file_priv_get(struct ivpu_file_priv *file_priv, struct ivpu_file_priv **link)
+{
+	kref_get(&file_priv->ref);
+	*link = file_priv;
+}
+
+static void file_priv_release(struct kref *ref)
+{
+	struct ivpu_file_priv *file_priv = container_of(ref, struct ivpu_file_priv, ref);
+
+	kfree(file_priv);
+}
+
+void ivpu_file_priv_put(struct ivpu_file_priv **link)
+{
+	struct ivpu_file_priv *file_priv = *link;
+
+	*link = NULL;
+	kref_put(&file_priv->ref, file_priv_release);
+}
+
+static int ivpu_get_param_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 pci_dev *pdev = to_pci_dev(vdev->drm.dev);
+	struct drm_ivpu_param *args = data;
+	int ret = 0 > +
+	switch (args->param) {
+	case DRM_IVPU_PARAM_DEVICE_ID:
+		args->value = pdev->device;
+		break;
+	case DRM_IVPU_PARAM_DEVICE_REVISION:
+		args->value = pdev->revision;
+		break;
+	case DRM_IVPU_PARAM_PLATFORM_TYPE:
+		args->value = vdev->platform;
+		break;
+	case DRM_IVPU_PARAM_CORE_CLOCK_RATE:
+		args->value = ivpu_hw_reg_pll_freq_get(vdev);
+		break;
+	case DRM_IVPU_PARAM_NUM_CONTEXTS:

drivers/gpu/drm/ivpu/ivpu_drv.c line 103

Quoted 10 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +10 -0
+		args->value = ivpu_get_context_count(vdev);
+		break;
+	case DRM_IVPU_PARAM_CONTEXT_BASE_ADDRESS:
+		args->value = vdev->hw->ranges.user_low.start;
+		break;
+	case DRM_IVPU_PARAM_CONTEXT_PRIORITY:
+		args->value = file_priv->priority;
+		break;
+	default:
+		ret = -EINVAL;

This doesn't cause a switch case fallthrough warning?

Quoted 28 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +28 -0
+	}
+
+	return ret;
+}
+
+static int ivpu_set_param_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
+{
+	struct ivpu_file_priv *file_priv = file->driver_priv;
+	struct drm_ivpu_param *args = data;
+	int ret = 0;
+
+	switch (args->param) {
+	case DRM_IVPU_PARAM_CONTEXT_PRIORITY:
+		if (args->value <= DRM_IVPU_CONTEXT_PRIORITY_REALTIME)
+			file_priv->priority = args->value;
+		else
+			ret = -EINVAL;
+		break;
+	default:
+		ret = -EINVAL;
+	}
+
+	return ret;
+}
+
+static int ivpu_open(struct drm_device *dev, struct drm_file *file)
+{
+	struct ivpu_device *vdev = to_ivpu_device(dev);

drivers/gpu/drm/ivpu/ivpu_drv.c line 141

Quoted 10 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +10 -0
+	struct ivpu_file_priv *file_priv;
+
+	file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
+	if (!file_priv)
+		return -ENOMEM;
+
+	file_priv->vdev = vdev;
+	file_priv->priority = DRM_IVPU_CONTEXT_PRIORITY_NORMAL;
+
+	kref_init(&file_priv->ref);

VFS is going to maintain a refcount on the fd. This looks like you are duplicating that ref count, which seems pointless.

Later on you use this for jobs, as each job takes a ref, but why would it be valid for jobs to hang around after the fd is closed?

Quoted 22 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +22 -0
+
+	file->driver_priv = file_priv;
+
+	return 0;
+}
+
+static void ivpu_postclose(struct drm_device *dev, struct drm_file *file)
+{
+	struct ivpu_file_priv *file_priv = file->driver_priv;
+
+	ivpu_file_priv_put(&file_priv);
+}
+
+static const struct drm_ioctl_desc ivpu_drm_ioctls[] = {
+	DRM_IOCTL_DEF_DRV(IVPU_GET_PARAM, ivpu_get_param_ioctl, DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(IVPU_SET_PARAM, ivpu_set_param_ioctl, DRM_RENDER_ALLOW),
+};
+
+DEFINE_DRM_GEM_FOPS(ivpu_fops);
+
+int ivpu_shutdown(struct ivpu_device *vdev)
+{

drivers/gpu/drm/ivpu/ivpu_drv.c line 173

Quoted 10 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +10 -0
+	int ret;
+
+	ivpu_hw_irq_disable(vdev);
+
+	ret = ivpu_hw_power_down(vdev);
+	if (ret)
+		ivpu_warn(vdev, "Failed to power down HW: %d\n", ret);
+
+	return ret;
+}

Feels odd to have this function definition sit between the DEFINE_DRM_GEM_FOPS and the drm_driver

Quoted 16 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +16 -0
+
+static const struct drm_driver driver = {
+	.driver_features = DRIVER_GEM | DRIVER_RENDER,
+
+	.open = ivpu_open,
+	.postclose = ivpu_postclose,
+
+	.ioctls = ivpu_drm_ioctls,
+	.num_ioctls = ARRAY_SIZE(ivpu_drm_ioctls),
+	.fops = &ivpu_fops,
+
+	.name = DRIVER_NAME,
+	.desc = DRIVER_DESC,
+	.date = DRIVER_DATE,
+	.major = DRM_IVPU_DRIVER_MAJOR,
+	.minor = DRM_IVPU_DRIVER_MINOR,

drivers/gpu/drm/ivpu/ivpu_drv.c line 199

Quoted 10 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +10 -0
+};
+
+static int ivpu_irq_init(struct ivpu_device *vdev)
+{
+	struct pci_dev *pdev = to_pci_dev(vdev->drm.dev);
+	int ret;
+
+	ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI | PCI_IRQ_MSIX);
+	if (ret < 0) {
+		ivpu_err(vdev, "Failed to allocate and MSI IRQ: %d\n", ret);

"a MSI"?

Quoted 23 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +23 -0
+		return ret;
+	}
+
+	vdev->irq = pci_irq_vector(pdev, 0);
+
+	ret = request_irq(vdev->irq, vdev->hw->ops->irq_handler, IRQF_SHARED,
+			  DRIVER_NAME, vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to request an IRQ %d\n", ret);
+		pci_free_irq_vectors(pdev);
+	}
+
+	return ret;
+}
+
+static void ivpu_irq_fini(struct ivpu_device *vdev)
+{
+	free_irq(vdev->irq, vdev);
+	pci_free_irq_vectors(to_pci_dev(vdev->drm.dev));
+}
+
+static int ivpu_pci_init(struct ivpu_device *vdev)
+{

drivers/gpu/drm/ivpu/ivpu_drv.c line 232

Quoted 10 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +10 -0
+	struct pci_dev *pdev = to_pci_dev(vdev->drm.dev);
+	struct resource *bar0 = &pdev->resource[0];
+	struct resource *bar4 = &pdev->resource[4];
+	int ret;
+
+	ivpu_dbg(MISC, "Mapping BAR0 (RegV) %pR\n", bar0);
+	vdev->regv = devm_ioremap_resource(vdev->drm.dev, bar0);
+	if (IS_ERR(vdev->regv)) {
+		ivpu_err(vdev, "Failed to map bar 0\n");
+		return -ENOMEM;

You have a particular reason for recasting the error in vdev->regv to ENOMEM?

Quoted 85 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +85 -0
+	}
+
+	ivpu_dbg(MISC, "Mapping BAR4 (RegB) %pR\n", bar4);
+	vdev->regb = devm_ioremap_resource(vdev->drm.dev, bar4);
+	if (IS_ERR(vdev->regb)) {
+		ivpu_err(vdev, "Failed to map bar 4\n");
+		return -ENOMEM;
+	}
+
+	ret = dma_set_mask_and_coherent(vdev->drm.dev, DMA_BIT_MASK(38));
+	if (ret) {
+		ivpu_err(vdev, "Failed to set DMA mask: %d\n", ret);
+		return ret;
+	}
+
+	/* Clear any pending errors */
+	pcie_capability_clear_word(pdev, PCI_EXP_DEVSTA, 0x3f);
+
+	ret = pci_enable_device(pdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to enable PCI device: %d\n", ret);
+		return ret;
+	}
+
+	pci_set_master(pdev);
+
+	return 0;
+}
+
+static void ivpu_pci_fini(struct ivpu_device *vdev)
+{
+	pci_disable_device(to_pci_dev(vdev->drm.dev));
+}
+
+static int ivpu_dev_init(struct ivpu_device *vdev)
+{
+	int ret;
+
+	vdev->hw = devm_kzalloc(vdev->drm.dev, sizeof(*vdev->hw), GFP_KERNEL);
+	if (!vdev->hw)
+		return -ENOMEM;
+
+	vdev->hw->ops = &ivpu_hw_mtl_ops;
+	vdev->platform = IVPU_PLATFORM_INVALID;
+
+	xa_init_flags(&vdev->context_xa, XA_FLAGS_ALLOC);
+	vdev->context_xa_limit.min = IVPU_GLOBAL_CONTEXT_MMU_SSID + 1;
+	vdev->context_xa_limit.max = IVPU_CONTEXT_LIMIT;
+
+	ret = ivpu_pci_init(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to initialize PCI device: %d\n", ret);
+		return ret;
+	}
+
+	ret = ivpu_irq_init(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to initialize IRQs: %d\n", ret);
+		goto err_pci_fini;
+	}
+
+	ret = ivpu_hw_info_init(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to initialize HW info: %d\n", ret);
+		goto err_irq_fini;
+	}
+
+	ret = ivpu_hw_power_up(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to power up HW: %d\n", ret);
+		goto err_irq_fini;
+	}
+
+	return 0;
+
+err_irq_fini:
+	ivpu_irq_fini(vdev);
+err_pci_fini:
+	ivpu_pci_fini(vdev);
+	return ret;
+}
+
+static void ivpu_dev_fini(struct ivpu_device *vdev)
+{
+	ivpu_shutdown(vdev);

drivers/gpu/drm/ivpu/ivpu_drv.c line 327

Quoted 10 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +10 -0
+
+	ivpu_irq_fini(vdev);
+	ivpu_pci_fini(vdev);
+
+	WARN_ON(!xa_empty(&vdev->context_xa));
+	xa_destroy(&vdev->context_xa);
+}
+
+static struct pci_device_id ivpu_pci_ids[] = {
+	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_MTL) },

Why not use include/linux/pci_ids.h for the VID?

Quoted 54 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +54 -0
+	{ }
+};
+MODULE_DEVICE_TABLE(pci, ivpu_pci_ids);
+
+static int ivpu_probe(struct pci_dev *pdev, const struct pci_device_id *id)
+{
+	struct ivpu_device *vdev;
+	int ret;
+
+	vdev = devm_drm_dev_alloc(&pdev->dev, &driver, struct ivpu_device, drm);
+	if (IS_ERR(vdev))
+		return PTR_ERR(vdev);
+
+	pci_set_drvdata(pdev, vdev);
+	vdev->drm.dev_private = vdev;
+
+	ret = ivpu_dev_init(vdev);
+	if (ret) {
+		dev_err(&pdev->dev, "Failed to initialize VPU device: %d\n", ret);
+		return ret;
+	}
+
+	ret = drm_dev_register(&vdev->drm, 0);
+	if (ret) {
+		dev_err(&pdev->dev, "Failed to register DRM device: %d\n", ret);
+		ivpu_dev_fini(vdev);
+	}
+
+	return ret;
+}
+
+static void ivpu_remove(struct pci_dev *pdev)
+{
+	struct ivpu_device *vdev = pci_get_drvdata(pdev);
+
+	drm_dev_unregister(&vdev->drm);
+	ivpu_dev_fini(vdev);
+}
+
+static struct pci_driver ivpu_pci_driver = {
+	.name = KBUILD_MODNAME,
+	.id_table = ivpu_pci_ids,
+	.probe = ivpu_probe,
+	.remove = ivpu_remove,
+};
+
+static __init int ivpu_init(void)
+{
+	pr_info("Intel VPU driver version: %s", DRIVER_VERSION_STR);
+
+	return pci_register_driver(&ivpu_pci_driver);
+}
+
+static __exit void ivpu_fini(void)

drivers/gpu/drm/ivpu/ivpu_drv.c line 391

Quoted 10 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +10 -0
+{
+	pci_unregister_driver(&ivpu_pci_driver);
+}
+
+module_init(ivpu_init);
+module_exit(ivpu_fini);
+
+MODULE_AUTHOR("Intel Corporation");
+MODULE_DESCRIPTION(DRIVER_DESC);
+MODULE_LICENSE("GPL and additional rights");

I don't think this is valid. Pretty sure should just be "GPL".

Quoted 2,058 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +1 -0
+MODULE_VERSION(DRIVER_VERSION_STR);
drivers/gpu/drm/ivpu/ivpu_drv.h new file +153 -0
new file mode 100644
index 000000000000..43dfa78544c6
@@ -0,0 +1,153 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#ifndef __IVPU_DRV_H__
+#define __IVPU_DRV_H__
+
+#include <drm/drm_device.h>
+#include <drm/drm_mm.h>
+#include <drm/drm_print.h>
+
+#include <linux/pci.h>
+#include <linux/xarray.h>
+#include <uapi/drm/ivpu_drm.h>
+
+#define DRIVER_NAME "intel_vpu"
+#define DRIVER_DESC "Driver for Intel Versatile Processing Unit (VPU)"
+#define DRIVER_DATE "20220913"
+
+#define PCI_VENDOR_ID_INTEL 0x8086
+#define PCI_DEVICE_ID_MTL   0x7d1d
+
+#define IVPU_GLOBAL_CONTEXT_MMU_SSID 0
+#define IVPU_CONTEXT_LIMIT	     64
+#define IVPU_NUM_ENGINES	     2
+
+#define IVPU_PLATFORM_SILICON 0
+#define IVPU_PLATFORM_SIMICS  2
+#define IVPU_PLATFORM_FPGA    3
+#define IVPU_PLATFORM_INVALID 8
+
+#define IVPU_DBG_REG	 BIT(0)
+#define IVPU_DBG_IRQ	 BIT(1)
+#define IVPU_DBG_MMU	 BIT(2)
+#define IVPU_DBG_FILE	 BIT(3)
+#define IVPU_DBG_MISC	 BIT(4)
+#define IVPU_DBG_FW_BOOT BIT(5)
+#define IVPU_DBG_PM	 BIT(6)
+#define IVPU_DBG_IPC	 BIT(7)
+#define IVPU_DBG_BO	 BIT(8)
+#define IVPU_DBG_JOB	 BIT(9)
+#define IVPU_DBG_JSM	 BIT(10)
+#define IVPU_DBG_KREF	 BIT(11)
+
+#define ivpu_err(vdev, fmt, ...) \
+	dev_err((vdev)->drm.dev, "[%s] ERROR: " fmt, __func__, ##__VA_ARGS__)
+
+#define ivpu_err_ratelimited(vdev, fmt, ...) \
+	dev_err_ratelimited((vdev)->drm.dev, "[%s] ERROR: " fmt, __func__, ##__VA_ARGS__)
+
+#define ivpu_warn(vdev, fmt, ...) \
+	dev_warn((vdev)->drm.dev, "[%s] WARNING: " fmt, __func__, ##__VA_ARGS__)
+
+#define ivpu_warn_ratelimited(vdev, fmt, ...) \
+	dev_warn_ratelimited((vdev)->drm.dev, "[%s] WARNING: " fmt, __func__, ##__VA_ARGS__)
+
+#define ivpu_info(vdev, fmt, ...) dev_info((vdev)->drm.dev, fmt, ##__VA_ARGS__)
+
+#define ivpu_dbg(type, fmt, args...) do {                                      \
+	if (unlikely(IVPU_DBG_##type & ivpu_dbg_mask))                         \
+		dev_dbg((vdev)->drm.dev, "[%s] " fmt, #type, ##args);          \
+} while (0)
+
+#define IVPU_WA(wa_name) (vdev->wa.wa_name)
+
+struct ivpu_wa_table {
+	bool punit_disabled;
+	bool clear_runtime_mem;
+};
+
+struct ivpu_hw_info;
+
+struct ivpu_device {
+	struct drm_device drm; /* Must be first */
+	void __iomem *regb;
+	void __iomem *regv;
+	u32 platform;
+	u32 irq;
+
+	struct ivpu_wa_table wa;
+	struct ivpu_hw_info *hw;
+
+	struct xarray context_xa;
+	struct xa_limit context_xa_limit;
+
+	struct {
+		int boot;
+		int jsm;
+		int tdr;
+		int reschedule_suspend;
+	} timeout;
+};
+
+struct ivpu_file_priv {
+	struct kref ref;
+	struct ivpu_device *vdev;
+	u32 priority;
+};
+
+extern int ivpu_dbg_mask;
+extern u8 ivpu_pll_min_ratio;
+extern u8 ivpu_pll_max_ratio;
+
+void ivpu_file_priv_get(struct ivpu_file_priv *file_priv, struct ivpu_file_priv **link);
+void ivpu_file_priv_put(struct ivpu_file_priv **link);
+char *ivpu_platform_to_str(u32 platform);
+int ivpu_shutdown(struct ivpu_device *vdev);
+
+static inline bool ivpu_is_mtl(struct ivpu_device *vdev)
+{
+	return to_pci_dev(vdev->drm.dev)->device == PCI_DEVICE_ID_MTL;
+}
+
+static inline u8 ivpu_revison(struct ivpu_device *vdev)
+{
+	return to_pci_dev(vdev->drm.dev)->revision;
+}
+
+static inline struct ivpu_device *to_ivpu_device(struct drm_device *dev)
+{
+	return container_of(dev, struct ivpu_device, drm);
+}
+
+static inline u32 ivpu_get_context_count(struct ivpu_device *vdev)
+{
+	struct xa_limit ctx_limit = vdev->context_xa_limit;
+
+	return (ctx_limit.max - ctx_limit.min + 1);
+}
+
+static inline u32 ivpu_get_platform(struct ivpu_device *vdev)
+{
+	WARN_ON_ONCE(vdev->platform == IVPU_PLATFORM_INVALID);
+	return vdev->platform;
+}
+
+static inline bool ivpu_is_silicon(struct ivpu_device *vdev)
+{
+	return ivpu_get_platform(vdev) == IVPU_PLATFORM_SILICON;
+}
+
+static inline bool ivpu_is_simics(struct ivpu_device *vdev)
+{
+	return ivpu_get_platform(vdev) == IVPU_PLATFORM_SIMICS;
+}
+
+static inline bool ivpu_is_fpga(struct ivpu_device *vdev)
+{
+	return ivpu_get_platform(vdev) == IVPU_PLATFORM_FPGA;
+}
+
+#endif /* __IVPU_DRV_H__ */
drivers/gpu/drm/ivpu/ivpu_hw.h new file +169 -0
new file mode 100644
index 000000000000..06de7501e476
@@ -0,0 +1,169 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#ifndef __IVPU_HW_H__
+#define __IVPU_HW_H__
+
+#include "ivpu_drv.h"
+
+struct ivpu_hw_ops {
+	int (*info_init)(struct ivpu_device *vdev);
+	int (*power_up)(struct ivpu_device *vdev);
+	int (*boot_fw)(struct ivpu_device *vdev);
+	int (*power_down)(struct ivpu_device *vdev);
+	bool (*is_idle)(struct ivpu_device *vdev);
+	void (*wdt_disable)(struct ivpu_device *vdev);
+	void (*diagnose_tdr)(struct ivpu_device *vdev);
+	u32 (*reg_pll_freq_get)(struct ivpu_device *vdev);
+	u32 (*reg_telemetry_offset_get)(struct ivpu_device *vdev);
+	u32 (*reg_telemetry_size_get)(struct ivpu_device *vdev);
+	u32 (*reg_telemetry_enable_get)(struct ivpu_device *vdev);
+	void (*reg_db_set)(struct ivpu_device *vdev, u32 db_id);
+	u32 (*reg_ipc_rx_addr_get)(struct ivpu_device *vdev);
+	u32 (*reg_ipc_rx_count_get)(struct ivpu_device *vdev);
+	void (*reg_ipc_tx_set)(struct ivpu_device *vdev, u32 vpu_addr);
+	void (*irq_clear)(struct ivpu_device *vdev);
+	void (*irq_enable)(struct ivpu_device *vdev);
+	void (*irq_disable)(struct ivpu_device *vdev);
+	irqreturn_t (*irq_handler)(int irq, void *ptr);
+};
+
+struct ivpu_addr_range {
+	u64 start;
+	u64 end;
+};
+
+struct ivpu_hw_info {
+	const struct ivpu_hw_ops *ops;
+	struct {
+		struct ivpu_addr_range global_low;
+		struct ivpu_addr_range global_high;
+		struct ivpu_addr_range user_low;
+		struct ivpu_addr_range user_high;
+		struct ivpu_addr_range global_aliased_pio;
+	} ranges;
+	struct {
+		u8 min_ratio;
+		u8 max_ratio;
+	} pll;
+	u32 tile_fuse;
+	u32 sku;
+	u16 config;
+};
+
+extern const struct ivpu_hw_ops ivpu_hw_mtl_ops;
+
+static inline int ivpu_hw_info_init(struct ivpu_device *vdev)
+{
+	return vdev->hw->ops->info_init(vdev);
+};
+
+static inline int ivpu_hw_power_up(struct ivpu_device *vdev)
+{
+	ivpu_dbg(PM, "HW power up\n");
+
+	return vdev->hw->ops->power_up(vdev);
+};
+
+static inline int ivpu_hw_boot_fw(struct ivpu_device *vdev)
+{
+	return vdev->hw->ops->boot_fw(vdev);
+};
+
+static inline bool ivpu_hw_is_idle(struct ivpu_device *vdev)
+{
+	return vdev->hw->ops->is_idle(vdev);
+};
+
+static inline int ivpu_hw_power_down(struct ivpu_device *vdev)
+{
+	ivpu_dbg(PM, "HW power down\n");
+
+	return vdev->hw->ops->power_down(vdev);
+};
+
+static inline void ivpu_hw_wdt_disable(struct ivpu_device *vdev)
+{
+	vdev->hw->ops->wdt_disable(vdev);
+};
+
+/* Register indirect accesses */
+static inline u32 ivpu_hw_reg_pll_freq_get(struct ivpu_device *vdev)
+{
+	return vdev->hw->ops->reg_pll_freq_get(vdev);
+};
+
+static inline u32 ivpu_hw_reg_telemetry_offset_get(struct ivpu_device *vdev)
+{
+	return vdev->hw->ops->reg_telemetry_offset_get(vdev);
+};
+
+static inline u32 ivpu_hw_reg_telemetry_size_get(struct ivpu_device *vdev)
+{
+	return vdev->hw->ops->reg_telemetry_size_get(vdev);
+};
+
+static inline u32 ivpu_hw_reg_telemetry_enable_get(struct ivpu_device *vdev)
+{
+	return vdev->hw->ops->reg_telemetry_enable_get(vdev);
+};
+
+static inline void ivpu_hw_reg_db_set(struct ivpu_device *vdev, u32 db_id)
+{
+	vdev->hw->ops->reg_db_set(vdev, db_id);
+};
+
+static inline u32 ivpu_hw_reg_ipc_rx_addr_get(struct ivpu_device *vdev)
+{
+	return vdev->hw->ops->reg_ipc_rx_addr_get(vdev);
+};
+
+static inline u32 ivpu_hw_reg_ipc_rx_count_get(struct ivpu_device *vdev)
+{
+	return vdev->hw->ops->reg_ipc_rx_count_get(vdev);
+};
+
+static inline void ivpu_hw_reg_ipc_tx_set(struct ivpu_device *vdev, u32 vpu_addr)
+{
+	vdev->hw->ops->reg_ipc_tx_set(vdev, vpu_addr);
+};
+
+static inline void ivpu_hw_irq_clear(struct ivpu_device *vdev)
+{
+	vdev->hw->ops->irq_clear(vdev);
+};
+
+static inline void ivpu_hw_irq_enable(struct ivpu_device *vdev)
+{
+	vdev->hw->ops->irq_enable(vdev);
+};
+
+static inline void ivpu_hw_irq_disable(struct ivpu_device *vdev)
+{
+	vdev->hw->ops->irq_disable(vdev);
+};
+
+static inline void ivpu_hw_init_range(struct ivpu_addr_range *range, u64 start, u64 size)
+{
+	range->start = start;
+	range->end = start + size;
+}
+
+static inline u64 ivpu_hw_range_size(const struct ivpu_addr_range *range)
+{
+	return range->end - range->start;
+}
+
+static inline u8 ivpu_hw_get_pll_ratio_in_range(u8 pll_ratio, u8 min, u8 max)
+{
+	return min_t(u8, max_t(u8, pll_ratio, min), max);
+}
+
+static inline void ivpu_hw_diagnose_tdr(struct ivpu_device *vdev)
+{
+	vdev->hw->ops->diagnose_tdr(vdev);
+}
+
+#endif /* __IVPU_HW_H__ */
drivers/gpu/drm/ivpu/ivpu_hw_mtl.c new file +1021 -0
new file mode 100644
index 000000000000..17fc8e1104df
@@ -0,0 +1,1021 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#include "ivpu_drv.h"
+#include "ivpu_hw_mtl_reg.h"
+#include "ivpu_hw_reg_io.h"
+#include "ivpu_hw.h"
+
+#define TILE_FUSE_ENABLE_BOTH	     0x0
+#define TILE_FUSE_ENABLE_UPPER	     0x1
+#define TILE_FUSE_ENABLE_LOWER	     0x2
+
+#define TILE_SKU_BOTH_MTL	     0x3630
+#define TILE_SKU_LOWER_MTL	     0x3631
+#define TILE_SKU_UPPER_MTL	     0x3632
+
+/* Work point configuration values */
+#define WP_CONFIG_1_TILE_5_3_RATIO   0x0101
+#define WP_CONFIG_1_TILE_4_3_RATIO   0x0102
+#define WP_CONFIG_2_TILE_5_3_RATIO   0x0201
+#define WP_CONFIG_2_TILE_4_3_RATIO   0x0202
+#define WP_CONFIG_0_TILE_PLL_OFF     0x0000
+
+#define PLL_REF_CLK_FREQ	     (50 * 1000000)
+#define PLL_SIMULATION_FREQ	     (10 * 1000000)
+#define PLL_RATIO_TO_FREQ(x)	     ((x) * PLL_REF_CLK_FREQ)
+#define PLL_DEFAULT_EPP_VALUE	     0x80
+
+#define TIM_SAFE_ENABLE		     0xf1d0dead
+#define TIM_WATCHDOG_RESET_VALUE     0xffffffff
+
+#define TIMEOUT_US		     (150 * USEC_PER_MSEC)
+#define PWR_ISLAND_STATUS_TIMEOUT_US (5 * USEC_PER_MSEC)
+#define PLL_TIMEOUT_US		     (1500 * USEC_PER_MSEC)
+#define IDLE_TIMEOUT_US		     (500 * USEC_PER_MSEC)
+
+#define ICB_0_IRQ_MASK ((REG_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, HOST_IPC_FIFO_INT)) | \
+			(REG_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_0_INT)) | \
+			(REG_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_1_INT)) | \
+			(REG_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_2_INT)) | \
+			(REG_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, NOC_FIREWALL_INT)) | \
+			(REG_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, CPU_INT_REDIRECT_0_INT)) | \
+			(REG_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, CPU_INT_REDIRECT_1_INT)))
+
+#define ICB_1_IRQ_MASK ((REG_FLD(MTL_VPU_HOST_SS_ICB_STATUS_1, CPU_INT_REDIRECT_2_INT)) | \
+			(REG_FLD(MTL_VPU_HOST_SS_ICB_STATUS_1, CPU_INT_REDIRECT_3_INT)) | \
+			(REG_FLD(MTL_VPU_HOST_SS_ICB_STATUS_1, CPU_INT_REDIRECT_4_INT)))
+
+#define ICB_0_1_IRQ_MASK ((((u64)ICB_1_IRQ_MASK) << 32) | ICB_0_IRQ_MASK)
+
+#define BUTTRESS_IRQ_MASK ((REG_FLD(MTL_BUTTRESS_INTERRUPT_STAT, FREQ_CHANGE)) | \
+			   (REG_FLD(MTL_BUTTRESS_INTERRUPT_STAT, ATS_ERR)) | \
+			   (REG_FLD(MTL_BUTTRESS_INTERRUPT_STAT, UFI_ERR)))
+
+#define ITF_FIREWALL_VIOLATION_MASK ((REG_FLD(MTL_VPU_HOST_SS_FW_SOC_IRQ_EN, CSS_ROM_CMX)) | \
+				     (REG_FLD(MTL_VPU_HOST_SS_FW_SOC_IRQ_EN, CSS_DBG)) | \
+				     (REG_FLD(MTL_VPU_HOST_SS_FW_SOC_IRQ_EN, CSS_CTRL)) | \
+				     (REG_FLD(MTL_VPU_HOST_SS_FW_SOC_IRQ_EN, DEC400)) | \
+				     (REG_FLD(MTL_VPU_HOST_SS_FW_SOC_IRQ_EN, MSS_NCE)) | \
+				     (REG_FLD(MTL_VPU_HOST_SS_FW_SOC_IRQ_EN, MSS_MBI)) | \
+				     (REG_FLD(MTL_VPU_HOST_SS_FW_SOC_IRQ_EN, MSS_MBI_CMX)))
+
+static void ivpu_hw_read_platform(struct ivpu_device *vdev)
+{
+	u32 gen_ctrl = REGV_RD32(MTL_VPU_HOST_SS_GEN_CTRL);
+	u32 platform = REG_GET_FLD(MTL_VPU_HOST_SS_GEN_CTRL, PS, gen_ctrl);
+
+	if  (platform == IVPU_PLATFORM_SIMICS || platform == IVPU_PLATFORM_FPGA)
+		vdev->platform = platform;
+	else
+		vdev->platform = IVPU_PLATFORM_SILICON;
+
+	ivpu_dbg(MISC, "Platform type: %s (%d)\n",
+		 ivpu_platform_to_str(vdev->platform), vdev->platform);
+}
+
+static void ivpu_hw_wa_init(struct ivpu_device *vdev)
+{
+	vdev->wa.punit_disabled = ivpu_is_fpga(vdev);
+	vdev->wa.clear_runtime_mem = true;
+}
+
+static void ivpu_hw_timeouts_init(struct ivpu_device *vdev)
+{
+	if (ivpu_is_simics(vdev) || ivpu_is_fpga(vdev)) {
+		vdev->timeout.boot = 100000;
+		vdev->timeout.jsm = 50000;
+		vdev->timeout.tdr = 2000000;
+		vdev->timeout.reschedule_suspend = 1000;
+	} else {
+		vdev->timeout.boot = 1000;
+		vdev->timeout.jsm = 500;
+		vdev->timeout.tdr = 2000;
+		vdev->timeout.reschedule_suspend = 10;
+	}
+}
+
+static int ivpu_pll_wait_for_cmd_send(struct ivpu_device *vdev)
+{
+	return REGB_POLL_FLD(MTL_BUTTRESS_WP_REQ_CMD, SEND, 0, PLL_TIMEOUT_US);
+}
+
+/* Send KMD initiated workpoint change */
+static int ivpu_pll_cmd_send(struct ivpu_device *vdev, u16 min_ratio, u16 max_ratio,
+			     u16 target_ratio, u16 config)
+{
+	int ret;
+	u32 val;
+
+	ret = ivpu_pll_wait_for_cmd_send(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to sync before WP request: %d\n", ret);
+		return ret;
+	}
+
+	val = REGB_RD32(MTL_BUTTRESS_WP_REQ_PAYLOAD0);
+	val = REG_SET_FLD_NUM(MTL_BUTTRESS_WP_REQ_PAYLOAD0, MIN_RATIO, min_ratio, val);
+	val = REG_SET_FLD_NUM(MTL_BUTTRESS_WP_REQ_PAYLOAD0, MAX_RATIO, max_ratio, val);
+	REGB_WR32(MTL_BUTTRESS_WP_REQ_PAYLOAD0, val);
+
+	val = REGB_RD32(MTL_BUTTRESS_WP_REQ_PAYLOAD1);
+	val = REG_SET_FLD_NUM(MTL_BUTTRESS_WP_REQ_PAYLOAD1, TARGET_RATIO, target_ratio, val);
+	val = REG_SET_FLD_NUM(MTL_BUTTRESS_WP_REQ_PAYLOAD1, EPP, PLL_DEFAULT_EPP_VALUE, val);
+	REGB_WR32(MTL_BUTTRESS_WP_REQ_PAYLOAD1, val);
+
+	val = REGB_RD32(MTL_BUTTRESS_WP_REQ_PAYLOAD2);
+	val = REG_SET_FLD_NUM(MTL_BUTTRESS_WP_REQ_PAYLOAD2, CONFIG, config, val);
+	REGB_WR32(MTL_BUTTRESS_WP_REQ_PAYLOAD2, val);
+
+	val = REGB_RD32(MTL_BUTTRESS_WP_REQ_CMD);
+	val = REG_SET_FLD(MTL_BUTTRESS_WP_REQ_CMD, SEND, val);
+	REGB_WR32(MTL_BUTTRESS_WP_REQ_CMD, val);
+
+	ret = ivpu_pll_wait_for_cmd_send(vdev);
+	if (ret)
+		ivpu_err(vdev, "Failed to sync after WP request: %d\n", ret);
+
+	return ret;
+}
+
+static int ivpu_pll_wait_for_lock(struct ivpu_device *vdev, bool enable)
+{
+	u32 exp_val = enable ? 0x1 : 0x0;
+
+	if (IVPU_WA(punit_disabled))
+		return 0;
+
+	return REGB_POLL_FLD(MTL_BUTTRESS_PLL_STATUS, LOCK, exp_val, PLL_TIMEOUT_US);
+}
+
+static int ivpu_pll_wait_for_status_ready(struct ivpu_device *vdev)
+{
+	if (IVPU_WA(punit_disabled))
+		return 0;
+
+	return REGB_POLL_FLD(MTL_BUTTRESS_VPU_STATUS, READY, 1, PLL_TIMEOUT_US);
+}
+
+static u16 ivpu_hw_mtl_reg_pll_min_ratio_get(struct ivpu_device *vdev)
+{
+	if (IVPU_WA(punit_disabled))
+		return 0;
+
+	return REGB_RD32(MTL_BUTTRESS_FMIN_FUSE) & MTL_BUTTRESS_FMIN_FUSE_RATIO_MASK;
+}
+
+static u16 ivpu_hw_mtl_reg_pll_max_ratio_get(struct ivpu_device *vdev)
+{
+	if (IVPU_WA(punit_disabled))
+		return 0;
+
+	return REGB_RD32(MTL_BUTTRESS_FMAX_FUSE) & MTL_BUTTRESS_FMAX_FUSE_RATIO_MASK;
+}
+
+static int ivpu_pll_drive(struct ivpu_device *vdev, bool enable)
+{
+	int ret;
+	struct ivpu_hw_info *hw = vdev->hw;
+	u16 target_ratio;
+	u16 config;
+
+	if (IVPU_WA(punit_disabled)) {
+		ivpu_dbg(PM, "Skipping PLL request on %s\n", ivpu_platform_to_str(vdev->platform));
+		return 0;
+	}
+
+	if (enable) {
+		u8 pll_hw_min_ratio = ivpu_hw_mtl_reg_pll_min_ratio_get(vdev);
+		u8 pll_hw_max_ratio = ivpu_hw_mtl_reg_pll_max_ratio_get(vdev);
+
+		hw->pll.max_ratio = ivpu_hw_get_pll_ratio_in_range(ivpu_pll_max_ratio,
+								   pll_hw_min_ratio,
+								   pll_hw_max_ratio);
+		hw->pll.min_ratio = ivpu_hw_get_pll_ratio_in_range(ivpu_pll_min_ratio,
+								   pll_hw_min_ratio,
+								   pll_hw_max_ratio);
+		if (hw->pll.max_ratio < hw->pll.min_ratio) {
+			ivpu_err(vdev, "Invalid pll ratio values, min 0x%x max 0x%x\n",
+				 hw->pll.min_ratio, hw->pll.max_ratio);
+			return -EINVAL;
+		}
+
+		target_ratio = hw->pll.min_ratio;
+		config = hw->config;
+	} else {
+		target_ratio = 0;
+		config = 0;
+	}
+
+	ivpu_dbg(PM, "PLL workpoint request: %d Hz\n", PLL_RATIO_TO_FREQ(target_ratio));
+
+	ret = ivpu_pll_cmd_send(vdev, hw->pll.min_ratio, hw->pll.max_ratio, target_ratio, config);
+	if (ret) {
+		ivpu_err(vdev, "Failed to send PLL workpoint request: %d\n", ret);
+		return ret;
+	}
+
+	ret = ivpu_pll_wait_for_lock(vdev, enable);
+	if (ret) {
+		ivpu_err(vdev, "Timed out waiting for PLL lock\n");
+		return ret;
+	}
+
+	if (enable) {
+		ret = ivpu_pll_wait_for_status_ready(vdev);
+		if (ret) {
+			ivpu_err(vdev, "Timed out waiting for PLL ready status\n");
+			return ret;
+		}
+	}
+
+	return 0;
+}
+
+static int ivpu_pll_enable(struct ivpu_device *vdev)
+{
+	return ivpu_pll_drive(vdev, true);
+}
+
+static int ivpu_pll_disable(struct ivpu_device *vdev)
+{
+	return ivpu_pll_drive(vdev, false);
+}
+
+static void ivpu_boot_host_ss_rst_clr_assert(struct ivpu_device *vdev)
+{
+	u32 val = REGV_RD32(MTL_VPU_HOST_SS_CPR_RST_CLR);
+
+	val = REG_SET_FLD(MTL_VPU_HOST_SS_CPR_RST_CLR, TOP_NOC, val);
+	val = REG_SET_FLD(MTL_VPU_HOST_SS_CPR_RST_CLR, DSS_MAS, val);
+	val = REG_SET_FLD(MTL_VPU_HOST_SS_CPR_RST_CLR, MSS_MAS, val);
+
+	REGV_WR32(MTL_VPU_HOST_SS_CPR_RST_CLR, val);
+}
+
+static void ivpu_boot_host_ss_rst_drive(struct ivpu_device *vdev, bool enable)
+{
+	u32 val = REGV_RD32(MTL_VPU_HOST_SS_CPR_RST_SET);
+
+	if (enable) {
+		val = REG_SET_FLD(MTL_VPU_HOST_SS_CPR_RST_SET, TOP_NOC, val);
+		val = REG_SET_FLD(MTL_VPU_HOST_SS_CPR_RST_SET, DSS_MAS, val);
+		val = REG_SET_FLD(MTL_VPU_HOST_SS_CPR_RST_SET, MSS_MAS, val);
+	} else {
+		val = REG_CLR_FLD(MTL_VPU_HOST_SS_CPR_RST_SET, TOP_NOC, val);
+		val = REG_CLR_FLD(MTL_VPU_HOST_SS_CPR_RST_SET, DSS_MAS, val);
+		val = REG_CLR_FLD(MTL_VPU_HOST_SS_CPR_RST_SET, MSS_MAS, val);
+	}
+
+	REGV_WR32(MTL_VPU_HOST_SS_CPR_RST_SET, val);
+}
+
+static void ivpu_boot_host_ss_clk_drive(struct ivpu_device *vdev, bool enable)
+{
+	u32 val = REGV_RD32(MTL_VPU_HOST_SS_CPR_CLK_SET);
+
+	if (enable) {
+		val = REG_SET_FLD(MTL_VPU_HOST_SS_CPR_CLK_SET, TOP_NOC, val);
+		val = REG_SET_FLD(MTL_VPU_HOST_SS_CPR_CLK_SET, DSS_MAS, val);
+		val = REG_SET_FLD(MTL_VPU_HOST_SS_CPR_CLK_SET, MSS_MAS, val);
+	} else {
+		val = REG_CLR_FLD(MTL_VPU_HOST_SS_CPR_CLK_SET, TOP_NOC, val);
+		val = REG_CLR_FLD(MTL_VPU_HOST_SS_CPR_CLK_SET, DSS_MAS, val);
+		val = REG_CLR_FLD(MTL_VPU_HOST_SS_CPR_CLK_SET, MSS_MAS, val);
+	}
+
+	REGV_WR32(MTL_VPU_HOST_SS_CPR_CLK_SET, val);
+}
+
+static int ivpu_boot_noc_qreqn_check(struct ivpu_device *vdev, u32 exp_val)
+{
+	u32 val = REGV_RD32(MTL_VPU_HOST_SS_NOC_QREQN);
+
+	if (!REG_TEST_FLD_NUM(MTL_VPU_HOST_SS_NOC_QREQN, TOP_SOCMMIO, exp_val, val))
+		return -EIO;
+
+	return 0;
+}
+
+static int ivpu_boot_noc_qacceptn_check(struct ivpu_device *vdev, u32 exp_val)
+{
+	u32 val = REGV_RD32(MTL_VPU_HOST_SS_NOC_QACCEPTN);
+
+	if (!REG_TEST_FLD_NUM(MTL_VPU_HOST_SS_NOC_QACCEPTN, TOP_SOCMMIO, exp_val, val))
+		return -EIO;
+
+	return 0;
+}
+
+static int ivpu_boot_noc_qdeny_check(struct ivpu_device *vdev, u32 exp_val)
+{
+	u32 val = REGV_RD32(MTL_VPU_HOST_SS_NOC_QDENY);
+
+	if (!REG_TEST_FLD_NUM(MTL_VPU_HOST_SS_NOC_QDENY, TOP_SOCMMIO, exp_val, val))
+		return -EIO;
+
+	return 0;
+}
+
+static int ivpu_boot_top_noc_qrenqn_check(struct ivpu_device *vdev, u32 exp_val)
+{
+	u32 val = REGV_RD32(MTL_VPU_TOP_NOC_QREQN);
+
+	if (!REG_TEST_FLD_NUM(MTL_VPU_TOP_NOC_QREQN, CPU_CTRL, exp_val, val) ||
+	    !REG_TEST_FLD_NUM(MTL_VPU_TOP_NOC_QREQN, HOSTIF_L2CACHE, exp_val, val))
+		return -EIO;
+
+	return 0;
+}
+
+static int ivpu_boot_top_noc_qacceptn_check(struct ivpu_device *vdev, u32 exp_val)
+{
+	u32 val = REGV_RD32(MTL_VPU_TOP_NOC_QACCEPTN);
+
+	if (!REG_TEST_FLD_NUM(MTL_VPU_TOP_NOC_QACCEPTN, CPU_CTRL, exp_val, val) ||
+	    !REG_TEST_FLD_NUM(MTL_VPU_TOP_NOC_QACCEPTN, HOSTIF_L2CACHE, exp_val, val))
+		return -EIO;
+
+	return 0;
+}
+
+static int ivpu_boot_top_noc_qdeny_check(struct ivpu_device *vdev, u32 exp_val)
+{
+	u32 val = REGV_RD32(MTL_VPU_TOP_NOC_QDENY);
+
+	if (!REG_TEST_FLD_NUM(MTL_VPU_TOP_NOC_QDENY, CPU_CTRL, exp_val, val) ||
+	    !REG_TEST_FLD_NUM(MTL_VPU_TOP_NOC_QDENY, HOSTIF_L2CACHE, exp_val, val))
+		return -EIO;
+
+	return 0;
+}
+
+static int ivpu_boot_host_ss_configure(struct ivpu_device *vdev)
+{
+	ivpu_boot_host_ss_rst_clr_assert(vdev);
+
+	return ivpu_boot_noc_qreqn_check(vdev, 0x0);
+}
+
+static void ivpu_boot_vpu_idle_gen_disable(struct ivpu_device *vdev)
+{
+	REGV_WR32(MTL_VPU_HOST_SS_AON_VPU_IDLE_GEN, 0x0);
+}
+
+static int ivpu_boot_host_ss_axi_drive(struct ivpu_device *vdev, bool enable)
+{
+	int ret;
+	u32 val;
+
+	val = REGV_RD32(MTL_VPU_HOST_SS_NOC_QREQN);
+	if (enable)
+		val = REG_SET_FLD(MTL_VPU_HOST_SS_NOC_QREQN, TOP_SOCMMIO, val);
+	else
+		val = REG_CLR_FLD(MTL_VPU_HOST_SS_NOC_QREQN, TOP_SOCMMIO, val);
+	REGV_WR32(MTL_VPU_HOST_SS_NOC_QREQN, val);
+
+	ret = ivpu_boot_noc_qacceptn_check(vdev, enable ? 0x1 : 0x0);
+	if (ret) {
+		ivpu_err(vdev, "Failed qacceptn check: %d\n", ret);
+		return ret;
+	}
+
+	ret = ivpu_boot_noc_qdeny_check(vdev, 0x0);
+	if (ret)
+		ivpu_err(vdev, "Failed qdeny check: %d\n", ret);
+
+	return ret;
+}
+
+static int ivpu_boot_host_ss_axi_enable(struct ivpu_device *vdev)
+{
+	return ivpu_boot_host_ss_axi_drive(vdev, true);
+}
+
+static int ivpu_boot_host_ss_axi_disable(struct ivpu_device *vdev)
+{
+	return ivpu_boot_host_ss_axi_drive(vdev, false);
+}
+
+static int ivpu_boot_host_ss_top_noc_drive(struct ivpu_device *vdev, bool enable)
+{
+	int ret;
+	u32 val;
+
+	val = REGV_RD32(MTL_VPU_TOP_NOC_QREQN);
+	if (enable) {
+		val = REG_SET_FLD(MTL_VPU_TOP_NOC_QREQN, CPU_CTRL, val);
+		val = REG_SET_FLD(MTL_VPU_TOP_NOC_QREQN, HOSTIF_L2CACHE, val);
+	} else {
+		val = REG_CLR_FLD(MTL_VPU_TOP_NOC_QREQN, CPU_CTRL, val);
+		val = REG_CLR_FLD(MTL_VPU_TOP_NOC_QREQN, HOSTIF_L2CACHE, val);
+	}
+	REGV_WR32(MTL_VPU_TOP_NOC_QREQN, val);
+
+	ret = ivpu_boot_top_noc_qacceptn_check(vdev, enable ? 0x1 : 0x0);
+	if (ret) {
+		ivpu_err(vdev, "Failed qacceptn check: %d\n", ret);
+		return ret;
+	}
+
+	ret = ivpu_boot_top_noc_qdeny_check(vdev, 0x0);
+	if (ret)
+		ivpu_err(vdev, "Failed qdeny check: %d\n", ret);
+
+	return ret;
+}
+
+static int ivpu_boot_host_ss_top_noc_enable(struct ivpu_device *vdev)
+{
+	return ivpu_boot_host_ss_top_noc_drive(vdev, true);
+}
+
+static int ivpu_boot_host_ss_top_noc_disable(struct ivpu_device *vdev)
+{
+	return ivpu_boot_host_ss_top_noc_drive(vdev, false);
+}
+
+static void ivpu_boot_pwr_island_trickle_drive(struct ivpu_device *vdev, bool enable)
+{
+	u32 val = REGV_RD32(MTL_VPU_HOST_SS_AON_PWR_ISLAND_TRICKLE_EN0);
+
+	if (enable)
+		val = REG_SET_FLD(MTL_VPU_HOST_SS_AON_PWR_ISLAND_TRICKLE_EN0, MSS_CPU, val);
+	else
+		val = REG_CLR_FLD(MTL_VPU_HOST_SS_AON_PWR_ISLAND_TRICKLE_EN0, MSS_CPU, val);
+
+	REGV_WR32(MTL_VPU_HOST_SS_AON_PWR_ISLAND_TRICKLE_EN0, val);
+}
+
+static void ivpu_boot_pwr_island_drive(struct ivpu_device *vdev, bool enable)
+{
+	u32 val = REGV_RD32(MTL_VPU_HOST_SS_AON_PWR_ISLAND_EN0);
+
+	if (enable)
+		val = REG_SET_FLD(MTL_VPU_HOST_SS_AON_PWR_ISLAND_EN0, MSS_CPU, val);
+	else
+		val = REG_CLR_FLD(MTL_VPU_HOST_SS_AON_PWR_ISLAND_EN0, MSS_CPU, val);
+
+	REGV_WR32(MTL_VPU_HOST_SS_AON_PWR_ISLAND_EN0, val);
+}
+
+static int ivpu_boot_wait_for_pwr_island_status(struct ivpu_device *vdev, u32 exp_val)
+{
+	/* FPGA model (UPF) is not power aware, skipped Power Island polling */
+	if (ivpu_is_fpga(vdev))
+		return 0;
+
+	return REGV_POLL_FLD(MTL_VPU_HOST_SS_AON_PWR_ISLAND_STATUS0, MSS_CPU,
+			     exp_val, PWR_ISLAND_STATUS_TIMEOUT_US);
+}
+
+static void ivpu_boot_pwr_island_isolation_drive(struct ivpu_device *vdev, bool enable)
+{
+	u32 val = REGV_RD32(MTL_VPU_HOST_SS_AON_PWR_ISO_EN0);
+
+	if (enable)
+		val = REG_SET_FLD(MTL_VPU_HOST_SS_AON_PWR_ISO_EN0, MSS_CPU, val);
+	else
+		val = REG_CLR_FLD(MTL_VPU_HOST_SS_AON_PWR_ISO_EN0, MSS_CPU, val);
+
+	REGV_WR32(MTL_VPU_HOST_SS_AON_PWR_ISO_EN0, val);
+}
+
+static void ivpu_boot_dpu_active_drive(struct ivpu_device *vdev, bool enable)
+{
+	u32 val = REGV_RD32(MTL_VPU_HOST_SS_AON_DPU_ACTIVE);
+
+	if (enable)
+		val = REG_SET_FLD(MTL_VPU_HOST_SS_AON_DPU_ACTIVE, DPU_ACTIVE, val);
+	else
+		val = REG_CLR_FLD(MTL_VPU_HOST_SS_AON_DPU_ACTIVE, DPU_ACTIVE, val);
+
+	REGV_WR32(MTL_VPU_HOST_SS_AON_DPU_ACTIVE, val);
+}
+
+static int ivpu_boot_pwr_domain_disable(struct ivpu_device *vdev)
+{
+	ivpu_boot_dpu_active_drive(vdev, false);
+	ivpu_boot_pwr_island_isolation_drive(vdev, true);
+	ivpu_boot_pwr_island_trickle_drive(vdev, false);
+	ivpu_boot_pwr_island_drive(vdev, false);
+
+	return ivpu_boot_wait_for_pwr_island_status(vdev, 0x0);
+}
+
+static int ivpu_boot_pwr_domain_enable(struct ivpu_device *vdev)
+{
+	int ret;
+
+	ivpu_boot_pwr_island_trickle_drive(vdev, true);
+	ivpu_boot_pwr_island_drive(vdev, true);
+
+	ret = ivpu_boot_wait_for_pwr_island_status(vdev, 0x1);
+	if (ret) {
+		ivpu_err(vdev, "Timed out waiting for power island status\n");
+		return ret;
+	}
+
+	ret = ivpu_boot_top_noc_qrenqn_check(vdev, 0x0);
+	if (ret) {
+		ivpu_err(vdev, "Failed qrenqn check %d\n", ret);
+		return ret;
+	}
+
+	ivpu_boot_host_ss_clk_drive(vdev, true);
+	ivpu_boot_pwr_island_isolation_drive(vdev, false);
+	ivpu_boot_host_ss_rst_drive(vdev, true);
+	ivpu_boot_dpu_active_drive(vdev, true);
+
+	return ret;
+}
+
+static void ivpu_boot_no_snoop_enable(struct ivpu_device *vdev)
+{
+	u32 val = REGV_RD32(MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES);
+
+	val = REG_SET_FLD(MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES, NOSNOOP_OVERRIDE_EN, val);
+	val = REG_SET_FLD(MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES, AW_NOSNOOP_OVERRIDE, val);
+	val = REG_SET_FLD(MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES, AR_NOSNOOP_OVERRIDE, val);
+
+	REGV_WR32(MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES, val);
+}
+
+static void ivpu_boot_tbu_mmu_enable(struct ivpu_device *vdev)
+{
+	u32 val = REGV_RD32(MTL_VPU_HOST_IF_TBU_MMUSSIDV);
+
+	if (ivpu_is_fpga(vdev)) {
+		val = REG_SET_FLD(MTL_VPU_HOST_IF_TBU_MMUSSIDV, TBU0_AWMMUSSIDV, val);
+		val = REG_SET_FLD(MTL_VPU_HOST_IF_TBU_MMUSSIDV, TBU0_ARMMUSSIDV, val);
+		val = REG_SET_FLD(MTL_VPU_HOST_IF_TBU_MMUSSIDV, TBU2_AWMMUSSIDV, val);
+		val = REG_SET_FLD(MTL_VPU_HOST_IF_TBU_MMUSSIDV, TBU2_ARMMUSSIDV, val);
+	} else {
+		val = REG_SET_FLD(MTL_VPU_HOST_IF_TBU_MMUSSIDV, TBU0_AWMMUSSIDV, val);
+		val = REG_SET_FLD(MTL_VPU_HOST_IF_TBU_MMUSSIDV, TBU0_ARMMUSSIDV, val);
+		val = REG_SET_FLD(MTL_VPU_HOST_IF_TBU_MMUSSIDV, TBU1_AWMMUSSIDV, val);
+		val = REG_SET_FLD(MTL_VPU_HOST_IF_TBU_MMUSSIDV, TBU1_ARMMUSSIDV, val);
+		val = REG_SET_FLD(MTL_VPU_HOST_IF_TBU_MMUSSIDV, TBU2_AWMMUSSIDV, val);
+		val = REG_SET_FLD(MTL_VPU_HOST_IF_TBU_MMUSSIDV, TBU2_ARMMUSSIDV, val);
+		val = REG_SET_FLD(MTL_VPU_HOST_IF_TBU_MMUSSIDV, TBU3_AWMMUSSIDV, val);
+		val = REG_SET_FLD(MTL_VPU_HOST_IF_TBU_MMUSSIDV, TBU3_ARMMUSSIDV, val);
+	}
+
+	REGV_WR32(MTL_VPU_HOST_IF_TBU_MMUSSIDV, val);
+}
+
+static void ivpu_boot_soc_cpu_boot(struct ivpu_device *vdev)
+{
+	u32 val;
+
+	val = REGV_RD32(MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC);
+	val = REG_SET_FLD(MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC, IRQI_RSTRUN0, val);
+
+	val = REG_CLR_FLD(MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC, IRQI_RSTVEC, val);
+	REGV_WR32(MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC, val);
+
+	val = REG_SET_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 = 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);
+}
+
+static int ivpu_boot_d0i3_drive(struct ivpu_device *vdev, bool enable)
+{
+	int ret;
+	u32 val;
+
+	ret = REGB_POLL_FLD(MTL_BUTTRESS_VPU_D0I3_CONTROL, INPROGRESS, 0, TIMEOUT_US);
+	if (ret) {
+		ivpu_err(vdev, "Failed to sync before D0i3 tansition: %d\n", ret);
+		return ret;
+	}
+
+	val = REGB_RD32(MTL_BUTTRESS_VPU_D0I3_CONTROL);
+	if (enable)
+		val = REG_SET_FLD(MTL_BUTTRESS_VPU_D0I3_CONTROL, I3, val);
+	else
+		val = REG_CLR_FLD(MTL_BUTTRESS_VPU_D0I3_CONTROL, I3, val);
+	REGB_WR32(MTL_BUTTRESS_VPU_D0I3_CONTROL, val);
+
+	ret = REGB_POLL_FLD(MTL_BUTTRESS_VPU_D0I3_CONTROL, INPROGRESS, 0, TIMEOUT_US);
+	if (ret)
+		ivpu_err(vdev, "Failed to sync after D0i3 tansition: %d\n", ret);
+
+	return ret;
+}
+
+static int ivpu_hw_mtl_info_init(struct ivpu_device *vdev)
+{
+	struct ivpu_hw_info *hw = vdev->hw;
+	u32 tile_fuse;
+
+	tile_fuse = REGB_RD32(MTL_BUTTRESS_TILE_FUSE);
+	if (!REG_TEST_FLD(MTL_BUTTRESS_TILE_FUSE, VALID, tile_fuse))
+		ivpu_warn(vdev, "Tile Fuse: Invalid (0x%x)\n", tile_fuse);
+
+	hw->tile_fuse = REG_GET_FLD(MTL_BUTTRESS_TILE_FUSE, SKU, tile_fuse);
+	switch (hw->tile_fuse) {
+	case TILE_FUSE_ENABLE_LOWER:
+		hw->sku = TILE_SKU_LOWER_MTL;
+		hw->config = WP_CONFIG_1_TILE_5_3_RATIO;
+		ivpu_dbg(MISC, "Tile Fuse: Enable Lower\n");
+		break;
+	case TILE_FUSE_ENABLE_UPPER:
+		hw->sku = TILE_SKU_UPPER_MTL;
+		hw->config = WP_CONFIG_1_TILE_4_3_RATIO;
+		ivpu_dbg(MISC, "Tile Fuse: Enable Upper\n");
+		break;
+	case TILE_FUSE_ENABLE_BOTH:
+		hw->sku = TILE_SKU_BOTH_MTL;
+		hw->config = WP_CONFIG_2_TILE_5_3_RATIO;
+		ivpu_dbg(MISC, "Tile Fuse: Enable Both\n");
+		break;
+	default:
+		hw->config = WP_CONFIG_0_TILE_PLL_OFF;
+		ivpu_dbg(MISC, "Tile Fuse: Disable\n");
+		break;
+	}
+
+	ivpu_hw_init_range(&hw->ranges.global_low, 0x80000000, SZ_512M);
+	ivpu_hw_init_range(&hw->ranges.global_high, 0x180000000, SZ_2M);
+	ivpu_hw_init_range(&hw->ranges.user_low, 0xc0000000, 255 * SZ_1M);
+	ivpu_hw_init_range(&hw->ranges.user_high, 0x180000000, SZ_2G);
+	hw->ranges.global_aliased_pio = hw->ranges.user_low;
+
+	return 0;
+}
+
+static int ivpu_hw_mtl_reset(struct ivpu_device *vdev)
+{
+	int ret;
+	u32 val;
+
+	if (IVPU_WA(punit_disabled))
+		return 0;
+
+	ret = REGB_POLL_FLD(MTL_BUTTRESS_VPU_IP_RESET, TRIGGER, 0, TIMEOUT_US);
+	if (ret) {
+		ivpu_err(vdev, "Timed out waiting for TRIGGER bit\n");
+		return ret;
+	}
+
+	val = REGB_RD32(MTL_BUTTRESS_VPU_IP_RESET);
+	val = REG_SET_FLD(MTL_BUTTRESS_VPU_IP_RESET, TRIGGER, val);
+	REGB_WR32(MTL_BUTTRESS_VPU_IP_RESET, val);
+
+	ret = REGB_POLL_FLD(MTL_BUTTRESS_VPU_IP_RESET, TRIGGER, 0, TIMEOUT_US);
+	if (ret)
+		ivpu_err(vdev, "Timed out waiting for RESET completion\n");
+
+	return ret;
+}
+
+static int ivpu_hw_mtl_d0i3_enable(struct ivpu_device *vdev)
+{
+	int ret;
+
+	ret = ivpu_boot_d0i3_drive(vdev, true);
+	if (ret)
+		ivpu_err(vdev, "Failed to enable D0i3: %d\n", ret);
+
+	udelay(5); /* VPU requires 5 us to complete the transition */
+
+	return ret;
+}
+
+static int ivpu_hw_mtl_d0i3_disable(struct ivpu_device *vdev)
+{
+	int ret;
+
+	ret = ivpu_boot_d0i3_drive(vdev, false);
+	if (ret)
+		ivpu_err(vdev, "Failed to disable D0i3: %d\n", ret);
+
+	return ret;
+}
+
+static int ivpu_hw_mtl_power_up(struct ivpu_device *vdev)
+{
+	int ret;
+
+	ivpu_hw_read_platform(vdev);
+	ivpu_hw_wa_init(vdev);
+	ivpu_hw_timeouts_init(vdev);
+
+	ret = ivpu_hw_mtl_reset(vdev);
+	if (ret)
+		ivpu_warn(vdev, "Failed to reset HW: %d\n", ret);
+
+	ret = ivpu_hw_mtl_d0i3_disable(vdev);
+	if (ret)
+		ivpu_warn(vdev, "Failed to disable D0I3: %d\n", ret);
+
+	ret = ivpu_pll_enable(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to enable PLL: %d\n", ret);
+		return ret;
+	}
+
+	ret = ivpu_boot_host_ss_configure(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to configure host SS: %d\n", ret);
+		return ret;
+	}
+
+	/*
+	 * The control circuitry for vpu_idle indication logic powers up active.
+	 * To ensure unnecessary low power mode signal from LRT during bring up,
+	 * KMD disables the circuitry prior to bringing up the Main Power island.
+	 */
+	ivpu_boot_vpu_idle_gen_disable(vdev);
+
+	ret = ivpu_boot_pwr_domain_enable(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to enable power domain: %d\n", ret);
+		return ret;
+	}
+
+	ret = ivpu_boot_host_ss_axi_enable(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to enable AXI: %d\n", ret);
+		return ret;
+	}
+
+	ret = ivpu_boot_host_ss_top_noc_enable(vdev);
+	if (ret)
+		ivpu_err(vdev, "Failed to enable TOP NOC: %d\n", ret);
+
+	return ret;
+}
+
+static int ivpu_hw_mtl_boot_fw(struct ivpu_device *vdev)
+{
+	ivpu_boot_no_snoop_enable(vdev);
+	ivpu_boot_tbu_mmu_enable(vdev);
+	ivpu_boot_soc_cpu_boot(vdev);
+
+	return 0;
+}
+
+static bool ivpu_hw_mtl_is_idle(struct ivpu_device *vdev)
+{
+	u32 val;
+
+	if (IVPU_WA(punit_disabled))
+		return true;
+
+	val = REGB_RD32(MTL_BUTTRESS_VPU_STATUS);
+	return REG_TEST_FLD(MTL_BUTTRESS_VPU_STATUS, READY, val) &&
+	       REG_TEST_FLD(MTL_BUTTRESS_VPU_STATUS, IDLE, val);
+}
+
+static int ivpu_hw_mtl_power_down(struct ivpu_device *vdev)
+{
+	int ret = 0;
+
+	/* FPGA requires manual clearing of IP_Reset bit by enabling quiescent state */
+	if (ivpu_is_fpga(vdev)) {
+		if (ivpu_boot_host_ss_top_noc_disable(vdev)) {
+			ivpu_err(vdev, "Failed to disable TOP NOC\n");
+			ret = -EIO;
+		}
+
+		if (ivpu_boot_host_ss_axi_disable(vdev)) {
+			ivpu_err(vdev, "Failed to disable AXI\n");
+			ret = -EIO;
+		}
+	}
+
+	if (ivpu_boot_pwr_domain_disable(vdev)) {
+		ivpu_err(vdev, "Failed to disable power domain\n");
+		ret = -EIO;
+	}
+
+	if (ivpu_pll_disable(vdev)) {
+		ivpu_err(vdev, "Failed to disable PLL\n");
+		ret = -EIO;
+	}
+
+	if (ivpu_hw_mtl_d0i3_enable(vdev))
+		ivpu_warn(vdev, "Failed to enable D0I3\n");
+
+	return ret;
+}
+
+static void ivpu_hw_mtl_wdt_disable(struct ivpu_device *vdev)
+{
+	u32 val;
+
+	/* Enable writing and set non-zero WDT value */
+	REGV_WR32(MTL_VPU_CPU_SS_TIM_SAFE, TIM_SAFE_ENABLE);
+	REGV_WR32(MTL_VPU_CPU_SS_TIM_WATCHDOG, TIM_WATCHDOG_RESET_VALUE);
+
+	/* Enable writing and disable watchdog timer */
+	REGV_WR32(MTL_VPU_CPU_SS_TIM_SAFE, TIM_SAFE_ENABLE);
+	REGV_WR32(MTL_VPU_CPU_SS_TIM_WDOG_EN, 0);
+
+	/* Now clear the timeout interrupt */
+	val = REGV_RD32(MTL_VPU_CPU_SS_TIM_GEN_CONFIG);
+	val = REG_CLR_FLD(MTL_VPU_CPU_SS_TIM_GEN_CONFIG, WDOG_TO_INT_CLR, val);
+	REGV_WR32(MTL_VPU_CPU_SS_TIM_GEN_CONFIG, val);
+}
+
+/* Register indirect accesses */
+static u32 ivpu_hw_mtl_reg_pll_freq_get(struct ivpu_device *vdev)
+{
+	u32 pll_curr_ratio;
+
+	pll_curr_ratio = REGB_RD32(MTL_BUTTRESS_CURRENT_PLL);
+	pll_curr_ratio &= MTL_BUTTRESS_CURRENT_PLL_RATIO_MASK;
+
+	if (!ivpu_is_silicon(vdev))
+		return PLL_SIMULATION_FREQ;
+
+	return PLL_RATIO_TO_FREQ(pll_curr_ratio);
+}
+
+static u32 ivpu_hw_mtl_reg_telemetry_offset_get(struct ivpu_device *vdev)
+{
+	return REGB_RD32(MTL_BUTTRESS_VPU_TELEMETRY_OFFSET);
+}
+
+static u32 ivpu_hw_mtl_reg_telemetry_size_get(struct ivpu_device *vdev)
+{
+	return REGB_RD32(MTL_BUTTRESS_VPU_TELEMETRY_SIZE);
+}
+
+static u32 ivpu_hw_mtl_reg_telemetry_enable_get(struct ivpu_device *vdev)
+{
+	return REGB_RD32(MTL_BUTTRESS_VPU_TELEMETRY_ENABLE);
+}
+
+static void ivpu_hw_mtl_reg_db_set(struct ivpu_device *vdev, u32 db_id)
+{
+	u32 reg_stride = MTL_VPU_CPU_SS_DOORBELL_1 - MTL_VPU_CPU_SS_DOORBELL_0;
+	u32 val = BIT(MTL_VPU_CPU_SS_DOORBELL_0_SET_OFFSET);
+
+	REGV_WR32I(MTL_VPU_CPU_SS_DOORBELL_0, reg_stride, db_id, val);
+}
+
+static u32 ivpu_hw_mtl_reg_ipc_rx_addr_get(struct ivpu_device *vdev)
+{
+	return REGV_RD32(MTL_VPU_HOST_SS_TIM_IPC_FIFO_ATM);
+}
+
+static u32 ivpu_hw_mtl_reg_ipc_rx_count_get(struct ivpu_device *vdev)
+{
+	u32 count = REGV_RD32_SILENT(MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT);
+
+	return REG_GET_FLD(MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT, FILL_LEVEL, count);
+}
+
+static void ivpu_hw_mtl_reg_ipc_tx_set(struct ivpu_device *vdev, u32 vpu_addr)
+{
+	REGV_WR32(MTL_VPU_CPU_SS_TIM_IPC_FIFO, vpu_addr);
+}
+
+static void ivpu_hw_mtl_irq_clear(struct ivpu_device *vdev)
+{
+	REGV_WR64(MTL_VPU_HOST_SS_ICB_CLEAR_0, ICB_0_1_IRQ_MASK);
+}
+
+static void ivpu_hw_mtl_irq_enable(struct ivpu_device *vdev)
+{
+	REGV_WR32(MTL_VPU_HOST_SS_FW_SOC_IRQ_EN, ITF_FIREWALL_VIOLATION_MASK);
+	REGV_WR64(MTL_VPU_HOST_SS_ICB_ENABLE_0, ICB_0_1_IRQ_MASK);
+	REGB_WR32(MTL_BUTTRESS_LOCAL_INT_MASK, (u32)~(BIT(1) | BIT(2)));
+	REGB_WR32(MTL_BUTTRESS_GLOBAL_INT_MASK, 0x0);
+}
+
+static void ivpu_hw_mtl_irq_disable(struct ivpu_device *vdev)
+{
+	REGB_WR32(MTL_BUTTRESS_GLOBAL_INT_MASK, 0x1);
+	REGB_WR32(MTL_BUTTRESS_LOCAL_INT_MASK, BUTTRESS_IRQ_MASK);
+	REGV_WR64(MTL_VPU_HOST_SS_ICB_ENABLE_0, 0x0ull);
+	REGB_WR32(MTL_VPU_HOST_SS_FW_SOC_IRQ_EN, 0x0ul);
+}
+
+static irqreturn_t ivpu_hw_mtl_irq_wdt_nce_handler(struct ivpu_device *vdev)
+{
+	ivpu_err_ratelimited(vdev, "WDT NCE irq\n");
+
+	return IRQ_HANDLED;
+}
+
+static irqreturn_t ivpu_hw_mtl_irq_wdt_mss_handler(struct ivpu_device *vdev)
+{
+	ivpu_err_ratelimited(vdev, "WDT MSS irq\n");
+
+	ivpu_hw_wdt_disable(vdev);
+
+	return IRQ_HANDLED;
+}
+
+static irqreturn_t ivpu_hw_mtl_irq_noc_firewall_handler(struct ivpu_device *vdev)
+{
+	ivpu_err_ratelimited(vdev, "NOC Firewall irq\n");
+
+	return IRQ_HANDLED;
+}
+
+/* Handler for IRQs from VPU core (irqV) */
+static irqreturn_t ivpu_hw_mtl_irqv_handler(struct ivpu_device *vdev, int irq)
+{
+	irqreturn_t ret = IRQ_HANDLED;
+	u32 status = REGV_RD32(MTL_VPU_HOST_SS_ICB_STATUS_0) & ICB_0_IRQ_MASK;
+
+	REGV_WR32(MTL_VPU_HOST_SS_ICB_CLEAR_0, status);
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, CPU_INT_REDIRECT_0_INT, status))
+		ret &= ivpu_hw_mtl_irq_wdt_mss_handler(vdev);
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, CPU_INT_REDIRECT_1_INT, status))
+		ret &= ivpu_hw_mtl_irq_wdt_nce_handler(vdev);
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, NOC_FIREWALL_INT, status))
+		ret &= ivpu_hw_mtl_irq_noc_firewall_handler(vdev);
+
+	return ret;
+}
+
+/* Handler for IRQs from Buttress core (irqB) */
+static irqreturn_t ivpu_hw_mtl_irqb_handler(struct ivpu_device *vdev, int irq)
+{
+	u32 status = REGB_RD32(MTL_BUTTRESS_INTERRUPT_STAT) & BUTTRESS_IRQ_MASK;
+
+	REGB_WR32(MTL_BUTTRESS_INTERRUPT_STAT, status);
+
+	if (REG_TEST_FLD(MTL_BUTTRESS_INTERRUPT_STAT, FREQ_CHANGE, status))
+		ivpu_dbg(IRQ, "FREQ_CHANGE");
+
+	if (REG_TEST_FLD(MTL_BUTTRESS_INTERRUPT_STAT, ATS_ERR, status)) {
+		ivpu_dbg(IRQ, "ATS_ERR 0x%016llx", REGB_RD64(MTL_BUTTRESS_ATS_ERR_LOG_0));
+		REGB_WR32(MTL_BUTTRESS_ATS_ERR_CLEAR, 0x1);
+	}
+
+	if (REG_TEST_FLD(MTL_BUTTRESS_INTERRUPT_STAT, UFI_ERR, status)) {
+		ivpu_dbg(IRQ, "UFI_ERR 0x%08x", REGB_RD32(MTL_BUTTRESS_UFI_ERR_LOG));
+		REGB_WR32(MTL_BUTTRESS_UFI_ERR_CLEAR, 0x1);
+	}
+
+	return IRQ_HANDLED;
+}
+
+static irqreturn_t ivpu_hw_mtl_irq_handler(int irq, void *ptr)
+{
+	struct ivpu_device *vdev = ptr;
+	irqreturn_t ret_irqv;
+	irqreturn_t ret_irqb;
+
+	ivpu_hw_mtl_irq_disable(vdev);
+
+	ret_irqv = ivpu_hw_mtl_irqv_handler(vdev, irq);
+	ret_irqb = ivpu_hw_mtl_irqb_handler(vdev, irq);
+
+	ivpu_hw_mtl_irq_enable(vdev);
+
+	return ret_irqv & ret_irqb;
+}
+
+static void ivpu_hw_mtl_diagnose_tdr(struct ivpu_device *vdev)
+{
+	u32 status = REGV_RD32(MTL_VPU_HOST_SS_ICB_STATUS_0) & ICB_0_IRQ_MASK;
+
+	if (ivpu_hw_mtl_reg_ipc_rx_count_get(vdev))
+		ivpu_err(vdev, "IPC FIFO queue not empty, missed IPC IRQ");
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, CPU_INT_REDIRECT_0_INT, status))
+		ivpu_err(vdev, "WDT MSS timeout detected\n");
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, CPU_INT_REDIRECT_1_INT, status))
+		ivpu_err(vdev, "WDT NCE timeout detected\n");
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, NOC_FIREWALL_INT, status))
+		ivpu_err(vdev, "NOC Firewall irq detected\n");
+}
+
+const struct ivpu_hw_ops ivpu_hw_mtl_ops = {
+	.info_init = ivpu_hw_mtl_info_init,
+	.power_up = ivpu_hw_mtl_power_up,
+	.is_idle = ivpu_hw_mtl_is_idle,
+	.power_down = ivpu_hw_mtl_power_down,
+	.boot_fw = ivpu_hw_mtl_boot_fw,
+	.wdt_disable = ivpu_hw_mtl_wdt_disable,
+	.diagnose_tdr = ivpu_hw_mtl_diagnose_tdr,
+	.reg_pll_freq_get = ivpu_hw_mtl_reg_pll_freq_get,
+	.reg_telemetry_offset_get = ivpu_hw_mtl_reg_telemetry_offset_get,
+	.reg_telemetry_size_get = ivpu_hw_mtl_reg_telemetry_size_get,
+	.reg_telemetry_enable_get = ivpu_hw_mtl_reg_telemetry_enable_get,
+	.reg_db_set = ivpu_hw_mtl_reg_db_set,
+	.reg_ipc_rx_addr_get = ivpu_hw_mtl_reg_ipc_rx_addr_get,
+	.reg_ipc_rx_count_get = ivpu_hw_mtl_reg_ipc_rx_count_get,
+	.reg_ipc_tx_set = ivpu_hw_mtl_reg_ipc_tx_set,
+	.irq_clear = ivpu_hw_mtl_irq_clear,
+	.irq_enable = ivpu_hw_mtl_irq_enable,
+	.irq_disable = ivpu_hw_mtl_irq_disable,
+	.irq_handler = ivpu_hw_mtl_irq_handler,
+};
drivers/gpu/drm/ivpu/ivpu_hw_mtl_reg.h new file +468 -0
new file mode 100644
index 000000000000..2d591d0d3f15
@@ -0,0 +1,468 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#ifndef __IVPU_HW_MTL_REG_H__
+#define __IVPU_HW_MTL_REG_H__
+
+#define MTL_BUTTRESS_INTERRUPT_TYPE					0x00000000u
+
+#define MTL_BUTTRESS_INTERRUPT_STAT					0x00000004u
+#define MTL_BUTTRESS_INTERRUPT_STAT_FREQ_CHANGE_SHIFT			0u
+#define MTL_BUTTRESS_INTERRUPT_STAT_FREQ_CHANGE_MASK			0x00000001u
+#define MTL_BUTTRESS_INTERRUPT_STAT_ATS_ERR_SHIFT			1u
+#define MTL_BUTTRESS_INTERRUPT_STAT_ATS_ERR_MASK			0x00000002u
+#define MTL_BUTTRESS_INTERRUPT_STAT_UFI_ERR_SHIFT			2u
+#define MTL_BUTTRESS_INTERRUPT_STAT_UFI_ERR_MASK			0x00000004u
+
+#define MTL_BUTTRESS_WP_REQ_PAYLOAD0					0x00000008u
+#define MTL_BUTTRESS_WP_REQ_PAYLOAD0_MIN_RATIO_SHIFT			0u
+#define MTL_BUTTRESS_WP_REQ_PAYLOAD0_MIN_RATIO_MASK			0x0000ffffu
+#define MTL_BUTTRESS_WP_REQ_PAYLOAD0_MAX_RATIO_SHIFT			16u
+#define MTL_BUTTRESS_WP_REQ_PAYLOAD0_MAX_RATIO_MASK			0xffff0000u
+
+#define MTL_BUTTRESS_WP_REQ_PAYLOAD1					0x0000000cu
+#define MTL_BUTTRESS_WP_REQ_PAYLOAD1_TARGET_RATIO_SHIFT			0u
+#define MTL_BUTTRESS_WP_REQ_PAYLOAD1_TARGET_RATIO_MASK			0x0000ffffu
+#define MTL_BUTTRESS_WP_REQ_PAYLOAD1_EPP_SHIFT				16u
+#define MTL_BUTTRESS_WP_REQ_PAYLOAD1_EPP_MASK				0xffff0000u
+
+#define MTL_BUTTRESS_WP_REQ_PAYLOAD2					0x00000010u
+#define MTL_BUTTRESS_WP_REQ_PAYLOAD2_CONFIG_SHIFT			0u
+#define MTL_BUTTRESS_WP_REQ_PAYLOAD2_CONFIG_MASK			0x0000ffffu
+
+#define MTL_BUTTRESS_WP_REQ_CMD						0x00000014u
+#define MTL_BUTTRESS_WP_REQ_CMD_SEND_SHIFT				0u
+#define MTL_BUTTRESS_WP_REQ_CMD_SEND_MASK				0x00000001u
+
+#define MTL_BUTTRESS_WP_DOWNLOAD					0x00000018u
+#define MTL_BUTTRESS_WP_DOWNLOAD_TARGET_RATIO_SHIFT			0u
+#define MTL_BUTTRESS_WP_DOWNLOAD_TARGET_RATIO_MASK			0x0000ffffu
+
+#define MTL_BUTTRESS_CURRENT_PLL					0x0000001cu
+#define MTL_BUTTRESS_CURRENT_PLL_RATIO_SHIFT				0u
+#define MTL_BUTTRESS_CURRENT_PLL_RATIO_MASK				0x0000ffffu
+
+#define MTL_BUTTRESS_PLL_ENABLE						0x00000020u
+
+#define MTL_BUTTRESS_FMIN_FUSE						0x00000024u
+#define MTL_BUTTRESS_FMIN_FUSE_RATIO_SHIFT				0u
+#define MTL_BUTTRESS_FMIN_FUSE_RATIO_MASK				0x0000ffffu
+
+#define MTL_BUTTRESS_FMAX_FUSE						0x00000028u
+#define MTL_BUTTRESS_FMAX_FUSE_RATIO_SHIFT				0u
+#define MTL_BUTTRESS_FMAX_FUSE_RATIO_MASK				0x0000ffffu
+
+#define MTL_BUTTRESS_TILE_FUSE						0x0000002cu
+#define MTL_BUTTRESS_TILE_FUSE_VALID_SHIFT				0u
+#define MTL_BUTTRESS_TILE_FUSE_VALID_MASK				0x00000001u
+#define MTL_BUTTRESS_TILE_FUSE_SKU_SHIFT				1u
+#define MTL_BUTTRESS_TILE_FUSE_SKU_MASK					0x00000006u
+
+#define MTL_BUTTRESS_LOCAL_INT_MASK					0x00000030u
+#define MTL_BUTTRESS_GLOBAL_INT_MASK					0x00000034u
+
+#define MTL_BUTTRESS_PLL_STATUS						0x00000040u
+#define MTL_BUTTRESS_PLL_STATUS_LOCK_SHIFT				1u
+#define MTL_BUTTRESS_PLL_STATUS_LOCK_MASK				0x00000002u
+
+#define MTL_BUTTRESS_VPU_STATUS						0x00000044u
+#define MTL_BUTTRESS_VPU_STATUS_READY_SHIFT				0u
+#define MTL_BUTTRESS_VPU_STATUS_READY_MASK				0x00000001u
+#define MTL_BUTTRESS_VPU_STATUS_IDLE_SHIFT				1u
+#define MTL_BUTTRESS_VPU_STATUS_IDLE_MASK				0x00000002u
+
+#define MTL_BUTTRESS_VPU_D0I3_CONTROL					0x00000060u
+#define MTL_BUTTRESS_VPU_D0I3_CONTROL_INPROGRESS_SHIFT			0u
+#define MTL_BUTTRESS_VPU_D0I3_CONTROL_INPROGRESS_MASK			0x00000001u
+#define MTL_BUTTRESS_VPU_D0I3_CONTROL_I3_SHIFT				2u
+#define MTL_BUTTRESS_VPU_D0I3_CONTROL_I3_MASK				0x00000004u
+
+#define MTL_BUTTRESS_VPU_IP_RESET					0x00000050u
+#define MTL_BUTTRESS_VPU_IP_RESET_TRIGGER_SHIFT				0u
+#define MTL_BUTTRESS_VPU_IP_RESET_TRIGGER_MASK				0x00000001u
+
+#define MTL_BUTTRESS_VPU_TELEMETRY_OFFSET				0x00000080u
+#define MTL_BUTTRESS_VPU_TELEMETRY_SIZE					0x00000084u
+#define MTL_BUTTRESS_VPU_TELEMETRY_ENABLE				0x00000088u
+
+#define MTL_BUTTRESS_ATS_ERR_LOG_0					0x000000a0u
+#define MTL_BUTTRESS_ATS_ERR_LOG_1					0x000000a4u
+#define MTL_BUTTRESS_ATS_ERR_CLEAR					0x000000a8u
+#define MTL_BUTTRESS_UFI_ERR_LOG					0x000000b0u
+#define MTL_BUTTRESS_UFI_ERR_CLEAR					0x000000b4u
+
+#define MTL_VPU_HOST_SS_CPR_CLK_SET					0x00000084u
+#define MTL_VPU_HOST_SS_CPR_CLK_SET_TOP_NOC_SHIFT			1u
+#define MTL_VPU_HOST_SS_CPR_CLK_SET_TOP_NOC_MASK			0x00000002u
+#define MTL_VPU_HOST_SS_CPR_CLK_SET_DSS_MAS_SHIFT			10u
+#define MTL_VPU_HOST_SS_CPR_CLK_SET_DSS_MAS_MASK			0x00000400u
+#define MTL_VPU_HOST_SS_CPR_CLK_SET_MSS_MAS_SHIFT			11u
+#define MTL_VPU_HOST_SS_CPR_CLK_SET_MSS_MAS_MASK			0x00000800u
+
+#define MTL_VPU_HOST_SS_CPR_RST_SET					0x00000094u
+#define MTL_VPU_HOST_SS_CPR_RST_SET_TOP_NOC_SHIFT			1u
+#define MTL_VPU_HOST_SS_CPR_RST_SET_TOP_NOC_MASK			0x00000002u
+#define MTL_VPU_HOST_SS_CPR_RST_SET_DSS_MAS_SHIFT			10u
+#define MTL_VPU_HOST_SS_CPR_RST_SET_DSS_MAS_MASK			0x00000400u
+#define MTL_VPU_HOST_SS_CPR_RST_SET_MSS_MAS_SHIFT			11u
+#define MTL_VPU_HOST_SS_CPR_RST_SET_MSS_MAS_MASK			0x00000800u
+
+#define MTL_VPU_HOST_SS_CPR_RST_CLR					0x00000098u
+#define MTL_VPU_HOST_SS_CPR_RST_CLR_TOP_NOC_SHIFT			1u
+#define MTL_VPU_HOST_SS_CPR_RST_CLR_TOP_NOC_MASK			0x00000002u
+#define MTL_VPU_HOST_SS_CPR_RST_CLR_DSS_MAS_SHIFT			10u
+#define MTL_VPU_HOST_SS_CPR_RST_CLR_DSS_MAS_MASK			0x00000400u
+#define MTL_VPU_HOST_SS_CPR_RST_CLR_MSS_MAS_SHIFT			11u
+#define MTL_VPU_HOST_SS_CPR_RST_CLR_MSS_MAS_MASK			0x00000800u
+
+#define MTL_VPU_HOST_SS_HW_VERSION					0x00000108u
+#define MTL_VPU_HOST_SS_HW_VERSION_SOC_REVISION_SHIFT			0u
+#define MTL_VPU_HOST_SS_HW_VERSION_SOC_REVISION_MASK			0x000000ffu
+#define MTL_VPU_HOST_SS_HW_VERSION_SOC_NUMBER_SHIFT			8u
+#define MTL_VPU_HOST_SS_HW_VERSION_SOC_NUMBER_MASK			0x0000ff00u
+#define MTL_VPU_HOST_SS_HW_VERSION_VPU_GENERATION_SHIFT			16u
+#define MTL_VPU_HOST_SS_HW_VERSION_VPU_GENERATION_MASK			0x00ff0000u
+
+#define MTL_VPU_HOST_SS_SW_VERSION					0x0000010cu
+
+#define MTL_VPU_HOST_SS_GEN_CTRL					0x00000118u
+#define MTL_VPU_HOST_SS_GEN_CTRL_PS_SHIFT				0x0000001du
+#define MTL_VPU_HOST_SS_GEN_CTRL_PS_MASK				0xe0000000u
+
+#define MTL_VPU_HOST_SS_NOC_QREQN					0x00000154u
+#define MTL_VPU_HOST_SS_NOC_QREQN_TOP_SOCMMIO_SHIFT			0u
+#define MTL_VPU_HOST_SS_NOC_QREQN_TOP_SOCMMIO_MASK			0x00000001u
+
+#define MTL_VPU_HOST_SS_NOC_QACCEPTN					0x00000158u
+#define MTL_VPU_HOST_SS_NOC_QACCEPTN_TOP_SOCMMIO_SHIFT			0u
+#define MTL_VPU_HOST_SS_NOC_QACCEPTN_TOP_SOCMMIO_MASK			0x00000001u
+
+#define MTL_VPU_HOST_SS_NOC_QDENY					0x0000015cu
+#define MTL_VPU_HOST_SS_NOC_QDENY_TOP_SOCMMIO_SHIFT			0u
+#define MTL_VPU_HOST_SS_NOC_QDENY_TOP_SOCMMIO_MASK			0x00000001u
+
+#define MTL_VPU_TOP_NOC_QREQN						0x00000160u
+#define MTL_VPU_TOP_NOC_QREQN_CPU_CTRL_SHIFT				0u
+#define MTL_VPU_TOP_NOC_QREQN_CPU_CTRL_MASK				0x00000001u
+#define MTL_VPU_TOP_NOC_QREQN_HOSTIF_L2CACHE_SHIFT			1u
+#define MTL_VPU_TOP_NOC_QREQN_HOSTIF_L2CACHE_MASK			0x00000002u
+
+#define MTL_VPU_TOP_NOC_QACCEPTN					0x00000164u
+#define MTL_VPU_TOP_NOC_QACCEPTN_CPU_CTRL_SHIFT				0u
+#define MTL_VPU_TOP_NOC_QACCEPTN_CPU_CTRL_MASK				0x00000001u
+#define MTL_VPU_TOP_NOC_QACCEPTN_HOSTIF_L2CACHE_SHIFT			1u
+#define MTL_VPU_TOP_NOC_QACCEPTN_HOSTIF_L2CACHE_MASK			0x00000002u
+
+#define MTL_VPU_TOP_NOC_QDENY						0x00000168u
+#define MTL_VPU_TOP_NOC_QDENY_CPU_CTRL_SHIFT				0u
+#define MTL_VPU_TOP_NOC_QDENY_CPU_CTRL_MASK				0x00000001u
+#define MTL_VPU_TOP_NOC_QDENY_HOSTIF_L2CACHE_SHIFT			1u
+#define MTL_VPU_TOP_NOC_QDENY_HOSTIF_L2CACHE_MASK			0x00000002u
+
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN					0x00000170u
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN_CSS_ROM_CMX_SHIFT			0u
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN_CSS_ROM_CMX_MASK			0x00000001u
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN_CSS_DBG_SHIFT			1u
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN_CSS_DBG_MASK			0x00000002u
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN_CSS_CTRL_SHIFT			2u
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN_CSS_CTRL_MASK			0x00000004u
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN_DEC400_SHIFT			3u
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN_DEC400_MASK			0x00000008u
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN_MSS_NCE_SHIFT			4u
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN_MSS_NCE_MASK			0x00000010u
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN_MSS_MBI_SHIFT			5u
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN_MSS_MBI_MASK			0x00000020u
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN_MSS_MBI_CMX_SHIFT			6u
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN_MSS_MBI_CMX_MASK			0x00000040u
+
+#define MTL_VPU_HOST_SS_ICB_STATUS_0					0x00010210u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_TIMER_0_INT_SHIFT			0u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_TIMER_0_INT_MASK			0x00000001u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_TIMER_1_INT_SHIFT			1u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_TIMER_1_INT_MASK			0x00000002u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_TIMER_2_INT_SHIFT			2u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_TIMER_2_INT_MASK			0x00000004u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_TIMER_3_INT_SHIFT			3u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_TIMER_3_INT_MASK			0x00000008u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_HOST_IPC_FIFO_INT_SHIFT		4u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_HOST_IPC_FIFO_INT_MASK		0x00000010u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_MMU_IRQ_0_INT_SHIFT		5u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_MMU_IRQ_0_INT_MASK			0x00000020u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_MMU_IRQ_1_INT_SHIFT		6u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_MMU_IRQ_1_INT_MASK			0x00000040u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_MMU_IRQ_2_INT_SHIFT		7u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_MMU_IRQ_2_INT_MASK			0x00000080u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_NOC_FIREWALL_INT_SHIFT		8u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_NOC_FIREWALL_INT_MASK		0x00000100u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_CPU_INT_REDIRECT_0_INT_SHIFT	30u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_CPU_INT_REDIRECT_0_INT_MASK	0x40000000u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_CPU_INT_REDIRECT_1_INT_SHIFT	31u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_CPU_INT_REDIRECT_1_INT_MASK	0x80000000u
+
+#define MTL_VPU_HOST_SS_ICB_STATUS_1					0x00010214u
+#define MTL_VPU_HOST_SS_ICB_STATUS_1_CPU_INT_REDIRECT_2_INT_SHIFT	0u
+#define MTL_VPU_HOST_SS_ICB_STATUS_1_CPU_INT_REDIRECT_2_INT_MASK	0x00000001u
+#define MTL_VPU_HOST_SS_ICB_STATUS_1_CPU_INT_REDIRECT_3_INT_SHIFT	1u
+#define MTL_VPU_HOST_SS_ICB_STATUS_1_CPU_INT_REDIRECT_3_INT_MASK	0x00000002u
+#define MTL_VPU_HOST_SS_ICB_STATUS_1_CPU_INT_REDIRECT_4_INT_SHIFT	2u
+#define MTL_VPU_HOST_SS_ICB_STATUS_1_CPU_INT_REDIRECT_4_INT_MASK	0x00000004u
+
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0					0x00010220u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_TIMER_0_INT_SHIFT			0u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_TIMER_0_INT_MASK			0x00000001u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_TIMER_1_INT_SHIFT			1u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_TIMER_1_INT_MASK			0x00000002u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_TIMER_2_INT_SHIFT			2u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_TIMER_2_INT_MASK			0x00000004u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_TIMER_3_INT_SHIFT			3u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_TIMER_3_INT_MASK			0x00000008u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_HOST_IPC_FIFO_INT_SHIFT		4u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_HOST_IPC_FIFO_INT_MASK		0x00000010u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_MMU_IRQ_0_INT_SHIFT			5u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_MMU_IRQ_0_INT_MASK			0x00000020u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_MMU_IRQ_1_INT_SHIFT			6u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_MMU_IRQ_1_INT_MASK			0x00000040u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_MMU_IRQ_2_INT_SHIFT			7u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_MMU_IRQ_2_INT_MASK			0x00000080u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_NOC_FIREWALL_INT_SHIFT		8u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_NOC_FIREWALL_INT_MASK		0x00000100u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_CPU_INT_REDIRECT_0_INT_SHIFT	30u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_CPU_INT_REDIRECT_0_INT_MASK		0x40000000u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_CPU_INT_REDIRECT_1_INT_SHIFT	31u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_CPU_INT_REDIRECT_1_INT_MASK		0x80000000u
+
+#define MTL_VPU_HOST_SS_ICB_CLEAR_1					0x00010224u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_1_CPU_INT_REDIRECT_2_INT_SHIFT	0u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_1_CPU_INT_REDIRECT_2_INT_MASK		0x00000001u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_1_CPU_INT_REDIRECT_3_INT_SHIFT	1u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_1_CPU_INT_REDIRECT_3_INT_MASK		0x00000002u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_1_CPU_INT_REDIRECT_4_INT_SHIFT	2u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_1_CPU_INT_REDIRECT_4_INT_MASK		0x00000004u
+
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0					0x00010240u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_TIMER_0_INT_SHIFT			0u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_TIMER_0_INT_MASK			0x00000001u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_TIMER_1_INT_SHIFT			1u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_TIMER_1_INT_MASK			0x00000002u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_TIMER_2_INT_SHIFT			2u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_TIMER_2_INT_MASK			0x00000004u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_TIMER_3_INT_SHIFT			3u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_TIMER_3_INT_MASK			0x00000008u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_HOST_IPC_FIFO_INT_SHIFT		4u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_HOST_IPC_FIFO_INT_MASK		0x00000010u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_MMU_IRQ_0_INT_SHIFT		5u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_MMU_IRQ_0_INT_MASK			0x00000020u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_MMU_IRQ_1_INT_SHIFT		6u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_MMU_IRQ_1_INT_MASK			0x00000040u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_MMU_IRQ_2_INT_SHIFT		7u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_MMU_IRQ_2_INT_MASK			0x00000080u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_NOC_FIREWALL_INT_SHIFT		8u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_NOC_FIREWALL_INT_MASK		0x00000100u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_CPU_INT_REDIRECT_0_INT_SHIFT	30u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_CPU_INT_REDIRECT_0_INT_MASK	0x40000000u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_CPU_INT_REDIRECT_1_INT_SHIFT	31u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_CPU_INT_REDIRECT_1_INT_MASK	0x80000000u
+
+#define MTL_VPU_HOST_SS_ICB_ENABLE_1					0x00010244u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_1_CPU_INT_REDIRECT_2_INT_SHIFT	0u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_1_CPU_INT_REDIRECT_2_INT_MASK	0x00000001u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_1_CPU_INT_REDIRECT_3_INT_SHIFT	1u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_1_CPU_INT_REDIRECT_3_INT_MASK	0x00000002u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_1_CPU_INT_REDIRECT_4_INT_SHIFT	2u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_1_CPU_INT_REDIRECT_4_INT_MASK	0x00000004u
+
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_ATM				0x000200f4u
+
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT				0x000200fcu
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT_DEFAULT			0x00000000u
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT_READ_POINTER_SHIFT		0u
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT_READ_POINTER_MASK		0x000000ffu
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT_READ_POINTER_EFAULT		0x00000000u
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT_WRITE_POINTER_SHIFT		8u
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT_WRITE_POINTER_MASK		0x0000ff00u
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT_WRITE_POINTER_DEFAULT		0x00000000u
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT_FILL_LEVEL_SHIFT		16u
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT_FILL_LEVEL_MASK		0x00ff0000u
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT_FILL_LEVEL_DEFAULT		0x00000000u
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT_RSVD0_SHIFT			24u
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT_RSVD0_MASK			0xff000000u
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT_RSVD0_DEFAULT			0x00000000u
+
+#define MTL_VPU_HOST_SS_AON_PWR_ISO_EN0					0x00030020u
+#define MTL_VPU_HOST_SS_AON_PWR_ISO_EN0_MSS_CPU_SHIFT			3u
+#define MTL_VPU_HOST_SS_AON_PWR_ISO_EN0_MSS_CPU_MASK			0x00000008u
+
+#define MTL_VPU_HOST_SS_AON_PWR_ISLAND_EN0				0x00030024u
+#define MTL_VPU_HOST_SS_AON_PWR_ISLAND_EN0_MSS_CPU_SHIFT		3u
+#define MTL_VPU_HOST_SS_AON_PWR_ISLAND_EN0_MSS_CPU_MASK			0x00000008u
+
+#define MTL_VPU_HOST_SS_AON_PWR_ISLAND_TRICKLE_EN0			0x00030028u
+#define MTL_VPU_HOST_SS_AON_PWR_ISLAND_TRICKLE_EN0_MSS_CPU_SHIFT	3u
+#define MTL_VPU_HOST_SS_AON_PWR_ISLAND_TRICKLE_EN0_MSS_CPU_MASK		0x00000008u
+
+#define MTL_VPU_HOST_SS_AON_PWR_ISLAND_STATUS0				0x0003002cu
+#define MTL_VPU_HOST_SS_AON_PWR_ISLAND_STATUS0_MSS_CPU_SHIFT		3u
+#define MTL_VPU_HOST_SS_AON_PWR_ISLAND_STATUS0_MSS_CPU_MASK		0x00000008u
+
+#define MTL_VPU_HOST_SS_AON_VPU_IDLE_GEN				0x00030200u
+#define MTL_VPU_HOST_SS_AON_VPU_IDLE_GEN_EN_SHIFT			0u
+#define MTL_VPU_HOST_SS_AON_VPU_IDLE_GEN_EN_MASK			0x00000001u
+
+#define MTL_VPU_HOST_SS_AON_DPU_ACTIVE					0x00030204u
+#define MTL_VPU_HOST_SS_AON_DPU_ACTIVE_DPU_ACTIVE_SHIFT			0u
+#define MTL_VPU_HOST_SS_AON_DPU_ACTIVE_DPU_ACTIVE_MASK			0x00000001u
+
+#define MTL_VPU_HOST_SS_LOADING_ADDRESS_LO				0x00041040u
+#define MTL_VPU_HOST_SS_LOADING_ADDRESS_LO_DONE_SHIFT			0u
+#define MTL_VPU_HOST_SS_LOADING_ADDRESS_LO_DONE_MASK			0x00000001u
+#define MTL_VPU_HOST_SS_LOADING_ADDRESS_LO_IOSF_RS_ID_SHIFT		1u
+#define MTL_VPU_HOST_SS_LOADING_ADDRESS_LO_IOSF_RS_ID_MASK		0x00000006u
+#define MTL_VPU_HOST_SS_LOADING_ADDRESS_LO_IMAGE_LOCATION_SHIFT		3u
+#define MTL_VPU_HOST_SS_LOADING_ADDRESS_LO_IMAGE_LOCATION_MASK		0xfffffff8u
+
+#define MTL_VPU_HOST_SS_WORKPOINT_CONFIG_MIRROR				0x00082020u
+#define MTL_VPU_HOST_SS_WORKPOINT_CONFIG_MIRROR_FINAL_PLL_FREQ_SHIFT	0u
+#define MTL_VPU_HOST_SS_WORKPOINT_CONFIG_MIRROR_FINAL_PLL_FREQ_MASK	0x0000ffffu
+#define MTL_VPU_HOST_SS_WORKPOINT_CONFIG_MIRROR_CONFIG_ID_SHIFT		16u
+#define MTL_VPU_HOST_SS_WORKPOINT_CONFIG_MIRROR_CONFIG_ID_MASK		0xffff0000u
+
+#define MTL_VPU_HOST_MMU_IDR0						0x00200000u
+#define MTL_VPU_HOST_MMU_IDR1						0x00200004u
+#define MTL_VPU_HOST_MMU_IDR3						0x0020000cu
+#define MTL_VPU_HOST_MMU_IDR5						0x00200014u
+#define MTL_VPU_HOST_MMU_CR0						0x00200020u
+#define MTL_VPU_HOST_MMU_CR0ACK						0x00200024u
+#define MTL_VPU_HOST_MMU_CR1						0x00200028u
+#define MTL_VPU_HOST_MMU_CR2						0x0020002cu
+#define MTL_VPU_HOST_MMU_IRQ_CTRL					0x00200050u
+#define MTL_VPU_HOST_MMU_IRQ_CTRLACK					0x00200054u
+
+#define MTL_VPU_HOST_MMU_GERROR						0x00200060u
+#define MTL_VPU_HOST_MMU_GERROR_CMDQ_SHIFT				0u
+#define MTL_VPU_HOST_MMU_GERROR_CMDQ_MASK				0x00000001u
+#define MTL_VPU_HOST_MMU_GERROR_EVTQ_ABT_SHIFT				2u
+#define MTL_VPU_HOST_MMU_GERROR_EVTQ_ABT_MASK				0x00000004u
+#define MTL_VPU_HOST_MMU_GERROR_PRIQ_ABT_SHIFT				3u
+#define MTL_VPU_HOST_MMU_GERROR_PRIQ_ABT_MASK				0x00000008u
+#define MTL_VPU_HOST_MMU_GERROR_MSI_CMDQ_ABT_SHIFT			4u
+#define MTL_VPU_HOST_MMU_GERROR_MSI_CMDQ_ABT_MASK			0x00000010u
+#define MTL_VPU_HOST_MMU_GERROR_MSI_EVTQ_ABT_SHIFT			5u
+#define MTL_VPU_HOST_MMU_GERROR_MSI_EVTQ_ABT_MASK			0x00000020u
+#define MTL_VPU_HOST_MMU_GERROR_MSI_PRIQ_ABT_SHIFT			6u
+#define MTL_VPU_HOST_MMU_GERROR_MSI_PRIQ_ABT_MASK			0x00000040u
+#define MTL_VPU_HOST_MMU_GERROR_MSI_ABT_SHIFT				7u
+#define MTL_VPU_HOST_MMU_GERROR_MSI_ABT_MASK				0x00000080u
+#define MTL_VPU_HOST_MMU_GERROR_SFM_SHIFT				8u
+#define MTL_VPU_HOST_MMU_GERROR_SFM_MASK				0x00000100u
+#define MTL_VPU_HOST_MMU_GERRORN					0x00200064u
+
+#define MTL_VPU_HOST_MMU_STRTAB_BASE					0x00200080u
+#define MTL_VPU_HOST_MMU_STRTAB_BASE_CFG				0x00200088u
+#define MTL_VPU_HOST_MMU_CMDQ_BASE					0x00200090u
+#define MTL_VPU_HOST_MMU_CMDQ_PROD					0x00200098u
+#define MTL_VPU_HOST_MMU_CMDQ_CONS					0x0020009cu
+#define MTL_VPU_HOST_MMU_EVTQ_BASE					0x002000a0u
+#define MTL_VPU_HOST_MMU_EVTQ_PROD					0x002000a8u
+#define MTL_VPU_HOST_MMU_EVTQ_CONS					0x002000acu
+#define MTL_VPU_HOST_MMU_EVTQ_PROD_SEC					(0x002000a8u + SZ_64K)
+#define MTL_VPU_HOST_MMU_EVTQ_CONS_SEC					(0x002000acu + SZ_64K)
+
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES				0x00360000u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_CACHE_OVERRIDE_EN_SHIFT	0u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_CACHE_OVERRIDE_EN_MASK	0x00000001u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_AWCACHE_OVERRIDE_SHIFT	1u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_AWCACHE_OVERRIDE_MASK		0x00000002u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_ARCACHE_OVERRIDE_SHIFT	2u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_ARCACHE_OVERRIDE_MASK		0x00000004u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_NOSNOOP_OVERRIDE_EN_SHIFT	3u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_NOSNOOP_OVERRIDE_EN_MASK	0x00000008u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_AW_NOSNOOP_OVERRIDE_SHIFT	4u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_AW_NOSNOOP_OVERRIDE_MASK	0x00000010u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_AR_NOSNOOP_OVERRIDE_SHIFT	5u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_AR_NOSNOOP_OVERRIDE_MASK	0x00000020u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_PTW_AW_CONTEXT_FLAG_SHIFT	6u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_PTW_AW_CONTEXT_FLAG_MASK	0x000007C0u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_PTW_AR_CONTEXT_FLAG_SHIFT	11u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_PTW_AR_CONTEXT_FLAG_MASK	0x0000f800u
+
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV					0x00360004u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU0_AWMMUSSIDV_SHIFT		0u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU0_AWMMUSSIDV_MASK		0x00000001u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU0_ARMMUSSIDV_SHIFT		1u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU0_ARMMUSSIDV_MASK		0x00000002u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU1_AWMMUSSIDV_SHIFT		2u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU1_AWMMUSSIDV_MASK		0x00000004u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU1_ARMMUSSIDV_SHIFT		3u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU1_ARMMUSSIDV_MASK		0x00000008u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU2_AWMMUSSIDV_SHIFT		4u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU2_AWMMUSSIDV_MASK		0x00000010u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU2_ARMMUSSIDV_SHIFT		5u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU2_ARMMUSSIDV_MASK		0x00000020u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU3_AWMMUSSIDV_SHIFT		6u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU3_AWMMUSSIDV_MASK		0x00000040u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU3_ARMMUSSIDV_SHIFT		7u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU3_ARMMUSSIDV_MASK		0x00000080u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU4_AWMMUSSIDV_SHIFT		8u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU4_AWMMUSSIDV_MASK		0x00000100u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU4_ARMMUSSIDV_SHIFT		9u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU4_ARMMUSSIDV_MASK		0x00000200u
+
+#define MTL_VPU_CPU_SS_DSU_LEON_RT_BASE					0x04000000u
+#define MTL_VPU_CPU_SS_DSU_LEON_RT_DSU_CTRL				0x04000000u
+#define MTL_VPU_CPU_SS_DSU_LEON_RT_PC_REG				0x04400010u
+#define MTL_VPU_CPU_SS_DSU_LEON_RT_NPC_REG				0x04400014u
+#define MTL_VPU_CPU_SS_DSU_LEON_RT_DSU_TRAP_REG				0x04400020u
+
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_CLK_SET				0x06010004u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_CLK_SET_CPU_DSU_SHIFT			1u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_CLK_SET_CPU_DSU_MASK			0x00000002u
+
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_RST_CLR				0x06010018u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_RST_CLR_CPU_DSU_SHIFT			1u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_RST_CLR_CPU_DSU_MASK			0x00000002u
+
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC				0x06010040u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RSTRUN0_SHIFT	0u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RSTRUN0_MASK		0x00000001u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RESUME0_SHIFT	1u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RESUME0_MASK		0x00000002u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RSTRUN1_SHIFT	2u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RSTRUN1_MASK		0x00000004u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RESUME1_SHIFT	3u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RESUME1_MASK		0x00000008u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RSTVEC_SHIFT		4u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RSTVEC_MASK		0xfffffff0u
+
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC				0x06010040u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RSTRUN0_SHIFT	0u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RSTRUN0_MASK		0x00000001u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RESUME0_SHIFT	1u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RESUME0_MASK		0x00000002u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RSTRUN1_SHIFT	2u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RSTRUN1_MASK		0x00000004u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RESUME1_SHIFT	3u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RESUME1_MASK		0x00000008u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RSTVEC_SHIFT		4u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RSTVEC_MASK		0xfffffff0u
+
+#define MTL_VPU_CPU_SS_TIM_WATCHDOG					0x0602009cu
+#define MTL_VPU_CPU_SS_TIM_WDOG_EN					0x060200a4u
+#define MTL_VPU_CPU_SS_TIM_SAFE						0x060200a8u
+
+#define MTL_VPU_CPU_SS_TIM_IPC_FIFO					0x060200f0u
+#define MTL_VPU_CPU_SS_TIM_IPC_FIFO_OF_FLAG0				0x06020100u
+#define MTL_VPU_CPU_SS_TIM_IPC_FIFO_OF_FLAG1				0x06020104u
+
+#define MTL_VPU_CPU_SS_TIM_GEN_CONFIG					0x06021008u
+#define MTL_VPU_CPU_SS_TIM_GEN_CONFIG_WDOG_TO_INT_CLR_OFFSET		9u
+#define MTL_VPU_CPU_SS_TIM_GEN_CONFIG_WDOG_TO_INT_CLR_MASK		0x00000200u
+
+#define MTL_VPU_CPU_SS_DOORBELL_0					0x06300000u
+#define MTL_VPU_CPU_SS_DOORBELL_0_SET_OFFSET				0u
+#define MTL_VPU_CPU_SS_DOORBELL_0_SET_MASK				0x00000001u
+
+#define MTL_VPU_CPU_SS_DOORBELL_1					0x06301000u
+
+#endif /* __IVPU_HW_MTL_REG_H__ */
drivers/gpu/drm/ivpu/ivpu_hw_reg_io.h new file +115 -0
new file mode 100644
index 000000000000..c32d54cd65b6
@@ -0,0 +1,115 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#ifndef __IVPU_HW_REG_IO_H__
+#define __IVPU_HW_REG_IO_H__
+
+#include <linux/bitfield.h>
+#include <linux/io.h>
+#include <linux/iopoll.h>
+
+#include "ivpu_drv.h"
+
+#define REG_POLL_SLEEP_US 50
+#define REG_IO_ERROR      0xffffffff
+
+#define REGB_RD32(reg)          ivpu_hw_reg_rd32(vdev, vdev->regb, (reg), #reg, __func__)
+#define REGB_RD32_SILENT(reg)   readl(vdev->regb + (reg))
+#define REGB_RD64(reg)          ivpu_hw_reg_rd64(vdev, vdev->regb, (reg), #reg, __func__)
+#define REGB_WR32(reg, val)     ivpu_hw_reg_wr32(vdev, vdev->regb, (reg), (val), #reg, __func__)
+#define REGB_WR64(reg, val)     ivpu_hw_reg_wr64(vdev, vdev->regb, (reg), (val), #reg, __func__)
+
+#define REGV_RD32(reg)          ivpu_hw_reg_rd32(vdev, vdev->regv, (reg), #reg, __func__)
+#define REGV_RD32_SILENT(reg)   readl(vdev->regv + (reg))
+#define REGV_RD64(reg)          ivpu_hw_reg_rd64(vdev, vdev->regv, (reg), #reg, __func__)
+#define REGV_WR32(reg, val)     ivpu_hw_reg_wr32(vdev, vdev->regv, (reg), (val), #reg, __func__)
+#define REGV_WR64(reg, val)     ivpu_hw_reg_wr64(vdev, vdev->regv, (reg), (val), #reg, __func__)
+
+#define REGV_WR32I(reg, stride, index, val) \
+	ivpu_hw_reg_wr32_index(vdev, vdev->regv, (reg), (stride), (index), (val), #reg, __func__)
+
+#define REG_FLD(REG, FLD) \
+	(REG##_##FLD##_MASK)
+#define REG_FLD_NUM(REG, FLD, num) \
+	FIELD_PREP(REG##_##FLD##_MASK, num)
+#define REG_GET_FLD(REG, FLD, val) \
+	FIELD_GET(REG##_##FLD##_MASK, val)
+#define REG_CLR_FLD(REG, FLD, val) \
+	((val) & ~(REG##_##FLD##_MASK))
+#define REG_SET_FLD(REG, FLD, val) \
+	((val) | (REG##_##FLD##_MASK))
+#define REG_SET_FLD_NUM(REG, FLD, num, val) \
+	(((val) & ~(REG##_##FLD##_MASK)) | FIELD_PREP(REG##_##FLD##_MASK, num))
+#define REG_TEST_FLD(REG, FLD, val) \
+	((REG##_##FLD##_MASK) == ((val) & (REG##_##FLD##_MASK)))
+#define REG_TEST_FLD_NUM(REG, FLD, num, val) \
+	((num) == FIELD_GET(REG##_##FLD##_MASK, val))
+
+#define REGB_POLL(reg, var, cond, timeout_us) \
+	read_poll_timeout(REGB_RD32_SILENT, var, cond, REG_POLL_SLEEP_US, timeout_us, false, reg)
+
+#define REGV_POLL(reg, var, cond, timeout_us) \
+	read_poll_timeout(REGV_RD32_SILENT, var, cond, REG_POLL_SLEEP_US, timeout_us, false, reg)
+
+#define REGB_POLL_FLD(reg, fld, val, timeout_us) \
+({ \
+	u32 var; \
+	REGB_POLL(reg, var, (FIELD_GET(reg##_##fld##_MASK, var) == (val)), timeout_us); \
+})
+
+#define REGV_POLL_FLD(reg, fld, val, timeout_us) \
+({ \
+	u32 var; \
+	REGV_POLL(reg, var, (FIELD_GET(reg##_##fld##_MASK, var) == (val)), timeout_us); \
+})
+
+static inline u32
+ivpu_hw_reg_rd32(struct ivpu_device *vdev, void __iomem *base, u32 reg,
+		 const char *name, const char *func)
+{
+	u32 val = readl(base + reg);
+
+	ivpu_dbg(REG, "%s RD: %s (0x%08x) => 0x%08x\n", func, name, reg, val);
+	return val;
+}
+
+static inline u64
+ivpu_hw_reg_rd64(struct ivpu_device *vdev, void __iomem *base, u32 reg,
+		 const char *name, const char *func)
+{
+	u64 val = readq(base + reg);
+
+	ivpu_dbg(REG, "%s RD: %s (0x%08x) => 0x%016llx\n", func, name, reg, val);
+	return val;
+}
+
+static inline void
+ivpu_hw_reg_wr32(struct ivpu_device *vdev, void __iomem *base, u32 reg, u32 val,
+		 const char *name, const char *func)
+{
+	ivpu_dbg(REG, "%s WR: %s (0x%08x) <= 0x%08x\n", func, name, reg, val);
+	writel(val, base + reg);
+}
+
+static inline void
+ivpu_hw_reg_wr64(struct ivpu_device *vdev, void __iomem *base, u32 reg, u64 val,
+		 const char *name, const char *func)
+{
+	ivpu_dbg(REG, "%s WR: %s (0x%08x) <= 0x%016llx\n", func, name, reg, val);
+	writeq(val, base + reg);
+}
+
+static inline void
+ivpu_hw_reg_wr32_index(struct ivpu_device *vdev, void __iomem *base, u32 reg,
+		       u32 stride, u32 index, u32 val, const char *name,
+		      const char *func)
+{
+	reg += index * stride;
+
+	ivpu_dbg(REG, "%s WR: %s_%d (0x%08x) <= 0x%08x\n", func, name, index, reg, val);
+	writel(val, base + reg);
+}
+
+#endif /* __IVPU_HW_REG_IO_H__ */
include/uapi/drm/ivpu_drm.h new file +95 -0
new file mode 100644
index 000000000000..7a7dbe2f521b
@@ -0,0 +1,95 @@
+/* SPDX-License-Identifier: GPL-2.0-only WITH Linux-syscall-note */
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#ifndef __UAPI_IVPU_DRM_H__
+#define __UAPI_IVPU_DRM_H__
+
+#include "drm.h"
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+#define DRM_IVPU_DRIVER_MAJOR 1
+#define DRM_IVPU_DRIVER_MINOR 0
+
+#define DRM_IVPU_GET_PARAM		  0x00
+#define DRM_IVPU_SET_PARAM		  0x01
+
+#define DRM_IOCTL_IVPU_GET_PARAM                                               \
+	DRM_IOWR(DRM_COMMAND_BASE + DRM_IVPU_GET_PARAM, struct drm_ivpu_param)
+
+#define DRM_IOCTL_IVPU_SET_PARAM                                               \
+	DRM_IOW(DRM_COMMAND_BASE + DRM_IVPU_SET_PARAM, struct drm_ivpu_param)
+
+/**
+ * DOC: contexts
+ *
+ * VPU contexts have private virtual address space, job queues and priority.
+ * Each context is identified by an unique ID. Context is created on open().
+ */
+
+#define DRM_IVPU_PARAM_DEVICE_ID	    0
+#define DRM_IVPU_PARAM_DEVICE_REVISION	    1
+#define DRM_IVPU_PARAM_PLATFORM_TYPE	    2
+#define DRM_IVPU_PARAM_CORE_CLOCK_RATE	    3
+#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_PLATFORM_TYPE_SILICON	    0
+
+#define DRM_IVPU_CONTEXT_PRIORITY_IDLE	    0
+#define DRM_IVPU_CONTEXT_PRIORITY_NORMAL    1
+#define DRM_IVPU_CONTEXT_PRIORITY_FOCUS	    2
+#define DRM_IVPU_CONTEXT_PRIORITY_REALTIME  3
+
+/**
+ * struct drm_ivpu_param - Get/Set VPU parameters
+ */
+struct drm_ivpu_param {
+	/**
+	 * @param:
+	 *
+	 * Supported params:
+	 *
+	 * %DRM_IVPU_PARAM_DEVICE_ID:
+	 * PCI Device ID of the VPU device (read-only)
+	 *
+	 * %DRM_IVPU_PARAM_DEVICE_REVISION:
+	 * VPU device revision (read-only)
+	 *
+	 * %DRM_IVPU_PARAM_PLATFORM_TYPE:
+	 * Returns %DRM_IVPU_PLATFORM_TYPE_SILICON on real hardware or device specific
+	 * platform type when executing on a simulator or emulator (read-only)
+	 *
+	 * %DRM_IVPU_PARAM_CORE_CLOCK_RATE:
+	 * Current PLL frequency (read-only)
+	 *
+	 * %DRM_IVPU_PARAM_NUM_CONTEXTS:
+	 * Maximum number of simultaneously existing contexts (read-only)
+	 *
+	 * %DRM_IVPU_PARAM_CONTEXT_BASE_ADDRESS:
+	 * Lowest VPU virtual address available in the current context (read-only)
+	 *
+	 * %DRM_IVPU_PARAM_CONTEXT_PRIORITY:
+	 * Value of current context scheduling priority (read-write).
+	 * See DRM_IVPU_CONTEXT_PRIORITY_* for possible values.
+	 *
+	 */
+	__u32 param;
+
+	/** @index: Index for params that have multiple instances */
+	__u32 index;
+
+	/** @value: Param value */
+	__u64 value;
+};
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif /* __UAPI_IVPU_DRM_H__ */
#3 lore raw

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

Hi, thanks for detailed review. My responses inline.

Quoted 56 lines · Jeffrey Hugo
Quoted 55 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 componensts:
- Buttress - provides CPU to VPU integration, interrupt, frequency and power management.
- Memory Management Unit (based on ARM MMU-600) - translates VPU to host DMA addresses, isolates user workloads.
- RISC based microcontroller - executes firmware that provides job execution API for the kernel-mode driver
- Neural Compute Subsystem (NCS) - does the actual work, provides Compute and Copy engines.
- Network on Chip (NoC) - network fabric connecting all the components

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

Module sources are at drivers/gpu/drm/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                            |    8 +
  drivers/gpu/drm/Kconfig                |    2 +
  drivers/gpu/drm/Makefile               |    1 +
  drivers/gpu/drm/ivpu/Kconfig           |   12 +
  drivers/gpu/drm/ivpu/Makefile          |    8 +
  drivers/gpu/drm/ivpu/TODO              |    7 +
  drivers/gpu/drm/ivpu/ivpu_drv.c        |  392 +++++++++
  drivers/gpu/drm/ivpu/ivpu_drv.h        |  153 ++++
  drivers/gpu/drm/ivpu/ivpu_hw.h         |  169 ++++
  drivers/gpu/drm/ivpu/ivpu_hw_mtl.c     | 1021 ++++++++++++++++++++++++
  drivers/gpu/drm/ivpu/ivpu_hw_mtl_reg.h |  468 +++++++++++
  drivers/gpu/drm/ivpu/ivpu_hw_reg_io.h  |  115 +++
  include/uapi/drm/ivpu_drm.h            |   95 +++
  13 files changed, 2451 insertions(+)

create mode 100644 drivers/gpu/drm/ivpu/Kconfig create mode 100644 drivers/gpu/drm/ivpu/Makefile create mode 100644 drivers/gpu/drm/ivpu/TODO create mode 100644 drivers/gpu/drm/ivpu/ivpu_drv.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_drv.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_mtl.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_mtl_reg.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_reg_io.h create mode 100644 include/uapi/drm/ivpu_drm.h

Quoted 16 lines
Quoted 5 lines
MAINTAINERS +0 -0
index 9475aa701a3f..d72ceef107e6 100644

MAINTAINERS F: Documentation/devicetree/bindings/gpu/vivante,gc.yaml · line 7054

Quoted 9 lines
MAINTAINERS +6 -0
@@ -7046,6 +7046,14 @@ F:    Documentation/devicetree/bindings/gpu/vivante,gc.yaml
  F:    drivers/gpu/drm/etnaviv/
  F:    include/uapi/drm/etnaviv_drm.h
  +DRM DRIVERS FOR VPU
+M:    Jacek Lawrynowicz <[email protected]>
+M:    Stanislaw Gruszka <[email protected]>
+S:    Supported
+T:    git git://anongit.freedesktop.org/drm/drm-misc
+F:    drivers/gpu/drm/ivpu/
+F:    include/uapi/drm/ivpu_drm.h

No mail list?

OK, I will add a link to dri-devel.

Quoted 6 lines
Quoted 6 lines
MAINTAINERS +1 -0
+
  DRM DRIVERS FOR XEN
  M:    Oleksandr Andrushchenko <[email protected]>
  L:    [email protected]
drivers/gpu/drm/Kconfig +0 -0
index 198ba846d34b..0aaac0e5366f 100644
Quoted 8 lines

drivers/gpu/drm/Kconfig source "drivers/gpu/drm/v3d/Kconfig" · line 366

Quoted 6 lines
drivers/gpu/drm/Kconfig +1 -0
@@ -364,6 +364,8 @@ source "drivers/gpu/drm/v3d/Kconfig"
    source "drivers/gpu/drm/vc4/Kconfig"
  +source "drivers/gpu/drm/ivpu/Kconfig"
+

Why here of all places? Just randomly in the middle of the list of sourced Kconfigs?

I'll move it to the end.

Quoted 5 lines
Quoted 5 lines
drivers/gpu/drm/Kconfig +0 -0
  source "drivers/gpu/drm/etnaviv/Kconfig"
    source "drivers/gpu/drm/hisilicon/Kconfig"
drivers/gpu/drm/Makefile +0 -1
index 25d0ba310509..1bfd7415c2d0 100644
--- a/drivers/gpu/drm/Makefile
Quoted 8 lines

drivers/gpu/drm/Makefile obj-$(CONFIG_DRM_KMB_DISPLAY) += kmb/ · line 97

Quoted 6 lines

+++ b/drivers/gpu/drm/Makefile

drivers/gpu/drm/Makefile +1 -0
@@ -94,6 +94,7 @@ obj-$(CONFIG_DRM_KMB_DISPLAY)  += kmb/
  obj-$(CONFIG_DRM_MGAG200) += mgag200/
  obj-$(CONFIG_DRM_V3D)  += v3d/
  obj-$(CONFIG_DRM_VC4)  += vc4/
+obj-$(CONFIG_DRM_IVPU)  += ivpu/

Again, why here?

I'll move it to the end.

Quoted 10 lines

drivers/gpu/drm/ivpu/Makefile line 2

Quoted 8 lines
drivers/gpu/drm/ivpu/Makefile new file +2 -0
new file mode 100644
index 000000000000..e59dc65abe6a
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-only
+# Copyright © 2022 Intel Corporation

I'm pretty sure (C) is preferred. Looks like you do this in multiple places. I'm only going to mention it here.

OK, I'll use (C) everywhere.

Quoted 5 lines

drivers/gpu/drm/ivpu/Makefile line 5

Quoted 3 lines
drivers/gpu/drm/ivpu/Makefile new file +3 -0
+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.");

Shouldn't this be unnecessary with the DRM_DEBUG levels, or making the things tracepoints?

drm logging doesn't provide the granuality we need. We plan to add tracepoints in future patches.

Quoted 7 lines
Quoted 7 lines
drivers/gpu/drm/ivpu/Makefile new file +7 -0
+char *ivpu_platform_to_str(u32 platform)
+{
+    switch (platform) {
+    case IVPU_PLATFORM_SILICON:
+        return "IVPU_PLATFORM_SILICON";
+    case IVPU_PLATFORM_SIMICS:
+        return "IVPU_PLATFORM_SIMICS";
Quoted 8 lines

drivers/gpu/drm/ivpu/Makefile

Quoted 6 lines
drivers/gpu/drm/ivpu/Makefile new file +6 -0
+    case IVPU_PLATFORM_FPGA:
+        return "IVPU_PLATFORM_FPGA";
+    default:
+        return "Invalid platform";
+    }
+}

In this entire series, this is only used in this patch, and only in one file. Seems pointless to define it here, and have it in the header. Why shouldn't this be moved to the file it is used in, and made static?

OK, I'll move it.

Quoted 25 lines
Quoted 25 lines
drivers/gpu/drm/ivpu/Makefile new file +25 -0
+static int ivpu_get_param_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 pci_dev *pdev = to_pci_dev(vdev->drm.dev);
+    struct drm_ivpu_param *args = data;
+    int ret = 0;
+    switch (args->param) {
+    case DRM_IVPU_PARAM_DEVICE_ID:
+        args->value = pdev->device;
+        break;
+    case DRM_IVPU_PARAM_DEVICE_REVISION:
+        args->value = pdev->revision;
+        break;
+    case DRM_IVPU_PARAM_PLATFORM_TYPE:
+        args->value = vdev->platform;
+        break;
+    case DRM_IVPU_PARAM_CORE_CLOCK_RATE:
+        args->value = ivpu_hw_reg_pll_freq_get(vdev);
+        break;
+    case DRM_IVPU_PARAM_NUM_CONTEXTS:
+        args->value = ivpu_get_context_count(vdev);
+        break;
+    case DRM_IVPU_PARAM_CONTEXT_BASE_ADDRESS:
+        args->value = vdev->hw->ranges.user_low.start;
Quoted 8 lines

drivers/gpu/drm/ivpu/Makefile

Quoted 6 lines
drivers/gpu/drm/ivpu/Makefile new file +6 -0
+        break;
+    case DRM_IVPU_PARAM_CONTEXT_PRIORITY:
+        args->value = file_priv->priority;
+        break;
+    default:
+        ret = -EINVAL;

This doesn't cause a switch case fallthrough warning?

No, but I will add break for consistency.

Quoted 16 lines

drivers/gpu/drm/ivpu/Makefile

Quoted 13 lines
drivers/gpu/drm/ivpu/Makefile new file +13 -0
+static int ivpu_open(struct drm_device *dev, struct drm_file *file)
+{
+    struct ivpu_device *vdev = to_ivpu_device(dev);
+    struct ivpu_file_priv *file_priv;
+
+    file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
+    if (!file_priv)
+        return -ENOMEM;
+
+    file_priv->vdev = vdev;
+    file_priv->priority = DRM_IVPU_CONTEXT_PRIORITY_NORMAL;
+
+    kref_init(&file_priv->ref);

VFS is going to maintain a refcount on the fd. This looks like you are duplicating that ref count, which seems pointless.

Quoted 1 line

Later on you use this for jobs, as each job takes a ref, but why would it be valid for jobs to hang around after the fd is closed?

This allows user space to close fd immediately without blocking the process in case the job is still being processed by the HW.

Quoted 13 lines
Quoted 13 lines
drivers/gpu/drm/ivpu/Makefile new file +13 -0
+static const struct drm_ioctl_desc ivpu_drm_ioctls[] = {
+    DRM_IOCTL_DEF_DRV(IVPU_GET_PARAM, ivpu_get_param_ioctl, DRM_RENDER_ALLOW),
+    DRM_IOCTL_DEF_DRV(IVPU_SET_PARAM, ivpu_set_param_ioctl, DRM_RENDER_ALLOW),
+};
+
+DEFINE_DRM_GEM_FOPS(ivpu_fops);
+
+int ivpu_shutdown(struct ivpu_device *vdev)
+{
+    int ret;
+
+    ivpu_hw_irq_disable(vdev);
+
Quoted 8 lines

drivers/gpu/drm/ivpu/Makefile

Quoted 6 lines
drivers/gpu/drm/ivpu/Makefile new file +6 -0
+    ret = ivpu_hw_power_down(vdev);
+    if (ret)
+        ivpu_warn(vdev, "Failed to power down HW: %d\n", ret);
+
+    return ret;
+}

Feels odd to have this function definition sit between the DEFINE_DRM_GEM_FOPS and the drm_driver

OK, I will move this function up.

Quoted 10 lines

drivers/gpu/drm/ivpu/Makefile

Quoted 8 lines
drivers/gpu/drm/ivpu/Makefile new file +8 -0
+static int ivpu_irq_init(struct ivpu_device *vdev)
+{
+    struct pci_dev *pdev = to_pci_dev(vdev->drm.dev);
+    int ret;
+
+    ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI | PCI_IRQ_MSIX);
+    if (ret < 0) {
+        ivpu_err(vdev, "Failed to allocate and MSI IRQ: %d\n", ret);

"a MSI"?

Yes, it's a typo.

Quoted 6 lines
Quoted 6 lines
drivers/gpu/drm/ivpu/Makefile new file +6 -0
+static int ivpu_pci_init(struct ivpu_device *vdev)
+{
+    struct pci_dev *pdev = to_pci_dev(vdev->drm.dev);
+    struct resource *bar0 = &pdev->resource[0];
+    struct resource *bar4 = &pdev->resource[4];
+    int ret;
Quoted 8 lines

drivers/gpu/drm/ivpu/Makefile

Quoted 6 lines
drivers/gpu/drm/ivpu/Makefile new file +6 -0
+
+    ivpu_dbg(MISC, "Mapping BAR0 (RegV) %pR\n", bar0);
+    vdev->regv = devm_ioremap_resource(vdev->drm.dev, bar0);
+    if (IS_ERR(vdev->regv)) {
+        ivpu_err(vdev, "Failed to map bar 0\n");
+        return -ENOMEM;

You have a particular reason for recasting the error in vdev->regv to ENOMEM?

No, I'll use PTR_ERR.

Quoted 4 lines

drivers/gpu/drm/ivpu/Makefile

Quoted 2 lines
drivers/gpu/drm/ivpu/Makefile new file +2 -0
+static struct pci_device_id ivpu_pci_ids[] = {
+    { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_MTL) },

Why not use include/linux/pci_ids.h for the VID?

Sure, I will use it.

Quoted 5 lines

drivers/gpu/drm/ivpu/Makefile

Quoted 3 lines
drivers/gpu/drm/ivpu/Makefile new file +3 -0
+MODULE_AUTHOR("Intel Corporation");
+MODULE_DESCRIPTION(DRIVER_DESC);
+MODULE_LICENSE("GPL and additional rights");

I don't think this is valid. Pretty sure should just be "GPL".

We have two firmware API headers under MIT. Can we still say "GPL"? Some drivers use "GPL v2", wouldn't this be better as it's more specific?

Regards,
Jacek

#4 lore raw

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

Hi

Am 25.10.22 um 13:42 schrieb Jacek Lawrynowicz:

Quoted 93 lines

Hi, thanks for detailed review. My responses inline.

Quoted 56 lines · Jeffrey Hugo
Quoted 55 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 componensts:
- Buttress - provides CPU to VPU integration, interrupt, frequency and power management.
- Memory Management Unit (based on ARM MMU-600) - translates VPU to host DMA addresses, isolates user workloads.
- RISC based microcontroller - executes firmware that provides job execution API for the kernel-mode driver
- Neural Compute Subsystem (NCS) - does the actual work, provides Compute and Copy engines.
- Network on Chip (NoC) - network fabric connecting all the components

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

Module sources are at drivers/gpu/drm/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                            |    8 +
   drivers/gpu/drm/Kconfig                |    2 +
   drivers/gpu/drm/Makefile               |    1 +
   drivers/gpu/drm/ivpu/Kconfig           |   12 +
   drivers/gpu/drm/ivpu/Makefile          |    8 +
   drivers/gpu/drm/ivpu/TODO              |    7 +
   drivers/gpu/drm/ivpu/ivpu_drv.c        |  392 +++++++++
   drivers/gpu/drm/ivpu/ivpu_drv.h        |  153 ++++
   drivers/gpu/drm/ivpu/ivpu_hw.h         |  169 ++++
   drivers/gpu/drm/ivpu/ivpu_hw_mtl.c     | 1021 ++++++++++++++++++++++++
   drivers/gpu/drm/ivpu/ivpu_hw_mtl_reg.h |  468 +++++++++++
   drivers/gpu/drm/ivpu/ivpu_hw_reg_io.h  |  115 +++
   include/uapi/drm/ivpu_drm.h            |   95 +++
   13 files changed, 2451 insertions(+)

create mode 100644 drivers/gpu/drm/ivpu/Kconfig create mode 100644 drivers/gpu/drm/ivpu/Makefile create mode 100644 drivers/gpu/drm/ivpu/TODO create mode 100644 drivers/gpu/drm/ivpu/ivpu_drv.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_drv.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_mtl.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_mtl_reg.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_reg_io.h create mode 100644 include/uapi/drm/ivpu_drm.h

Quoted 16 lines
Quoted 5 lines
MAINTAINERS +0 -0
index 9475aa701a3f..d72ceef107e6 100644

MAINTAINERS F: Documentation/devicetree/bindings/gpu/vivante,gc.yaml · line 7054

Quoted 9 lines
MAINTAINERS +6 -0
@@ -7046,6 +7046,14 @@ F:    Documentation/devicetree/bindings/gpu/vivante,gc.yaml
   F:    drivers/gpu/drm/etnaviv/
   F:    include/uapi/drm/etnaviv_drm.h
   +DRM DRIVERS FOR VPU
+M:    Jacek Lawrynowicz <[email protected]>
+M:    Stanislaw Gruszka <[email protected]>
+S:    Supported
+T:    git git://anongit.freedesktop.org/drm/drm-misc
+F:    drivers/gpu/drm/ivpu/
+F:    include/uapi/drm/ivpu_drm.h

No mail list?

OK, I will add a link to dri-devel.

Quoted 6 lines
Quoted 6 lines
MAINTAINERS +1 -0
+
   DRM DRIVERS FOR XEN
   M:    Oleksandr Andrushchenko <[email protected]>
   L:    [email protected]
drivers/gpu/drm/Kconfig +0 -0
index 198ba846d34b..0aaac0e5366f 100644
Quoted 8 lines

drivers/gpu/drm/Kconfig source "drivers/gpu/drm/v3d/Kconfig" · line 366

Quoted 6 lines
drivers/gpu/drm/Kconfig +1 -0
@@ -364,6 +364,8 @@ source "drivers/gpu/drm/v3d/Kconfig"
     source "drivers/gpu/drm/vc4/Kconfig"
   +source "drivers/gpu/drm/ivpu/Kconfig"
+

Why here of all places? Just randomly in the middle of the list of sourced Kconfigs?

Quoted 1 line

I'll move it to the end.

I known that the Kconfigs and Makefiles are chaotic. But if you can, try to sort it alphabetically.

Quoted 15 lines
Quoted 14 lines

drivers/gpu/drm/Makefile obj-$(CONFIG_DRM_KMB_DISPLAY) += kmb/ · line 97

Quoted 12 lines
drivers/gpu/drm/Kconfig +0 -0
   source "drivers/gpu/drm/etnaviv/Kconfig"
     source "drivers/gpu/drm/hisilicon/Kconfig"
drivers/gpu/drm/Makefile +1 -0
index 25d0ba310509..1bfd7415c2d0 100644
@@ -94,6 +94,7 @@ obj-$(CONFIG_DRM_KMB_DISPLAY)  += kmb/
   obj-$(CONFIG_DRM_MGAG200) += mgag200/
   obj-$(CONFIG_DRM_V3D)  += v3d/
   obj-$(CONFIG_DRM_VC4)  += vc4/
+obj-$(CONFIG_DRM_IVPU)  += ivpu/

Again, why here?

Quoted 1 line

I'll move it to the end.

Same.

Best regards
Thomas

Signature · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Maxfeldstr. 5, 90409 Nürnberg, Germany
(HRB 36809, AG Nürnberg)
Geschäftsführer: Ivo Totev
#5 lore raw

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

Hi,

Quoted 96 lines · Thomas Zimmermann

Hi

Am 25.10.22 um 13:42 schrieb Jacek Lawrynowicz:

Quoted 93 lines

Hi, thanks for detailed review. My responses inline.

Quoted 56 lines · Jeffrey Hugo
Quoted 55 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 componensts:
    - Buttress - provides CPU to VPU integration, interrupt, frequency and power management.
    - Memory Management Unit (based on ARM MMU-600) - translates VPU to host DMA addresses, isolates user workloads.
    - RISC based microcontroller - executes firmware that provides job execution API for the kernel-mode driver
    - Neural Compute Subsystem (NCS) - does the actual work, provides Compute and Copy engines.
    - Network on Chip (NoC) - network fabric connecting all the components

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

Module sources are at drivers/gpu/drm/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                            |    8 + drivers/gpu/drm/Kconfig                |    2 + drivers/gpu/drm/Makefile               |    1 + drivers/gpu/drm/ivpu/Kconfig           |   12 + drivers/gpu/drm/ivpu/Makefile          |    8 + drivers/gpu/drm/ivpu/TODO              |    7 + drivers/gpu/drm/ivpu/ivpu_drv.c        |  392 +++++++++ drivers/gpu/drm/ivpu/ivpu_drv.h        |  153 ++++ drivers/gpu/drm/ivpu/ivpu_hw.h         |  169 ++++ drivers/gpu/drm/ivpu/ivpu_hw_mtl.c     | 1021 ++++++++++++++++++++++++ drivers/gpu/drm/ivpu/ivpu_hw_mtl_reg.h |  468 +++++++++++ drivers/gpu/drm/ivpu/ivpu_hw_reg_io.h  |  115 +++ include/uapi/drm/ivpu_drm.h            |   95 +++

   13 files changed, 2451 insertions(+)

   create mode 100644 drivers/gpu/drm/ivpu/Kconfig create mode 100644 drivers/gpu/drm/ivpu/Makefile create mode 100644 drivers/gpu/drm/ivpu/TODO create mode 100644 drivers/gpu/drm/ivpu/ivpu_drv.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_drv.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_mtl.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_mtl_reg.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_reg_io.h create mode 100644 include/uapi/drm/ivpu_drm.h

Quoted 16 lines
Quoted 8 lines
MAINTAINERS +0 -0
index 9475aa701a3f..d72ceef107e6 100644
@@ -7046,6 +7046,14 @@ F:    Documentation/devicetree/bindings/gpu/vivante,gc.yaml

   F:    drivers/gpu/drm/etnaviv/
   F:    include/uapi/drm/etnaviv_drm.h
   +DRM DRIVERS FOR VPU

MAINTAINERS F: Documentation/devicetree/bindings/gpu/vivante,gc.yaml · line 7055

Quoted 6 lines
MAINTAINERS +6 -0
+M:    Jacek Lawrynowicz <[email protected]>
+M:    Stanislaw Gruszka <[email protected]>
+S:    Supported
+T:    git git://anongit.freedesktop.org/drm/drm-misc
+F:    drivers/gpu/drm/ivpu/
+F:    include/uapi/drm/ivpu_drm.h

No mail list?

OK, I will add a link to dri-devel.

Quoted 14 lines
Quoted 9 lines

+
   DRM DRIVERS FOR XEN
   M:    Oleksandr Andrushchenko <[email protected]> L:    [email protected]

drivers/gpu/drm/Kconfig +0 -0
index 198ba846d34b..0aaac0e5366f 100644
@@ -364,6 +364,8 @@ source "drivers/gpu/drm/v3d/Kconfig"
Quoted 3 lines

     source "drivers/gpu/drm/vc4/Kconfig"
   +source "drivers/gpu/drm/ivpu/Kconfig"
+

Why here of all places?  Just randomly in the middle of the list of sourced Kconfigs?

Quoted 3 lines
Quoted 1 line

I'll move it to the end.

I known that the Kconfigs and Makefiles are chaotic. But if you can, try to sort it alphabetically.

Man, this is an impossible task :)
Would the line under i915 be OK?

Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
Quoted 8 lines

   source "drivers/gpu/drm/etnaviv/Kconfig"
     source "drivers/gpu/drm/hisilicon/Kconfig"

drivers/gpu/drm/Makefile +0 -0
index 25d0ba310509..1bfd7415c2d0 100644
@@ -94,6 +94,7 @@ obj-$(CONFIG_DRM_KMB_DISPLAY)  += kmb/
Quoted 4 lines

   obj-$(CONFIG_DRM_MGAG200) += mgag200/
   obj-$(CONFIG_DRM_V3D)  += v3d/
   obj-$(CONFIG_DRM_VC4)  += vc4/
+obj-$(CONFIG_DRM_IVPU)  += ivpu/

Again, why here?

Quoted 3 lines
Quoted 1 line

I'll move it to the end.

Same.

Under i915?

Regards,
Jacek

#6 lore raw

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

Hi

Am 26.10.22 um 14:07 schrieb Jacek Lawrynowicz:

Quoted 99 lines

Hi,

Quoted 96 lines · Thomas Zimmermann

Hi

Am 25.10.22 um 13:42 schrieb Jacek Lawrynowicz:

Quoted 93 lines

Hi, thanks for detailed review. My responses inline.

Quoted 56 lines · Jeffrey Hugo
Quoted 55 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 componensts:
    - Buttress - provides CPU to VPU integration, interrupt, frequency and power management.
    - Memory Management Unit (based on ARM MMU-600) - translates VPU to host DMA addresses, isolates user workloads.
    - RISC based microcontroller - executes firmware that provides job execution API for the kernel-mode driver
    - Neural Compute Subsystem (NCS) - does the actual work, provides Compute and Copy engines.
    - Network on Chip (NoC) - network fabric connecting all the components

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

Module sources are at drivers/gpu/drm/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                            |    8 +
    drivers/gpu/drm/Kconfig                |    2 +
    drivers/gpu/drm/Makefile               |    1 +
    drivers/gpu/drm/ivpu/Kconfig           |   12 +
    drivers/gpu/drm/ivpu/Makefile          |    8 +
    drivers/gpu/drm/ivpu/TODO              |    7 +
    drivers/gpu/drm/ivpu/ivpu_drv.c        |  392 +++++++++
    drivers/gpu/drm/ivpu/ivpu_drv.h        |  153 ++++
    drivers/gpu/drm/ivpu/ivpu_hw.h         |  169 ++++
    drivers/gpu/drm/ivpu/ivpu_hw_mtl.c     | 1021 ++++++++++++++++++++++++
    drivers/gpu/drm/ivpu/ivpu_hw_mtl_reg.h |  468 +++++++++++
    drivers/gpu/drm/ivpu/ivpu_hw_reg_io.h  |  115 +++
    include/uapi/drm/ivpu_drm.h            |   95 +++
    13 files changed, 2451 insertions(+)

   create mode 100644 drivers/gpu/drm/ivpu/Kconfig create mode 100644 drivers/gpu/drm/ivpu/Makefile create mode 100644 drivers/gpu/drm/ivpu/TODO create mode 100644 drivers/gpu/drm/ivpu/ivpu_drv.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_drv.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_mtl.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_mtl_reg.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_reg_io.h create mode 100644 include/uapi/drm/ivpu_drm.h

Quoted 16 lines
Quoted 5 lines
MAINTAINERS +0 -0
index 9475aa701a3f..d72ceef107e6 100644

MAINTAINERS F:    Documentation/devicetree/bindings/gpu/vivante,gc.yaml · line 7054

Quoted 9 lines
MAINTAINERS +6 -0
@@ -7046,6 +7046,14 @@ F:    Documentation/devicetree/bindings/gpu/vivante,gc.yaml
    F:    drivers/gpu/drm/etnaviv/
    F:    include/uapi/drm/etnaviv_drm.h
    +DRM DRIVERS FOR VPU
+M:    Jacek Lawrynowicz <[email protected]>
+M:    Stanislaw Gruszka <[email protected]>
+S:    Supported
+T:    git git://anongit.freedesktop.org/drm/drm-misc
+F:    drivers/gpu/drm/ivpu/
+F:    include/uapi/drm/ivpu_drm.h

No mail list?

OK, I will add a link to dri-devel.

Quoted 14 lines

drivers/gpu/drm/Kconfig source "drivers/gpu/drm/v3d/Kconfig" · line 366

Quoted 12 lines
MAINTAINERS +1 -0
+
    DRM DRIVERS FOR XEN
    M:    Oleksandr Andrushchenko <[email protected]>
    L:    [email protected]
drivers/gpu/drm/Kconfig +1 -0
index 198ba846d34b..0aaac0e5366f 100644
@@ -364,6 +364,8 @@ source "drivers/gpu/drm/v3d/Kconfig"
      source "drivers/gpu/drm/vc4/Kconfig"
    +source "drivers/gpu/drm/ivpu/Kconfig"
+

Why here of all places?  Just randomly in the middle of the list of sourced Kconfigs?

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

I'll move it to the end.

I known that the Kconfigs and Makefiles are chaotic. But if you can, try to sort it alphabetically.

Man, this is an impossible task :)
Would the line under i915 be OK?

:D

Sounds good.

Best regards
Thomas

Quoted 25 lines
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines

drivers/gpu/drm/Makefile obj-$(CONFIG_DRM_KMB_DISPLAY)  += kmb/ · line 97

Quoted 13 lines
drivers/gpu/drm/Kconfig +0 -0
    source "drivers/gpu/drm/etnaviv/Kconfig"
      source "drivers/gpu/drm/hisilicon/Kconfig"
drivers/gpu/drm/Makefile +1 -0
index 25d0ba310509..1bfd7415c2d0 100644
@@ -94,6 +94,7 @@ obj-$(CONFIG_DRM_KMB_DISPLAY)  += kmb/
    obj-$(CONFIG_DRM_MGAG200) += mgag200/
    obj-$(CONFIG_DRM_V3D)  += v3d/
    obj-$(CONFIG_DRM_VC4)  += vc4/
+obj-$(CONFIG_DRM_IVPU)  += ivpu/

Again, why here?

Quoted 3 lines
Quoted 1 line

I'll move it to the end.

Same.

Under i915?

Regards,
Jacek

Signature · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Maxfeldstr. 5, 90409 Nürnberg, Germany
(HRB 36809, AG Nürnberg)
Geschäftsführer: Ivo Totev
#7 lore raw

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

In reply to Jacek Lawrynowicz
Quoted 9 lines · Jacek Lawrynowicz

Hi, thanks for detailed review. My responses inline.

Quoted 6 lines · Jeffrey Hugo
Quoted 5 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 componensts:

Just noticed this. "components"

Quoted 194 lines
Quoted 50 lines
Quoted 50 lines

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

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

Module sources are at drivers/gpu/drm/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                            |    8 +
   drivers/gpu/drm/Kconfig                |    2 +
   drivers/gpu/drm/Makefile               |    1 +
   drivers/gpu/drm/ivpu/Kconfig           |   12 +
   drivers/gpu/drm/ivpu/Makefile          |    8 +
   drivers/gpu/drm/ivpu/TODO              |    7 +
   drivers/gpu/drm/ivpu/ivpu_drv.c        |  392 +++++++++
   drivers/gpu/drm/ivpu/ivpu_drv.h        |  153 ++++
   drivers/gpu/drm/ivpu/ivpu_hw.h         |  169 ++++
   drivers/gpu/drm/ivpu/ivpu_hw_mtl.c     | 1021 ++++++++++++++++++++++++
   drivers/gpu/drm/ivpu/ivpu_hw_mtl_reg.h |  468 +++++++++++
   drivers/gpu/drm/ivpu/ivpu_hw_reg_io.h  |  115 +++
   include/uapi/drm/ivpu_drm.h            |   95 +++
   13 files changed, 2451 insertions(+)

create mode 100644 drivers/gpu/drm/ivpu/Kconfig create mode 100644 drivers/gpu/drm/ivpu/Makefile create mode 100644 drivers/gpu/drm/ivpu/TODO create mode 100644 drivers/gpu/drm/ivpu/ivpu_drv.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_drv.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_mtl.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_mtl_reg.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_reg_io.h create mode 100644 include/uapi/drm/ivpu_drm.h

Quoted 16 lines
Quoted 5 lines
MAINTAINERS +0 -0
index 9475aa701a3f..d72ceef107e6 100644

MAINTAINERS F: Documentation/devicetree/bindings/gpu/vivante,gc.yaml · line 7054

Quoted 9 lines
MAINTAINERS +6 -0
@@ -7046,6 +7046,14 @@ F:    Documentation/devicetree/bindings/gpu/vivante,gc.yaml
   F:    drivers/gpu/drm/etnaviv/
   F:    include/uapi/drm/etnaviv_drm.h
   +DRM DRIVERS FOR VPU
+M:    Jacek Lawrynowicz <[email protected]>
+M:    Stanislaw Gruszka <[email protected]>
+S:    Supported
+T:    git git://anongit.freedesktop.org/drm/drm-misc
+F:    drivers/gpu/drm/ivpu/
+F:    include/uapi/drm/ivpu_drm.h

No mail list?

OK, I will add a link to dri-devel.

Quoted 6 lines
Quoted 6 lines
MAINTAINERS +1 -0
+
   DRM DRIVERS FOR XEN
   M:    Oleksandr Andrushchenko <[email protected]>
   L:    [email protected]
drivers/gpu/drm/Kconfig +0 -0
index 198ba846d34b..0aaac0e5366f 100644
Quoted 8 lines

drivers/gpu/drm/Kconfig source "drivers/gpu/drm/v3d/Kconfig" · line 366

Quoted 6 lines
drivers/gpu/drm/Kconfig +1 -0
@@ -364,6 +364,8 @@ source "drivers/gpu/drm/v3d/Kconfig"
     source "drivers/gpu/drm/vc4/Kconfig"
   +source "drivers/gpu/drm/ivpu/Kconfig"
+

Why here of all places? Just randomly in the middle of the list of sourced Kconfigs?

I'll move it to the end.

Quoted 5 lines
Quoted 5 lines
drivers/gpu/drm/Kconfig +0 -0
   source "drivers/gpu/drm/etnaviv/Kconfig"
     source "drivers/gpu/drm/hisilicon/Kconfig"
drivers/gpu/drm/Makefile +0 -1
index 25d0ba310509..1bfd7415c2d0 100644
--- a/drivers/gpu/drm/Makefile
Quoted 8 lines

drivers/gpu/drm/Makefile obj-$(CONFIG_DRM_KMB_DISPLAY) += kmb/ · line 97

Quoted 6 lines

+++ b/drivers/gpu/drm/Makefile

drivers/gpu/drm/Makefile +1 -0
@@ -94,6 +94,7 @@ obj-$(CONFIG_DRM_KMB_DISPLAY)  += kmb/
   obj-$(CONFIG_DRM_MGAG200) += mgag200/
   obj-$(CONFIG_DRM_V3D)  += v3d/
   obj-$(CONFIG_DRM_VC4)  += vc4/
+obj-$(CONFIG_DRM_IVPU)  += ivpu/

Again, why here?

I'll move it to the end.

Quoted 10 lines

drivers/gpu/drm/ivpu/Makefile line 2

Quoted 8 lines
drivers/gpu/drm/ivpu/Makefile new file +2 -0
new file mode 100644
index 000000000000..e59dc65abe6a
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-only
+# Copyright © 2022 Intel Corporation

I'm pretty sure (C) is preferred. Looks like you do this in multiple places. I'm only going to mention it here.

OK, I'll use (C) everywhere.

Quoted 5 lines

drivers/gpu/drm/ivpu/Makefile line 5

Quoted 3 lines
drivers/gpu/drm/ivpu/Makefile new file +3 -0
+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.");

Shouldn't this be unnecessary with the DRM_DEBUG levels, or making the things tracepoints?

drm logging doesn't provide the granuality we need. We plan to add tracepoints in future patches.

Quoted 7 lines
Quoted 7 lines
drivers/gpu/drm/ivpu/Makefile new file +7 -0
+char *ivpu_platform_to_str(u32 platform)
+{
+    switch (platform) {
+    case IVPU_PLATFORM_SILICON:
+        return "IVPU_PLATFORM_SILICON";
+    case IVPU_PLATFORM_SIMICS:
+        return "IVPU_PLATFORM_SIMICS";
Quoted 8 lines

drivers/gpu/drm/ivpu/Makefile

Quoted 6 lines
drivers/gpu/drm/ivpu/Makefile new file +6 -0
+    case IVPU_PLATFORM_FPGA:
+        return "IVPU_PLATFORM_FPGA";
+    default:
+        return "Invalid platform";
+    }
+}

In this entire series, this is only used in this patch, and only in one file. Seems pointless to define it here, and have it in the header. Why shouldn't this be moved to the file it is used in, and made static?

OK, I'll move it.

Quoted 25 lines
Quoted 25 lines
drivers/gpu/drm/ivpu/Makefile new file +25 -0
+static int ivpu_get_param_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 pci_dev *pdev = to_pci_dev(vdev->drm.dev);
+    struct drm_ivpu_param *args = data;
+    int ret = 0;
+    switch (args->param) {
+    case DRM_IVPU_PARAM_DEVICE_ID:
+        args->value = pdev->device;
+        break;
+    case DRM_IVPU_PARAM_DEVICE_REVISION:
+        args->value = pdev->revision;
+        break;
+    case DRM_IVPU_PARAM_PLATFORM_TYPE:
+        args->value = vdev->platform;
+        break;
+    case DRM_IVPU_PARAM_CORE_CLOCK_RATE:
+        args->value = ivpu_hw_reg_pll_freq_get(vdev);
+        break;
+    case DRM_IVPU_PARAM_NUM_CONTEXTS:
+        args->value = ivpu_get_context_count(vdev);
+        break;
+    case DRM_IVPU_PARAM_CONTEXT_BASE_ADDRESS:
+        args->value = vdev->hw->ranges.user_low.start;
Quoted 8 lines

drivers/gpu/drm/ivpu/Makefile

Quoted 6 lines
drivers/gpu/drm/ivpu/Makefile new file +6 -0
+        break;
+    case DRM_IVPU_PARAM_CONTEXT_PRIORITY:
+        args->value = file_priv->priority;
+        break;
+    default:
+        ret = -EINVAL;

This doesn't cause a switch case fallthrough warning?

No, but I will add break for consistency.

Quoted 16 lines

drivers/gpu/drm/ivpu/Makefile

Quoted 13 lines
drivers/gpu/drm/ivpu/Makefile new file +13 -0
+static int ivpu_open(struct drm_device *dev, struct drm_file *file)
+{
+    struct ivpu_device *vdev = to_ivpu_device(dev);
+    struct ivpu_file_priv *file_priv;
+
+    file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
+    if (!file_priv)
+        return -ENOMEM;
+
+    file_priv->vdev = vdev;
+    file_priv->priority = DRM_IVPU_CONTEXT_PRIORITY_NORMAL;
+
+    kref_init(&file_priv->ref);

VFS is going to maintain a refcount on the fd. This looks like you are duplicating that ref count, which seems pointless.

Quoted 3 lines
Quoted 1 line

Later on you use this for jobs, as each job takes a ref, but why would it be valid for jobs to hang around after the fd is closed?

This allows user space to close fd immediately without blocking the process in case the job is still being processed by the HW.

Eh. Ok. Maybe add a comment to that effect?

Quoted 62 lines
Quoted 14 lines
Quoted 14 lines
drivers/gpu/drm/ivpu/Makefile new file +13 -0
+static const struct drm_ioctl_desc ivpu_drm_ioctls[] = {
+    DRM_IOCTL_DEF_DRV(IVPU_GET_PARAM, ivpu_get_param_ioctl, DRM_RENDER_ALLOW),
+    DRM_IOCTL_DEF_DRV(IVPU_SET_PARAM, ivpu_set_param_ioctl, DRM_RENDER_ALLOW),
+};
+
+DEFINE_DRM_GEM_FOPS(ivpu_fops);
+
+int ivpu_shutdown(struct ivpu_device *vdev)
+{
+    int ret;
+
+    ivpu_hw_irq_disable(vdev);
+
Quoted 8 lines

drivers/gpu/drm/ivpu/Makefile

Quoted 6 lines
drivers/gpu/drm/ivpu/Makefile new file +6 -0
+    ret = ivpu_hw_power_down(vdev);
+    if (ret)
+        ivpu_warn(vdev, "Failed to power down HW: %d\n", ret);
+
+    return ret;
+}

Feels odd to have this function definition sit between the DEFINE_DRM_GEM_FOPS and the drm_driver

OK, I will move this function up.

Quoted 10 lines

drivers/gpu/drm/ivpu/Makefile

Quoted 8 lines
drivers/gpu/drm/ivpu/Makefile new file +8 -0
+static int ivpu_irq_init(struct ivpu_device *vdev)
+{
+    struct pci_dev *pdev = to_pci_dev(vdev->drm.dev);
+    int ret;
+
+    ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI | PCI_IRQ_MSIX);
+    if (ret < 0) {
+        ivpu_err(vdev, "Failed to allocate and MSI IRQ: %d\n", ret);

"a MSI"?

Yes, it's a typo.

Quoted 6 lines
Quoted 6 lines
drivers/gpu/drm/ivpu/Makefile new file +6 -0
+static int ivpu_pci_init(struct ivpu_device *vdev)
+{
+    struct pci_dev *pdev = to_pci_dev(vdev->drm.dev);
+    struct resource *bar0 = &pdev->resource[0];
+    struct resource *bar4 = &pdev->resource[4];
+    int ret;
Quoted 8 lines

drivers/gpu/drm/ivpu/Makefile

Quoted 6 lines
drivers/gpu/drm/ivpu/Makefile new file +6 -0
+
+    ivpu_dbg(MISC, "Mapping BAR0 (RegV) %pR\n", bar0);
+    vdev->regv = devm_ioremap_resource(vdev->drm.dev, bar0);
+    if (IS_ERR(vdev->regv)) {
+        ivpu_err(vdev, "Failed to map bar 0\n");
+        return -ENOMEM;

You have a particular reason for recasting the error in vdev->regv to ENOMEM?

No, I'll use PTR_ERR.

Quoted 4 lines

drivers/gpu/drm/ivpu/Makefile

Quoted 2 lines
drivers/gpu/drm/ivpu/Makefile new file +2 -0
+static struct pci_device_id ivpu_pci_ids[] = {
+    { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_MTL) },

Why not use include/linux/pci_ids.h for the VID?

Sure, I will use it.

Quoted 7 lines
Quoted 5 lines

drivers/gpu/drm/ivpu/Makefile

Quoted 3 lines
drivers/gpu/drm/ivpu/Makefile new file +3 -0
+MODULE_AUTHOR("Intel Corporation");
+MODULE_DESCRIPTION(DRIVER_DESC);
+MODULE_LICENSE("GPL and additional rights");

I don't think this is valid. Pretty sure should just be "GPL".

We have two firmware API headers under MIT. Can we still say "GPL"? Some drivers use "GPL v2", wouldn't this be better as it's more specific?

Nah, I'm wrong. I thought this string was one of the ones that was deprecated, but looking at checkpatch, it is not. Sorry about the noise.

#8 lore raw

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

Quoted 4 lines · Jeffrey Hugo
Quoted 3 lines · Jacek Lawrynowicz

Hi, thanks for detailed review. My responses inline.

On 10/25/2022 1:00 AM, Jeffrey Hugo wrote:

Quoted 8 lines
Quoted 6 lines
Quoted 6 lines · Jacek Lawrynowicz
Quoted 5 lines

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

The VPU device consist of following componensts:

Just noticed this.  "components"

Fixed.

Quoted 194 lines
Quoted 194 lines
Quoted 50 lines
Quoted 50 lines

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

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

Module sources are at drivers/gpu/drm/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                            |    8 + drivers/gpu/drm/Kconfig                |    2 + drivers/gpu/drm/Makefile               |    1 + drivers/gpu/drm/ivpu/Kconfig           |   12 + drivers/gpu/drm/ivpu/Makefile          |    8 + drivers/gpu/drm/ivpu/TODO              |    7 + drivers/gpu/drm/ivpu/ivpu_drv.c        |  392 +++++++++ drivers/gpu/drm/ivpu/ivpu_drv.h        |  153 ++++ drivers/gpu/drm/ivpu/ivpu_hw.h         |  169 ++++ drivers/gpu/drm/ivpu/ivpu_hw_mtl.c     | 1021 ++++++++++++++++++++++++ drivers/gpu/drm/ivpu/ivpu_hw_mtl_reg.h |  468 +++++++++++ drivers/gpu/drm/ivpu/ivpu_hw_reg_io.h  |  115 +++ include/uapi/drm/ivpu_drm.h            |   95 +++

   13 files changed, 2451 insertions(+)

   create mode 100644 drivers/gpu/drm/ivpu/Kconfig create mode 100644 drivers/gpu/drm/ivpu/Makefile create mode 100644 drivers/gpu/drm/ivpu/TODO create mode 100644 drivers/gpu/drm/ivpu/ivpu_drv.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_drv.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_mtl.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_mtl_reg.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_reg_io.h create mode 100644 include/uapi/drm/ivpu_drm.h

Quoted 16 lines
Quoted 8 lines
MAINTAINERS +0 -0
index 9475aa701a3f..d72ceef107e6 100644
@@ -7046,6 +7046,14 @@ F:    Documentation/devicetree/bindings/gpu/vivante,gc.yaml

   F:    drivers/gpu/drm/etnaviv/
   F:    include/uapi/drm/etnaviv_drm.h
   +DRM DRIVERS FOR VPU

MAINTAINERS F: Documentation/devicetree/bindings/gpu/vivante,gc.yaml · line 7055

Quoted 6 lines
MAINTAINERS +6 -0
+M:    Jacek Lawrynowicz <[email protected]>
+M:    Stanislaw Gruszka <[email protected]>
+S:    Supported
+T:    git git://anongit.freedesktop.org/drm/drm-misc
+F:    drivers/gpu/drm/ivpu/
+F:    include/uapi/drm/ivpu_drm.h

No mail list?

OK, I will add a link to dri-devel.

Quoted 6 lines
Quoted 6 lines

+
   DRM DRIVERS FOR XEN
   M:    Oleksandr Andrushchenko <[email protected]> L:    [email protected]

drivers/gpu/drm/Kconfig +0 -0
index 198ba846d34b..0aaac0e5366f 100644
Quoted 8 lines
Quoted 6 lines
drivers/gpu/drm/Kconfig +0 -0
@@ -364,6 +364,8 @@ source "drivers/gpu/drm/v3d/Kconfig"

     source "drivers/gpu/drm/vc4/Kconfig"
   +source "drivers/gpu/drm/ivpu/Kconfig"
+

Why here of all places?  Just randomly in the middle of the list of sourced Kconfigs?

I'll move it to the end.

Quoted 5 lines
Quoted 5 lines

   source "drivers/gpu/drm/etnaviv/Kconfig"
     source "drivers/gpu/drm/hisilicon/Kconfig"

drivers/gpu/drm/Makefile +0 -1
index 25d0ba310509..1bfd7415c2d0 100644
--- a/drivers/gpu/drm/Makefile
Quoted 8 lines
Quoted 6 lines

+++ b/drivers/gpu/drm/Makefile

drivers/gpu/drm/Makefile +0 -0
@@ -94,6 +94,7 @@ obj-$(CONFIG_DRM_KMB_DISPLAY)  += kmb/

   obj-$(CONFIG_DRM_MGAG200) += mgag200/
   obj-$(CONFIG_DRM_V3D)  += v3d/
   obj-$(CONFIG_DRM_VC4)  += vc4/
+obj-$(CONFIG_DRM_IVPU)  += ivpu/

Again, why here?

I'll move it to the end.

Quoted 10 lines

drivers/gpu/drm/ivpu/Makefile line 2

Quoted 8 lines
drivers/gpu/drm/ivpu/Makefile new file +2 -0
new file mode 100644
index 000000000000..e59dc65abe6a
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-only
+# Copyright © 2022 Intel Corporation

I'm pretty sure (C) is preferred.  Looks like you do this in multiple places.  I'm only going to mention it here.

OK, I'll use (C) everywhere.

Quoted 5 lines

drivers/gpu/drm/ivpu/Makefile line 5

Quoted 3 lines
drivers/gpu/drm/ivpu/Makefile new file +3 -0
+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.");

Shouldn't this be unnecessary with the DRM_DEBUG levels, or making the things tracepoints?

drm logging doesn't provide the granuality we need. We plan to add tracepoints in future patches.

Quoted 7 lines
Quoted 7 lines
drivers/gpu/drm/ivpu/Makefile new file +7 -0
+char *ivpu_platform_to_str(u32 platform)
+{
+    switch (platform) {
+    case IVPU_PLATFORM_SILICON:
+        return "IVPU_PLATFORM_SILICON";
+    case IVPU_PLATFORM_SIMICS:
+        return "IVPU_PLATFORM_SIMICS";
Quoted 8 lines
Quoted 6 lines

+    case IVPU_PLATFORM_FPGA:
+        return "IVPU_PLATFORM_FPGA";
+    default:
+        return "Invalid platform";
+    }
+}

In this entire series, this is only used in this patch, and only in one file.  Seems pointless to define it here, and have it in the header. Why shouldn't this be moved to the file it is used in, and made static?

OK, I'll move it.

Quoted 25 lines
Quoted 25 lines
drivers/gpu/drm/ivpu/Makefile new file +25 -0
+static int ivpu_get_param_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 pci_dev *pdev = to_pci_dev(vdev->drm.dev);
+    struct drm_ivpu_param *args = data;
+    int ret = 0;
+    switch (args->param) {
+    case DRM_IVPU_PARAM_DEVICE_ID:
+        args->value = pdev->device;
+        break;
+    case DRM_IVPU_PARAM_DEVICE_REVISION:
+        args->value = pdev->revision;
+        break;
+    case DRM_IVPU_PARAM_PLATFORM_TYPE:
+        args->value = vdev->platform;
+        break;
+    case DRM_IVPU_PARAM_CORE_CLOCK_RATE:
+        args->value = ivpu_hw_reg_pll_freq_get(vdev);
+        break;
+    case DRM_IVPU_PARAM_NUM_CONTEXTS:
+        args->value = ivpu_get_context_count(vdev);
+        break;
+    case DRM_IVPU_PARAM_CONTEXT_BASE_ADDRESS:
+        args->value = vdev->hw->ranges.user_low.start;
Quoted 8 lines
Quoted 6 lines

+        break;
+    case DRM_IVPU_PARAM_CONTEXT_PRIORITY:
+        args->value = file_priv->priority;
+        break;
+    default:
+        ret = -EINVAL;

This doesn't cause a switch case fallthrough warning?

No, but I will add break for consistency.

Quoted 16 lines

drivers/gpu/drm/ivpu/Makefile

Quoted 13 lines
drivers/gpu/drm/ivpu/Makefile new file +13 -0
+static int ivpu_open(struct drm_device *dev, struct drm_file *file)
+{
+    struct ivpu_device *vdev = to_ivpu_device(dev);
+    struct ivpu_file_priv *file_priv;
+
+    file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
+    if (!file_priv)
+        return -ENOMEM;
+
+    file_priv->vdev = vdev;
+    file_priv->priority = DRM_IVPU_CONTEXT_PRIORITY_NORMAL;
+
+    kref_init(&file_priv->ref);

VFS is going to maintain a refcount on the fd.  This looks like you are duplicating that ref count, which seems pointless.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Later on you use this for jobs, as each job takes a ref, but why would it be valid for jobs to hang around after the fd is closed?

This allows user space to close fd immediately without blocking the process in case the job is still being processed by the HW.

Eh.  Ok.  Maybe add a comment to that effect?

OK.

Regards,
Jacek

#9 lore raw

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

In reply to Jacek Lawrynowicz

Hi,

please find some review comments below.

Am 24.09.22 um 17:11 schrieb Jacek Lawrynowicz:

Quoted 347 lines

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

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

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

Module sources are at drivers/gpu/drm/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                            |    8 +
  drivers/gpu/drm/Kconfig                |    2 +
  drivers/gpu/drm/Makefile               |    1 +
  drivers/gpu/drm/ivpu/Kconfig           |   12 +
  drivers/gpu/drm/ivpu/Makefile          |    8 +
  drivers/gpu/drm/ivpu/TODO              |    7 +
  drivers/gpu/drm/ivpu/ivpu_drv.c        |  392 +++++++++
  drivers/gpu/drm/ivpu/ivpu_drv.h        |  153 ++++
  drivers/gpu/drm/ivpu/ivpu_hw.h         |  169 ++++
  drivers/gpu/drm/ivpu/ivpu_hw_mtl.c     | 1021 ++++++++++++++++++++++++
  drivers/gpu/drm/ivpu/ivpu_hw_mtl_reg.h |  468 +++++++++++
  drivers/gpu/drm/ivpu/ivpu_hw_reg_io.h  |  115 +++
  include/uapi/drm/ivpu_drm.h            |   95 +++
  13 files changed, 2451 insertions(+)

create mode 100644 drivers/gpu/drm/ivpu/Kconfig create mode 100644 drivers/gpu/drm/ivpu/Makefile create mode 100644 drivers/gpu/drm/ivpu/TODO create mode 100644 drivers/gpu/drm/ivpu/ivpu_drv.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_drv.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_mtl.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_mtl_reg.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_hw_reg_io.h create mode 100644 include/uapi/drm/ivpu_drm.h

MAINTAINERS +8 -0
index 9475aa701a3f..d72ceef107e6 100644
@@ -7046,6 +7046,14 @@ F:	Documentation/devicetree/bindings/gpu/vivante,gc.yaml
  F:	drivers/gpu/drm/etnaviv/
  F:	include/uapi/drm/etnaviv_drm.h
 
+DRM DRIVERS FOR VPU
+M:	Jacek Lawrynowicz <[email protected]>
+M:	Stanislaw Gruszka <[email protected]>
+S:	Supported
+T:	git git://anongit.freedesktop.org/drm/drm-misc
+F:	drivers/gpu/drm/ivpu/
+F:	include/uapi/drm/ivpu_drm.h
+
  DRM DRIVERS FOR XEN
  M:	Oleksandr Andrushchenko <[email protected]>
  L:	[email protected]
drivers/gpu/drm/Kconfig +2 -0
index 198ba846d34b..0aaac0e5366f 100644
@@ -364,6 +364,8 @@ source "drivers/gpu/drm/v3d/Kconfig"
 
  source "drivers/gpu/drm/vc4/Kconfig"
 
+source "drivers/gpu/drm/ivpu/Kconfig"
+
  source "drivers/gpu/drm/etnaviv/Kconfig"
 
  source "drivers/gpu/drm/hisilicon/Kconfig"
drivers/gpu/drm/Makefile +1 -0
index 25d0ba310509..1bfd7415c2d0 100644
@@ -94,6 +94,7 @@ obj-$(CONFIG_DRM_KMB_DISPLAY)  += kmb/
  obj-$(CONFIG_DRM_MGAG200) += mgag200/
  obj-$(CONFIG_DRM_V3D)  += v3d/
  obj-$(CONFIG_DRM_VC4)  += vc4/
+obj-$(CONFIG_DRM_IVPU)  += ivpu/
  obj-$(CONFIG_DRM_SIS)   += sis/
  obj-$(CONFIG_DRM_SAVAGE)+= savage/
  obj-$(CONFIG_DRM_VMWGFX)+= vmwgfx/
drivers/gpu/drm/ivpu/Kconfig new file +12 -0
new file mode 100644
index 000000000000..30af359c52f2
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: GPL-2.0-only
+config DRM_IVPU
+	tristate "Intel VPU for Meteor Lake and newer"
+	depends on DRM
+	depends on X86_64 && PCI
+	select SHMEM
+	help
+	  Choose this option if you have a system that has an 14th generation Intel CPU
+	  or newer. VPU stands for Versatile Processing Unit and it's a CPU-integrated
+	  inference accelerator for Computer Vision and Deep Learning applications.
+
+	  If "M" is selected, the module will be called intel_vpu.
drivers/gpu/drm/ivpu/Makefile new file +8 -0
new file mode 100644
index 000000000000..e59dc65abe6a
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-only
+# Copyright © 2022 Intel Corporation
+
+intel_vpu-y := \
+	ivpu_drv.o \
+	ivpu_hw_mtl.o
+
+obj-$(CONFIG_DRM_IVPU) += intel_vpu.o
drivers/gpu/drm/ivpu/TODO new file +7 -0
new file mode 100644
index 000000000000..4c8aa7b96024
@@ -0,0 +1,7 @@
+- 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
\ No newline at end of file
drivers/gpu/drm/ivpu/ivpu_drv.c new file +196 -0
new file mode 100644
index 000000000000..a01c7244f6e5
@@ -0,0 +1,392 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#include <linux/firmware.h>
+#include <linux/module.h>
+#include <linux/pci.h>
+
+#include <drm/drm_drv.h>
+#include <drm/drm_file.h>
+#include <drm/drm_gem.h>
+#include <drm/drm_ioctl.h>
+
+#include "ivpu_drv.h"
+#include "ivpu_hw.h"
+
+#ifndef DRIVER_VERSION_STR
+#define DRIVER_VERSION_STR __stringify(DRM_IVPU_DRIVER_MAJOR) "." \
+			   __stringify(DRM_IVPU_DRIVER_MINOR) "."
+#endif
+
+static const struct drm_driver driver;
+
+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.");
+
+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");
+
+u8 ivpu_pll_max_ratio = U8_MAX;
+module_param_named(pll_max_ratio, ivpu_pll_max_ratio, byte, 0644);
+MODULE_PARM_DESC(pll_max_ratio, "Maximum PLL ratio used to set VPU frequency");
+
+char *ivpu_platform_to_str(u32 platform)
+{
+	switch (platform) {
+	case IVPU_PLATFORM_SILICON:
+		return "IVPU_PLATFORM_SILICON";
+	case IVPU_PLATFORM_SIMICS:
+		return "IVPU_PLATFORM_SIMICS";
+	case IVPU_PLATFORM_FPGA:
+		return "IVPU_PLATFORM_FPGA";
+	default:
+		return "Invalid platform";
+	}
+}
+
+void ivpu_file_priv_get(struct ivpu_file_priv *file_priv, struct ivpu_file_priv **link)
+{
+	kref_get(&file_priv->ref);
+	*link = file_priv;
+}
+
+static void file_priv_release(struct kref *ref)
+{
+	struct ivpu_file_priv *file_priv = container_of(ref, struct ivpu_file_priv, ref);
+
+	kfree(file_priv);
+}
+
+void ivpu_file_priv_put(struct ivpu_file_priv **link)
+{
+	struct ivpu_file_priv *file_priv = *link;
+
+	*link = NULL;
+	kref_put(&file_priv->ref, file_priv_release);
+}
+
+static int ivpu_get_param_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 pci_dev *pdev = to_pci_dev(vdev->drm.dev);
+	struct drm_ivpu_param *args = data;
+	int ret = 0;
+
+	switch (args->param) {
+	case DRM_IVPU_PARAM_DEVICE_ID:
+		args->value = pdev->device;
+		break;
+	case DRM_IVPU_PARAM_DEVICE_REVISION:
+		args->value = pdev->revision;
+		break;
+	case DRM_IVPU_PARAM_PLATFORM_TYPE:
+		args->value = vdev->platform;
+		break;
+	case DRM_IVPU_PARAM_CORE_CLOCK_RATE:
+		args->value = ivpu_hw_reg_pll_freq_get(vdev);
+		break;
+	case DRM_IVPU_PARAM_NUM_CONTEXTS:
+		args->value = ivpu_get_context_count(vdev);
+		break;
+	case DRM_IVPU_PARAM_CONTEXT_BASE_ADDRESS:
+		args->value = vdev->hw->ranges.user_low.start;
+		break;
+	case DRM_IVPU_PARAM_CONTEXT_PRIORITY:
+		args->value = file_priv->priority;
+		break;
+	default:
+		ret = -EINVAL;
+	}
+
+	return ret;
+}
+
+static int ivpu_set_param_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
+{
+	struct ivpu_file_priv *file_priv = file->driver_priv;
+	struct drm_ivpu_param *args = data;
+	int ret = 0;
+
+	switch (args->param) {
+	case DRM_IVPU_PARAM_CONTEXT_PRIORITY:
+		if (args->value <= DRM_IVPU_CONTEXT_PRIORITY_REALTIME)
+			file_priv->priority = args->value;
+		else
+			ret = -EINVAL;
+		break;
+	default:
+		ret = -EINVAL;
+	}
+
+	return ret;
+}
+
+static int ivpu_open(struct drm_device *dev, struct drm_file *file)
+{
+	struct ivpu_device *vdev = to_ivpu_device(dev);
+	struct ivpu_file_priv *file_priv;
+
+	file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
+	if (!file_priv)
+		return -ENOMEM;
+
+	file_priv->vdev = vdev;
+	file_priv->priority = DRM_IVPU_CONTEXT_PRIORITY_NORMAL;
+
+	kref_init(&file_priv->ref);
+
+	file->driver_priv = file_priv;
+
+	return 0;
+}
+
+static void ivpu_postclose(struct drm_device *dev, struct drm_file *file)
+{
+	struct ivpu_file_priv *file_priv = file->driver_priv;
+
+	ivpu_file_priv_put(&file_priv);
+}
+
+static const struct drm_ioctl_desc ivpu_drm_ioctls[] = {
+	DRM_IOCTL_DEF_DRV(IVPU_GET_PARAM, ivpu_get_param_ioctl, DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(IVPU_SET_PARAM, ivpu_set_param_ioctl, DRM_RENDER_ALLOW),
+};
+
+DEFINE_DRM_GEM_FOPS(ivpu_fops);
+
+int ivpu_shutdown(struct ivpu_device *vdev)
+{
+	int ret;
+
+	ivpu_hw_irq_disable(vdev);
+
+	ret = ivpu_hw_power_down(vdev);
+	if (ret)
+		ivpu_warn(vdev, "Failed to power down HW: %d\n", ret);
+
+	return ret;
+}
+
+static const struct drm_driver driver = {
+	.driver_features = DRIVER_GEM | DRIVER_RENDER,
+
+	.open = ivpu_open,
+	.postclose = ivpu_postclose,
+
+	.ioctls = ivpu_drm_ioctls,
+	.num_ioctls = ARRAY_SIZE(ivpu_drm_ioctls),
+	.fops = &ivpu_fops,
+
+	.name = DRIVER_NAME,
+	.desc = DRIVER_DESC,
+	.date = DRIVER_DATE,
+	.major = DRM_IVPU_DRIVER_MAJOR,
+	.minor = DRM_IVPU_DRIVER_MINOR,
+};
+
+static int ivpu_irq_init(struct ivpu_device *vdev)
+{
+	struct pci_dev *pdev = to_pci_dev(vdev->drm.dev);
+	int ret;
+

drivers/gpu/drm/ivpu/ivpu_drv.c line 206

Quoted 10 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +10 -0
+	ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI | PCI_IRQ_MSIX);
+	if (ret < 0) {
+		ivpu_err(vdev, "Failed to allocate and MSI IRQ: %d\n", ret);
+		return ret;
+	}
+
+	vdev->irq = pci_irq_vector(pdev, 0);
+
+	ret = request_irq(vdev->irq, vdev->hw->ops->irq_handler, IRQF_SHARED,
+			  DRIVER_NAME, vdev);

There's devm_request_irq(). Within DRM, we have mostly adopted managed cleanup. New drivers should be written that way.

drivers/gpu/drm/ivpu/ivpu_drv.c line 215

Quoted 9 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +9 -0
+	if (ret) {
+		ivpu_err(vdev, "Failed to request an IRQ %d\n", ret);
+		pci_free_irq_vectors(pdev);
+	}
+
+	return ret;
+}
+
+static void ivpu_irq_fini(struct ivpu_device *vdev)

All these _fini functions should be replaced by managed cleanups with the devm_, drmm_ and pcim_. There are sometimes multiple ways of supporting this, but manually calling _fini should be avoided.

drivers/gpu/drm/ivpu/ivpu_drv.c line 218

Quoted 3 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +3 -0
+{
+	free_irq(vdev->irq, vdev);
+	pci_free_irq_vectors(to_pci_dev(vdev->drm.dev));

There's no pcim_alloc_irq_vectors(). It's better to add one or at least provide such an implementation within your driver.

Quoted 4 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +4 -0
+}
+
+static int ivpu_pci_init(struct ivpu_device *vdev)
+{

drivers/gpu/drm/ivpu/ivpu_drv.c line 232

Quoted 10 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +10 -0
+	struct pci_dev *pdev = to_pci_dev(vdev->drm.dev);
+	struct resource *bar0 = &pdev->resource[0];
+	struct resource *bar4 = &pdev->resource[4];
+	int ret;
+
+	ivpu_dbg(MISC, "Mapping BAR0 (RegV) %pR\n", bar0);
+	vdev->regv = devm_ioremap_resource(vdev->drm.dev, bar0);
+	if (IS_ERR(vdev->regv)) {
+		ivpu_err(vdev, "Failed to map bar 0\n");
+		return -ENOMEM;

Why not return PTR_ERR(vdev->regv) ?

drivers/gpu/drm/ivpu/ivpu_drv.c line 239

Quoted 7 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +7 -0
+	}
+
+	ivpu_dbg(MISC, "Mapping BAR4 (RegB) %pR\n", bar4);
+	vdev->regb = devm_ioremap_resource(vdev->drm.dev, bar4);
+	if (IS_ERR(vdev->regb)) {
+		ivpu_err(vdev, "Failed to map bar 4\n");
+		return -ENOMEM;

Same

drivers/gpu/drm/ivpu/ivpu_drv.c line 251

Quoted 12 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +12 -0
+	}
+
+	ret = dma_set_mask_and_coherent(vdev->drm.dev, DMA_BIT_MASK(38));
+	if (ret) {
+		ivpu_err(vdev, "Failed to set DMA mask: %d\n", ret);
+		return ret;
+	}
+
+	/* Clear any pending errors */
+	pcie_capability_clear_word(pdev, PCI_EXP_DEVSTA, 0x3f);
+
+	ret = pci_enable_device(pdev);

pcim_enable device()

Quoted 81 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +81 -0
+	if (ret) {
+		ivpu_err(vdev, "Failed to enable PCI device: %d\n", ret);
+		return ret;
+	}
+
+	pci_set_master(pdev);
+
+	return 0;
+}
+
+static void ivpu_pci_fini(struct ivpu_device *vdev)
+{
+	pci_disable_device(to_pci_dev(vdev->drm.dev));
+}
+
+static int ivpu_dev_init(struct ivpu_device *vdev)
+{
+	int ret;
+
+	vdev->hw = devm_kzalloc(vdev->drm.dev, sizeof(*vdev->hw), GFP_KERNEL);
+	if (!vdev->hw)
+		return -ENOMEM;
+
+	vdev->hw->ops = &ivpu_hw_mtl_ops;
+	vdev->platform = IVPU_PLATFORM_INVALID;
+
+	xa_init_flags(&vdev->context_xa, XA_FLAGS_ALLOC);
+	vdev->context_xa_limit.min = IVPU_GLOBAL_CONTEXT_MMU_SSID + 1;
+	vdev->context_xa_limit.max = IVPU_CONTEXT_LIMIT;
+
+	ret = ivpu_pci_init(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to initialize PCI device: %d\n", ret);
+		return ret;
+	}
+
+	ret = ivpu_irq_init(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to initialize IRQs: %d\n", ret);
+		goto err_pci_fini;
+	}
+
+	ret = ivpu_hw_info_init(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to initialize HW info: %d\n", ret);
+		goto err_irq_fini;
+	}
+
+	ret = ivpu_hw_power_up(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to power up HW: %d\n", ret);
+		goto err_irq_fini;
+	}
+
+	return 0;
+
+err_irq_fini:
+	ivpu_irq_fini(vdev);
+err_pci_fini:
+	ivpu_pci_fini(vdev);
+	return ret;
+}
+
+static void ivpu_dev_fini(struct ivpu_device *vdev)
+{
+	ivpu_shutdown(vdev);
+
+	ivpu_irq_fini(vdev);
+	ivpu_pci_fini(vdev);
+
+	WARN_ON(!xa_empty(&vdev->context_xa));
+	xa_destroy(&vdev->context_xa);
+}
+
+static struct pci_device_id ivpu_pci_ids[] = {
+	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_MTL) },
+	{ }
+};
+MODULE_DEVICE_TABLE(pci, ivpu_pci_ids);
+
+static int ivpu_probe(struct pci_dev *pdev, const struct pci_device_id *id)

drivers/gpu/drm/ivpu/ivpu_drv.c line 342

Quoted 10 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +10 -0
+{
+	struct ivpu_device *vdev;
+	int ret;
+
+	vdev = devm_drm_dev_alloc(&pdev->dev, &driver, struct ivpu_device, drm);
+	if (IS_ERR(vdev))
+		return PTR_ERR(vdev);
+
+	pci_set_drvdata(pdev, vdev);
+	vdev->drm.dev_private = vdev;

No more use of dev_private in new drivers. Your struct ivpu_device should rather embed an instance of struct drm_device and you should upcast if necessary.

Quoted 24 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +24 -0
+
+	ret = ivpu_dev_init(vdev);
+	if (ret) {
+		dev_err(&pdev->dev, "Failed to initialize VPU device: %d\n", ret);
+		return ret;
+	}
+
+	ret = drm_dev_register(&vdev->drm, 0);
+	if (ret) {
+		dev_err(&pdev->dev, "Failed to register DRM device: %d\n", ret);
+		ivpu_dev_fini(vdev);
+	}
+
+	return ret;
+}
+
+static void ivpu_remove(struct pci_dev *pdev)
+{
+	struct ivpu_device *vdev = pci_get_drvdata(pdev);
+
+	drm_dev_unregister(&vdev->drm);
+	ivpu_dev_fini(vdev);
+}
+

drivers/gpu/drm/ivpu/ivpu_drv.c line 376

Quoted 10 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +10 -0
+static struct pci_driver ivpu_pci_driver = {
+	.name = KBUILD_MODNAME,
+	.id_table = ivpu_pci_ids,
+	.probe = ivpu_probe,
+	.remove = ivpu_remove,
+};
+
+static __init int ivpu_init(void)
+{
+	pr_info("Intel VPU driver version: %s", DRIVER_VERSION_STR);

No log spam please.

drivers/gpu/drm/ivpu/ivpu_drv.c line 387

Quoted 11 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +11 -0
+
+	return pci_register_driver(&ivpu_pci_driver);
+}
+
+static __exit void ivpu_fini(void)
+{
+	pci_unregister_driver(&ivpu_pci_driver);
+}
+
+module_init(ivpu_init);
+module_exit(ivpu_fini);

Maybe just module_pci_driver().

[1] https://elixir.bootlin.com/linux/latest/source/include/linux/pci.h#L1480

Quoted 48 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +5 -0
+
+MODULE_AUTHOR("Intel Corporation");
+MODULE_DESCRIPTION(DRIVER_DESC);
+MODULE_LICENSE("GPL and additional rights");
+MODULE_VERSION(DRIVER_VERSION_STR);
drivers/gpu/drm/ivpu/ivpu_drv.h new file +37 -0
new file mode 100644
index 000000000000..43dfa78544c6
@@ -0,0 +1,153 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#ifndef __IVPU_DRV_H__
+#define __IVPU_DRV_H__
+
+#include <drm/drm_device.h>
+#include <drm/drm_mm.h>
+#include <drm/drm_print.h>
+
+#include <linux/pci.h>
+#include <linux/xarray.h>
+#include <uapi/drm/ivpu_drm.h>
+
+#define DRIVER_NAME "intel_vpu"
+#define DRIVER_DESC "Driver for Intel Versatile Processing Unit (VPU)"
+#define DRIVER_DATE "20220913"
+
+#define PCI_VENDOR_ID_INTEL 0x8086
+#define PCI_DEVICE_ID_MTL   0x7d1d
+
+#define IVPU_GLOBAL_CONTEXT_MMU_SSID 0
+#define IVPU_CONTEXT_LIMIT	     64
+#define IVPU_NUM_ENGINES	     2
+
+#define IVPU_PLATFORM_SILICON 0
+#define IVPU_PLATFORM_SIMICS  2
+#define IVPU_PLATFORM_FPGA    3
+#define IVPU_PLATFORM_INVALID 8
+
+#define IVPU_DBG_REG	 BIT(0)
+#define IVPU_DBG_IRQ	 BIT(1)
+#define IVPU_DBG_MMU	 BIT(2)
+#define IVPU_DBG_FILE	 BIT(3)
+#define IVPU_DBG_MISC	 BIT(4)

drivers/gpu/drm/ivpu/ivpu_drv.h line 47

Quoted 10 lines
drivers/gpu/drm/ivpu/ivpu_drv.h new file +10 -0
+#define IVPU_DBG_FW_BOOT BIT(5)
+#define IVPU_DBG_PM	 BIT(6)
+#define IVPU_DBG_IPC	 BIT(7)
+#define IVPU_DBG_BO	 BIT(8)
+#define IVPU_DBG_JOB	 BIT(9)
+#define IVPU_DBG_JSM	 BIT(10)
+#define IVPU_DBG_KREF	 BIT(11)
+
+#define ivpu_err(vdev, fmt, ...) \
+	dev_err((vdev)->drm.dev, "[%s] ERROR: " fmt, __func__, ##__VA_ARGS__)

I see why you want your own dbg macros. But why do you duplicate DRM's print helpers?

Quoted 5 lines
drivers/gpu/drm/ivpu/ivpu_drv.h new file +5 -0
+
+#define ivpu_err_ratelimited(vdev, fmt, ...) \
+	dev_err_ratelimited((vdev)->drm.dev, "[%s] ERROR: " fmt, __func__, ##__VA_ARGS__)
+
+#define ivpu_warn(vdev, fmt, ...) \

drivers/gpu/drm/ivpu/ivpu_drv.h line 62

Quoted 10 lines
drivers/gpu/drm/ivpu/ivpu_drv.h new file +10 -0
+	dev_warn((vdev)->drm.dev, "[%s] WARNING: " fmt, __func__, ##__VA_ARGS__)
+
+#define ivpu_warn_ratelimited(vdev, fmt, ...) \
+	dev_warn_ratelimited((vdev)->drm.dev, "[%s] WARNING: " fmt, __func__, ##__VA_ARGS__)
+
+#define ivpu_info(vdev, fmt, ...) dev_info((vdev)->drm.dev, fmt, ##__VA_ARGS__)
+
+#define ivpu_dbg(type, fmt, args...) do {                                      \
+	if (unlikely(IVPU_DBG_##type & ivpu_dbg_mask))                         \
+		dev_dbg((vdev)->drm.dev, "[%s] " fmt, #type, ##args);          \

Why not drm_dbg()?

drivers/gpu/drm/ivpu/ivpu_drv.h line 75

Quoted 13 lines
drivers/gpu/drm/ivpu/ivpu_drv.h new file +13 -0
+} while (0)
+
+#define IVPU_WA(wa_name) (vdev->wa.wa_name)
+
+struct ivpu_wa_table {
+	bool punit_disabled;
+	bool clear_runtime_mem;
+};
+
+struct ivpu_hw_info;
+
+struct ivpu_device {
+	struct drm_device drm; /* Must be first */

Does not really have to be the first. We use upcast helpers that convert from the DRM type to the driver types.

static inline struct ivpu_device *to_ivpu_device(struct drm_device *drm)

{
	return container_of(drm, struct ivpu_device, drm);
}

Just use that to get the ipvu device.

Quoted 109 lines
drivers/gpu/drm/ivpu/ivpu_drv.h new file +78 -0
+	void __iomem *regb;
+	void __iomem *regv;
+	u32 platform;
+	u32 irq;
+
+	struct ivpu_wa_table wa;
+	struct ivpu_hw_info *hw;
+
+	struct xarray context_xa;
+	struct xa_limit context_xa_limit;
+
+	struct {
+		int boot;
+		int jsm;
+		int tdr;
+		int reschedule_suspend;
+	} timeout;
+};
+
+struct ivpu_file_priv {
+	struct kref ref;
+	struct ivpu_device *vdev;
+	u32 priority;
+};
+
+extern int ivpu_dbg_mask;
+extern u8 ivpu_pll_min_ratio;
+extern u8 ivpu_pll_max_ratio;
+
+void ivpu_file_priv_get(struct ivpu_file_priv *file_priv, struct ivpu_file_priv **link);
+void ivpu_file_priv_put(struct ivpu_file_priv **link);
+char *ivpu_platform_to_str(u32 platform);
+int ivpu_shutdown(struct ivpu_device *vdev);
+
+static inline bool ivpu_is_mtl(struct ivpu_device *vdev)
+{
+	return to_pci_dev(vdev->drm.dev)->device == PCI_DEVICE_ID_MTL;
+}
+
+static inline u8 ivpu_revison(struct ivpu_device *vdev)
+{
+	return to_pci_dev(vdev->drm.dev)->revision;
+}
+
+static inline struct ivpu_device *to_ivpu_device(struct drm_device *dev)
+{
+	return container_of(dev, struct ivpu_device, drm);
+}
+
+static inline u32 ivpu_get_context_count(struct ivpu_device *vdev)
+{
+	struct xa_limit ctx_limit = vdev->context_xa_limit;
+
+	return (ctx_limit.max - ctx_limit.min + 1);
+}
+
+static inline u32 ivpu_get_platform(struct ivpu_device *vdev)
+{
+	WARN_ON_ONCE(vdev->platform == IVPU_PLATFORM_INVALID);
+	return vdev->platform;
+}
+
+static inline bool ivpu_is_silicon(struct ivpu_device *vdev)
+{
+	return ivpu_get_platform(vdev) == IVPU_PLATFORM_SILICON;
+}
+
+static inline bool ivpu_is_simics(struct ivpu_device *vdev)
+{
+	return ivpu_get_platform(vdev) == IVPU_PLATFORM_SIMICS;
+}
+
+static inline bool ivpu_is_fpga(struct ivpu_device *vdev)
+{
+	return ivpu_get_platform(vdev) == IVPU_PLATFORM_FPGA;
+}
+
+#endif /* __IVPU_DRV_H__ */
drivers/gpu/drm/ivpu/ivpu_hw.h new file +25 -0
new file mode 100644
index 000000000000..06de7501e476
@@ -0,0 +1,169 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#ifndef __IVPU_HW_H__
+#define __IVPU_HW_H__
+
+#include "ivpu_drv.h"
+
+struct ivpu_hw_ops {
+	int (*info_init)(struct ivpu_device *vdev);
+	int (*power_up)(struct ivpu_device *vdev);
+	int (*boot_fw)(struct ivpu_device *vdev);
+	int (*power_down)(struct ivpu_device *vdev);
+	bool (*is_idle)(struct ivpu_device *vdev);
+	void (*wdt_disable)(struct ivpu_device *vdev);
+	void (*diagnose_tdr)(struct ivpu_device *vdev);
+	u32 (*reg_pll_freq_get)(struct ivpu_device *vdev);
+	u32 (*reg_telemetry_offset_get)(struct ivpu_device *vdev);
+	u32 (*reg_telemetry_size_get)(struct ivpu_device *vdev);
+	u32 (*reg_telemetry_enable_get)(struct ivpu_device *vdev);
+	void (*reg_db_set)(struct ivpu_device *vdev, u32 db_id);
+	u32 (*reg_ipc_rx_addr_get)(struct ivpu_device *vdev);
+	u32 (*reg_ipc_rx_count_get)(struct ivpu_device *vdev);

drivers/gpu/drm/ivpu/ivpu_hw.h line 35

Quoted 10 lines
drivers/gpu/drm/ivpu/ivpu_hw.h new file +10 -0
+	void (*reg_ipc_tx_set)(struct ivpu_device *vdev, u32 vpu_addr);
+	void (*irq_clear)(struct ivpu_device *vdev);
+	void (*irq_enable)(struct ivpu_device *vdev);
+	void (*irq_disable)(struct ivpu_device *vdev);
+	irqreturn_t (*irq_handler)(int irq, void *ptr);
+};
+
+struct ivpu_addr_range {
+	u64 start;
+	u64 end;

resource_size_t is the canonical type here. TBH I'm surprised that linux doesn't already have a type for address ranges. There must be plenty of drivers defining such a type.

Quoted 217 lines
drivers/gpu/drm/ivpu/ivpu_hw.h new file +134 -0
+};
+
+struct ivpu_hw_info {
+	const struct ivpu_hw_ops *ops;
+	struct {
+		struct ivpu_addr_range global_low;
+		struct ivpu_addr_range global_high;
+		struct ivpu_addr_range user_low;
+		struct ivpu_addr_range user_high;
+		struct ivpu_addr_range global_aliased_pio;
+	} ranges;
+	struct {
+		u8 min_ratio;
+		u8 max_ratio;
+	} pll;
+	u32 tile_fuse;
+	u32 sku;
+	u16 config;
+};
+
+extern const struct ivpu_hw_ops ivpu_hw_mtl_ops;
+
+static inline int ivpu_hw_info_init(struct ivpu_device *vdev)
+{
+	return vdev->hw->ops->info_init(vdev);
+};
+
+static inline int ivpu_hw_power_up(struct ivpu_device *vdev)
+{
+	ivpu_dbg(PM, "HW power up\n");
+
+	return vdev->hw->ops->power_up(vdev);
+};
+
+static inline int ivpu_hw_boot_fw(struct ivpu_device *vdev)
+{
+	return vdev->hw->ops->boot_fw(vdev);
+};
+
+static inline bool ivpu_hw_is_idle(struct ivpu_device *vdev)
+{
+	return vdev->hw->ops->is_idle(vdev);
+};
+
+static inline int ivpu_hw_power_down(struct ivpu_device *vdev)
+{
+	ivpu_dbg(PM, "HW power down\n");
+
+	return vdev->hw->ops->power_down(vdev);
+};
+
+static inline void ivpu_hw_wdt_disable(struct ivpu_device *vdev)
+{
+	vdev->hw->ops->wdt_disable(vdev);
+};
+
+/* Register indirect accesses */
+static inline u32 ivpu_hw_reg_pll_freq_get(struct ivpu_device *vdev)
+{
+	return vdev->hw->ops->reg_pll_freq_get(vdev);
+};
+
+static inline u32 ivpu_hw_reg_telemetry_offset_get(struct ivpu_device *vdev)
+{
+	return vdev->hw->ops->reg_telemetry_offset_get(vdev);
+};
+
+static inline u32 ivpu_hw_reg_telemetry_size_get(struct ivpu_device *vdev)
+{
+	return vdev->hw->ops->reg_telemetry_size_get(vdev);
+};
+
+static inline u32 ivpu_hw_reg_telemetry_enable_get(struct ivpu_device *vdev)
+{
+	return vdev->hw->ops->reg_telemetry_enable_get(vdev);
+};
+
+static inline void ivpu_hw_reg_db_set(struct ivpu_device *vdev, u32 db_id)
+{
+	vdev->hw->ops->reg_db_set(vdev, db_id);
+};
+
+static inline u32 ivpu_hw_reg_ipc_rx_addr_get(struct ivpu_device *vdev)
+{
+	return vdev->hw->ops->reg_ipc_rx_addr_get(vdev);
+};
+
+static inline u32 ivpu_hw_reg_ipc_rx_count_get(struct ivpu_device *vdev)
+{
+	return vdev->hw->ops->reg_ipc_rx_count_get(vdev);
+};
+
+static inline void ivpu_hw_reg_ipc_tx_set(struct ivpu_device *vdev, u32 vpu_addr)
+{
+	vdev->hw->ops->reg_ipc_tx_set(vdev, vpu_addr);
+};
+
+static inline void ivpu_hw_irq_clear(struct ivpu_device *vdev)
+{
+	vdev->hw->ops->irq_clear(vdev);
+};
+
+static inline void ivpu_hw_irq_enable(struct ivpu_device *vdev)
+{
+	vdev->hw->ops->irq_enable(vdev);
+};
+
+static inline void ivpu_hw_irq_disable(struct ivpu_device *vdev)
+{
+	vdev->hw->ops->irq_disable(vdev);
+};
+
+static inline void ivpu_hw_init_range(struct ivpu_addr_range *range, u64 start, u64 size)
+{
+	range->start = start;
+	range->end = start + size;
+}
+
+static inline u64 ivpu_hw_range_size(const struct ivpu_addr_range *range)
+{
+	return range->end - range->start;
+}
+
+static inline u8 ivpu_hw_get_pll_ratio_in_range(u8 pll_ratio, u8 min, u8 max)
+{
+	return min_t(u8, max_t(u8, pll_ratio, min), max);
+}
+
+static inline void ivpu_hw_diagnose_tdr(struct ivpu_device *vdev)
+{
+	vdev->hw->ops->diagnose_tdr(vdev);
+}
+
+#endif /* __IVPU_HW_H__ */
drivers/gpu/drm/ivpu/ivpu_hw_mtl.c new file +77 -0
new file mode 100644
index 000000000000..17fc8e1104df
@@ -0,0 +1,1021 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#include "ivpu_drv.h"
+#include "ivpu_hw_mtl_reg.h"
+#include "ivpu_hw_reg_io.h"
+#include "ivpu_hw.h"
+
+#define TILE_FUSE_ENABLE_BOTH	     0x0
+#define TILE_FUSE_ENABLE_UPPER	     0x1
+#define TILE_FUSE_ENABLE_LOWER	     0x2
+
+#define TILE_SKU_BOTH_MTL	     0x3630
+#define TILE_SKU_LOWER_MTL	     0x3631
+#define TILE_SKU_UPPER_MTL	     0x3632
+
+/* Work point configuration values */
+#define WP_CONFIG_1_TILE_5_3_RATIO   0x0101
+#define WP_CONFIG_1_TILE_4_3_RATIO   0x0102
+#define WP_CONFIG_2_TILE_5_3_RATIO   0x0201
+#define WP_CONFIG_2_TILE_4_3_RATIO   0x0202
+#define WP_CONFIG_0_TILE_PLL_OFF     0x0000
+
+#define PLL_REF_CLK_FREQ	     (50 * 1000000)
+#define PLL_SIMULATION_FREQ	     (10 * 1000000)
+#define PLL_RATIO_TO_FREQ(x)	     ((x) * PLL_REF_CLK_FREQ)
+#define PLL_DEFAULT_EPP_VALUE	     0x80
+
+#define TIM_SAFE_ENABLE		     0xf1d0dead
+#define TIM_WATCHDOG_RESET_VALUE     0xffffffff
+
+#define TIMEOUT_US		     (150 * USEC_PER_MSEC)
+#define PWR_ISLAND_STATUS_TIMEOUT_US (5 * USEC_PER_MSEC)
+#define PLL_TIMEOUT_US		     (1500 * USEC_PER_MSEC)
+#define IDLE_TIMEOUT_US		     (500 * USEC_PER_MSEC)
+
+#define ICB_0_IRQ_MASK ((REG_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, HOST_IPC_FIFO_INT)) | \
+			(REG_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_0_INT)) | \
+			(REG_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_1_INT)) | \
+			(REG_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_2_INT)) | \
+			(REG_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, NOC_FIREWALL_INT)) | \
+			(REG_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, CPU_INT_REDIRECT_0_INT)) | \
+			(REG_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, CPU_INT_REDIRECT_1_INT)))
+
+#define ICB_1_IRQ_MASK ((REG_FLD(MTL_VPU_HOST_SS_ICB_STATUS_1, CPU_INT_REDIRECT_2_INT)) | \
+			(REG_FLD(MTL_VPU_HOST_SS_ICB_STATUS_1, CPU_INT_REDIRECT_3_INT)) | \
+			(REG_FLD(MTL_VPU_HOST_SS_ICB_STATUS_1, CPU_INT_REDIRECT_4_INT)))
+
+#define ICB_0_1_IRQ_MASK ((((u64)ICB_1_IRQ_MASK) << 32) | ICB_0_IRQ_MASK)
+
+#define BUTTRESS_IRQ_MASK ((REG_FLD(MTL_BUTTRESS_INTERRUPT_STAT, FREQ_CHANGE)) | \
+			   (REG_FLD(MTL_BUTTRESS_INTERRUPT_STAT, ATS_ERR)) | \
+			   (REG_FLD(MTL_BUTTRESS_INTERRUPT_STAT, UFI_ERR)))
+
+#define ITF_FIREWALL_VIOLATION_MASK ((REG_FLD(MTL_VPU_HOST_SS_FW_SOC_IRQ_EN, CSS_ROM_CMX)) | \
+				     (REG_FLD(MTL_VPU_HOST_SS_FW_SOC_IRQ_EN, CSS_DBG)) | \
+				     (REG_FLD(MTL_VPU_HOST_SS_FW_SOC_IRQ_EN, CSS_CTRL)) | \
+				     (REG_FLD(MTL_VPU_HOST_SS_FW_SOC_IRQ_EN, DEC400)) | \
+				     (REG_FLD(MTL_VPU_HOST_SS_FW_SOC_IRQ_EN, MSS_NCE)) | \
+				     (REG_FLD(MTL_VPU_HOST_SS_FW_SOC_IRQ_EN, MSS_MBI)) | \
+				     (REG_FLD(MTL_VPU_HOST_SS_FW_SOC_IRQ_EN, MSS_MBI_CMX)))
+
+static void ivpu_hw_read_platform(struct ivpu_device *vdev)
+{
+	u32 gen_ctrl = REGV_RD32(MTL_VPU_HOST_SS_GEN_CTRL);
+	u32 platform = REG_GET_FLD(MTL_VPU_HOST_SS_GEN_CTRL, PS, gen_ctrl);
+
+	if  (platform == IVPU_PLATFORM_SIMICS || platform == IVPU_PLATFORM_FPGA)
+		vdev->platform = platform;
+	else
+		vdev->platform = IVPU_PLATFORM_SILICON;
+
+	ivpu_dbg(MISC, "Platform type: %s (%d)\n",
+		 ivpu_platform_to_str(vdev->platform), vdev->platform);
+}

drivers/gpu/drm/ivpu/ivpu_hw_mtl.c line 87

Quoted 10 lines
drivers/gpu/drm/ivpu/ivpu_hw_mtl.c new file +10 -0
+
+static void ivpu_hw_wa_init(struct ivpu_device *vdev)
+{
+	vdev->wa.punit_disabled = ivpu_is_fpga(vdev);
+	vdev->wa.clear_runtime_mem = true;
+}
+
+static void ivpu_hw_timeouts_init(struct ivpu_device *vdev)
+{
+	if (ivpu_is_simics(vdev) || ivpu_is_fpga(vdev)) {

I have seen such tests in multiple locations. Wouldn't it be better to write entirely different helpers for each? Handling different models/revisions in the same function is usually bad advice.

Best regards
Thomas

Quoted 1,630 lines
drivers/gpu/drm/ivpu/ivpu_hw_mtl.c new file +934 -0
+		vdev->timeout.boot = 100000;
+		vdev->timeout.jsm = 50000;
+		vdev->timeout.tdr = 2000000;
+		vdev->timeout.reschedule_suspend = 1000;
+	} else {
+		vdev->timeout.boot = 1000;
+		vdev->timeout.jsm = 500;
+		vdev->timeout.tdr = 2000;
+		vdev->timeout.reschedule_suspend = 10;
+	}
+}
+
+static int ivpu_pll_wait_for_cmd_send(struct ivpu_device *vdev)
+{
+	return REGB_POLL_FLD(MTL_BUTTRESS_WP_REQ_CMD, SEND, 0, PLL_TIMEOUT_US);
+}
+
+/* Send KMD initiated workpoint change */
+static int ivpu_pll_cmd_send(struct ivpu_device *vdev, u16 min_ratio, u16 max_ratio,
+			     u16 target_ratio, u16 config)
+{
+	int ret;
+	u32 val;
+
+	ret = ivpu_pll_wait_for_cmd_send(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to sync before WP request: %d\n", ret);
+		return ret;
+	}
+
+	val = REGB_RD32(MTL_BUTTRESS_WP_REQ_PAYLOAD0);
+	val = REG_SET_FLD_NUM(MTL_BUTTRESS_WP_REQ_PAYLOAD0, MIN_RATIO, min_ratio, val);
+	val = REG_SET_FLD_NUM(MTL_BUTTRESS_WP_REQ_PAYLOAD0, MAX_RATIO, max_ratio, val);
+	REGB_WR32(MTL_BUTTRESS_WP_REQ_PAYLOAD0, val);
+
+	val = REGB_RD32(MTL_BUTTRESS_WP_REQ_PAYLOAD1);
+	val = REG_SET_FLD_NUM(MTL_BUTTRESS_WP_REQ_PAYLOAD1, TARGET_RATIO, target_ratio, val);
+	val = REG_SET_FLD_NUM(MTL_BUTTRESS_WP_REQ_PAYLOAD1, EPP, PLL_DEFAULT_EPP_VALUE, val);
+	REGB_WR32(MTL_BUTTRESS_WP_REQ_PAYLOAD1, val);
+
+	val = REGB_RD32(MTL_BUTTRESS_WP_REQ_PAYLOAD2);
+	val = REG_SET_FLD_NUM(MTL_BUTTRESS_WP_REQ_PAYLOAD2, CONFIG, config, val);
+	REGB_WR32(MTL_BUTTRESS_WP_REQ_PAYLOAD2, val);
+
+	val = REGB_RD32(MTL_BUTTRESS_WP_REQ_CMD);
+	val = REG_SET_FLD(MTL_BUTTRESS_WP_REQ_CMD, SEND, val);
+	REGB_WR32(MTL_BUTTRESS_WP_REQ_CMD, val);
+
+	ret = ivpu_pll_wait_for_cmd_send(vdev);
+	if (ret)
+		ivpu_err(vdev, "Failed to sync after WP request: %d\n", ret);
+
+	return ret;
+}
+
+static int ivpu_pll_wait_for_lock(struct ivpu_device *vdev, bool enable)
+{
+	u32 exp_val = enable ? 0x1 : 0x0;
+
+	if (IVPU_WA(punit_disabled))
+		return 0;
+
+	return REGB_POLL_FLD(MTL_BUTTRESS_PLL_STATUS, LOCK, exp_val, PLL_TIMEOUT_US);
+}
+
+static int ivpu_pll_wait_for_status_ready(struct ivpu_device *vdev)
+{
+	if (IVPU_WA(punit_disabled))
+		return 0;
+
+	return REGB_POLL_FLD(MTL_BUTTRESS_VPU_STATUS, READY, 1, PLL_TIMEOUT_US);
+}
+
+static u16 ivpu_hw_mtl_reg_pll_min_ratio_get(struct ivpu_device *vdev)
+{
+	if (IVPU_WA(punit_disabled))
+		return 0;
+
+	return REGB_RD32(MTL_BUTTRESS_FMIN_FUSE) & MTL_BUTTRESS_FMIN_FUSE_RATIO_MASK;
+}
+
+static u16 ivpu_hw_mtl_reg_pll_max_ratio_get(struct ivpu_device *vdev)
+{
+	if (IVPU_WA(punit_disabled))
+		return 0;
+
+	return REGB_RD32(MTL_BUTTRESS_FMAX_FUSE) & MTL_BUTTRESS_FMAX_FUSE_RATIO_MASK;
+}
+
+static int ivpu_pll_drive(struct ivpu_device *vdev, bool enable)
+{
+	int ret;
+	struct ivpu_hw_info *hw = vdev->hw;
+	u16 target_ratio;
+	u16 config;
+
+	if (IVPU_WA(punit_disabled)) {
+		ivpu_dbg(PM, "Skipping PLL request on %s\n", ivpu_platform_to_str(vdev->platform));
+		return 0;
+	}
+
+	if (enable) {
+		u8 pll_hw_min_ratio = ivpu_hw_mtl_reg_pll_min_ratio_get(vdev);
+		u8 pll_hw_max_ratio = ivpu_hw_mtl_reg_pll_max_ratio_get(vdev);
+
+		hw->pll.max_ratio = ivpu_hw_get_pll_ratio_in_range(ivpu_pll_max_ratio,
+								   pll_hw_min_ratio,
+								   pll_hw_max_ratio);
+		hw->pll.min_ratio = ivpu_hw_get_pll_ratio_in_range(ivpu_pll_min_ratio,
+								   pll_hw_min_ratio,
+								   pll_hw_max_ratio);
+		if (hw->pll.max_ratio < hw->pll.min_ratio) {
+			ivpu_err(vdev, "Invalid pll ratio values, min 0x%x max 0x%x\n",
+				 hw->pll.min_ratio, hw->pll.max_ratio);
+			return -EINVAL;
+		}
+
+		target_ratio = hw->pll.min_ratio;
+		config = hw->config;
+	} else {
+		target_ratio = 0;
+		config = 0;
+	}
+
+	ivpu_dbg(PM, "PLL workpoint request: %d Hz\n", PLL_RATIO_TO_FREQ(target_ratio));
+
+	ret = ivpu_pll_cmd_send(vdev, hw->pll.min_ratio, hw->pll.max_ratio, target_ratio, config);
+	if (ret) {
+		ivpu_err(vdev, "Failed to send PLL workpoint request: %d\n", ret);
+		return ret;
+	}
+
+	ret = ivpu_pll_wait_for_lock(vdev, enable);
+	if (ret) {
+		ivpu_err(vdev, "Timed out waiting for PLL lock\n");
+		return ret;
+	}
+
+	if (enable) {
+		ret = ivpu_pll_wait_for_status_ready(vdev);
+		if (ret) {
+			ivpu_err(vdev, "Timed out waiting for PLL ready status\n");
+			return ret;
+		}
+	}
+
+	return 0;
+}
+
+static int ivpu_pll_enable(struct ivpu_device *vdev)
+{
+	return ivpu_pll_drive(vdev, true);
+}
+
+static int ivpu_pll_disable(struct ivpu_device *vdev)
+{
+	return ivpu_pll_drive(vdev, false);
+}
+
+static void ivpu_boot_host_ss_rst_clr_assert(struct ivpu_device *vdev)
+{
+	u32 val = REGV_RD32(MTL_VPU_HOST_SS_CPR_RST_CLR);
+
+	val = REG_SET_FLD(MTL_VPU_HOST_SS_CPR_RST_CLR, TOP_NOC, val);
+	val = REG_SET_FLD(MTL_VPU_HOST_SS_CPR_RST_CLR, DSS_MAS, val);
+	val = REG_SET_FLD(MTL_VPU_HOST_SS_CPR_RST_CLR, MSS_MAS, val);
+
+	REGV_WR32(MTL_VPU_HOST_SS_CPR_RST_CLR, val);
+}
+
+static void ivpu_boot_host_ss_rst_drive(struct ivpu_device *vdev, bool enable)
+{
+	u32 val = REGV_RD32(MTL_VPU_HOST_SS_CPR_RST_SET);
+
+	if (enable) {
+		val = REG_SET_FLD(MTL_VPU_HOST_SS_CPR_RST_SET, TOP_NOC, val);
+		val = REG_SET_FLD(MTL_VPU_HOST_SS_CPR_RST_SET, DSS_MAS, val);
+		val = REG_SET_FLD(MTL_VPU_HOST_SS_CPR_RST_SET, MSS_MAS, val);
+	} else {
+		val = REG_CLR_FLD(MTL_VPU_HOST_SS_CPR_RST_SET, TOP_NOC, val);
+		val = REG_CLR_FLD(MTL_VPU_HOST_SS_CPR_RST_SET, DSS_MAS, val);
+		val = REG_CLR_FLD(MTL_VPU_HOST_SS_CPR_RST_SET, MSS_MAS, val);
+	}
+
+	REGV_WR32(MTL_VPU_HOST_SS_CPR_RST_SET, val);
+}
+
+static void ivpu_boot_host_ss_clk_drive(struct ivpu_device *vdev, bool enable)
+{
+	u32 val = REGV_RD32(MTL_VPU_HOST_SS_CPR_CLK_SET);
+
+	if (enable) {
+		val = REG_SET_FLD(MTL_VPU_HOST_SS_CPR_CLK_SET, TOP_NOC, val);
+		val = REG_SET_FLD(MTL_VPU_HOST_SS_CPR_CLK_SET, DSS_MAS, val);
+		val = REG_SET_FLD(MTL_VPU_HOST_SS_CPR_CLK_SET, MSS_MAS, val);
+	} else {
+		val = REG_CLR_FLD(MTL_VPU_HOST_SS_CPR_CLK_SET, TOP_NOC, val);
+		val = REG_CLR_FLD(MTL_VPU_HOST_SS_CPR_CLK_SET, DSS_MAS, val);
+		val = REG_CLR_FLD(MTL_VPU_HOST_SS_CPR_CLK_SET, MSS_MAS, val);
+	}
+
+	REGV_WR32(MTL_VPU_HOST_SS_CPR_CLK_SET, val);
+}
+
+static int ivpu_boot_noc_qreqn_check(struct ivpu_device *vdev, u32 exp_val)
+{
+	u32 val = REGV_RD32(MTL_VPU_HOST_SS_NOC_QREQN);
+
+	if (!REG_TEST_FLD_NUM(MTL_VPU_HOST_SS_NOC_QREQN, TOP_SOCMMIO, exp_val, val))
+		return -EIO;
+
+	return 0;
+}
+
+static int ivpu_boot_noc_qacceptn_check(struct ivpu_device *vdev, u32 exp_val)
+{
+	u32 val = REGV_RD32(MTL_VPU_HOST_SS_NOC_QACCEPTN);
+
+	if (!REG_TEST_FLD_NUM(MTL_VPU_HOST_SS_NOC_QACCEPTN, TOP_SOCMMIO, exp_val, val))
+		return -EIO;
+
+	return 0;
+}
+
+static int ivpu_boot_noc_qdeny_check(struct ivpu_device *vdev, u32 exp_val)
+{
+	u32 val = REGV_RD32(MTL_VPU_HOST_SS_NOC_QDENY);
+
+	if (!REG_TEST_FLD_NUM(MTL_VPU_HOST_SS_NOC_QDENY, TOP_SOCMMIO, exp_val, val))
+		return -EIO;
+
+	return 0;
+}
+
+static int ivpu_boot_top_noc_qrenqn_check(struct ivpu_device *vdev, u32 exp_val)
+{
+	u32 val = REGV_RD32(MTL_VPU_TOP_NOC_QREQN);
+
+	if (!REG_TEST_FLD_NUM(MTL_VPU_TOP_NOC_QREQN, CPU_CTRL, exp_val, val) ||
+	    !REG_TEST_FLD_NUM(MTL_VPU_TOP_NOC_QREQN, HOSTIF_L2CACHE, exp_val, val))
+		return -EIO;
+
+	return 0;
+}
+
+static int ivpu_boot_top_noc_qacceptn_check(struct ivpu_device *vdev, u32 exp_val)
+{
+	u32 val = REGV_RD32(MTL_VPU_TOP_NOC_QACCEPTN);
+
+	if (!REG_TEST_FLD_NUM(MTL_VPU_TOP_NOC_QACCEPTN, CPU_CTRL, exp_val, val) ||
+	    !REG_TEST_FLD_NUM(MTL_VPU_TOP_NOC_QACCEPTN, HOSTIF_L2CACHE, exp_val, val))
+		return -EIO;
+
+	return 0;
+}
+
+static int ivpu_boot_top_noc_qdeny_check(struct ivpu_device *vdev, u32 exp_val)
+{
+	u32 val = REGV_RD32(MTL_VPU_TOP_NOC_QDENY);
+
+	if (!REG_TEST_FLD_NUM(MTL_VPU_TOP_NOC_QDENY, CPU_CTRL, exp_val, val) ||
+	    !REG_TEST_FLD_NUM(MTL_VPU_TOP_NOC_QDENY, HOSTIF_L2CACHE, exp_val, val))
+		return -EIO;
+
+	return 0;
+}
+
+static int ivpu_boot_host_ss_configure(struct ivpu_device *vdev)
+{
+	ivpu_boot_host_ss_rst_clr_assert(vdev);
+
+	return ivpu_boot_noc_qreqn_check(vdev, 0x0);
+}
+
+static void ivpu_boot_vpu_idle_gen_disable(struct ivpu_device *vdev)
+{
+	REGV_WR32(MTL_VPU_HOST_SS_AON_VPU_IDLE_GEN, 0x0);
+}
+
+static int ivpu_boot_host_ss_axi_drive(struct ivpu_device *vdev, bool enable)
+{
+	int ret;
+	u32 val;
+
+	val = REGV_RD32(MTL_VPU_HOST_SS_NOC_QREQN);
+	if (enable)
+		val = REG_SET_FLD(MTL_VPU_HOST_SS_NOC_QREQN, TOP_SOCMMIO, val);
+	else
+		val = REG_CLR_FLD(MTL_VPU_HOST_SS_NOC_QREQN, TOP_SOCMMIO, val);
+	REGV_WR32(MTL_VPU_HOST_SS_NOC_QREQN, val);
+
+	ret = ivpu_boot_noc_qacceptn_check(vdev, enable ? 0x1 : 0x0);
+	if (ret) {
+		ivpu_err(vdev, "Failed qacceptn check: %d\n", ret);
+		return ret;
+	}
+
+	ret = ivpu_boot_noc_qdeny_check(vdev, 0x0);
+	if (ret)
+		ivpu_err(vdev, "Failed qdeny check: %d\n", ret);
+
+	return ret;
+}
+
+static int ivpu_boot_host_ss_axi_enable(struct ivpu_device *vdev)
+{
+	return ivpu_boot_host_ss_axi_drive(vdev, true);
+}
+
+static int ivpu_boot_host_ss_axi_disable(struct ivpu_device *vdev)
+{
+	return ivpu_boot_host_ss_axi_drive(vdev, false);
+}
+
+static int ivpu_boot_host_ss_top_noc_drive(struct ivpu_device *vdev, bool enable)
+{
+	int ret;
+	u32 val;
+
+	val = REGV_RD32(MTL_VPU_TOP_NOC_QREQN);
+	if (enable) {
+		val = REG_SET_FLD(MTL_VPU_TOP_NOC_QREQN, CPU_CTRL, val);
+		val = REG_SET_FLD(MTL_VPU_TOP_NOC_QREQN, HOSTIF_L2CACHE, val);
+	} else {
+		val = REG_CLR_FLD(MTL_VPU_TOP_NOC_QREQN, CPU_CTRL, val);
+		val = REG_CLR_FLD(MTL_VPU_TOP_NOC_QREQN, HOSTIF_L2CACHE, val);
+	}
+	REGV_WR32(MTL_VPU_TOP_NOC_QREQN, val);
+
+	ret = ivpu_boot_top_noc_qacceptn_check(vdev, enable ? 0x1 : 0x0);
+	if (ret) {
+		ivpu_err(vdev, "Failed qacceptn check: %d\n", ret);
+		return ret;
+	}
+
+	ret = ivpu_boot_top_noc_qdeny_check(vdev, 0x0);
+	if (ret)
+		ivpu_err(vdev, "Failed qdeny check: %d\n", ret);
+
+	return ret;
+}
+
+static int ivpu_boot_host_ss_top_noc_enable(struct ivpu_device *vdev)
+{
+	return ivpu_boot_host_ss_top_noc_drive(vdev, true);
+}
+
+static int ivpu_boot_host_ss_top_noc_disable(struct ivpu_device *vdev)
+{
+	return ivpu_boot_host_ss_top_noc_drive(vdev, false);
+}
+
+static void ivpu_boot_pwr_island_trickle_drive(struct ivpu_device *vdev, bool enable)
+{
+	u32 val = REGV_RD32(MTL_VPU_HOST_SS_AON_PWR_ISLAND_TRICKLE_EN0);
+
+	if (enable)
+		val = REG_SET_FLD(MTL_VPU_HOST_SS_AON_PWR_ISLAND_TRICKLE_EN0, MSS_CPU, val);
+	else
+		val = REG_CLR_FLD(MTL_VPU_HOST_SS_AON_PWR_ISLAND_TRICKLE_EN0, MSS_CPU, val);
+
+	REGV_WR32(MTL_VPU_HOST_SS_AON_PWR_ISLAND_TRICKLE_EN0, val);
+}
+
+static void ivpu_boot_pwr_island_drive(struct ivpu_device *vdev, bool enable)
+{
+	u32 val = REGV_RD32(MTL_VPU_HOST_SS_AON_PWR_ISLAND_EN0);
+
+	if (enable)
+		val = REG_SET_FLD(MTL_VPU_HOST_SS_AON_PWR_ISLAND_EN0, MSS_CPU, val);
+	else
+		val = REG_CLR_FLD(MTL_VPU_HOST_SS_AON_PWR_ISLAND_EN0, MSS_CPU, val);
+
+	REGV_WR32(MTL_VPU_HOST_SS_AON_PWR_ISLAND_EN0, val);
+}
+
+static int ivpu_boot_wait_for_pwr_island_status(struct ivpu_device *vdev, u32 exp_val)
+{
+	/* FPGA model (UPF) is not power aware, skipped Power Island polling */
+	if (ivpu_is_fpga(vdev))
+		return 0;
+
+	return REGV_POLL_FLD(MTL_VPU_HOST_SS_AON_PWR_ISLAND_STATUS0, MSS_CPU,
+			     exp_val, PWR_ISLAND_STATUS_TIMEOUT_US);
+}
+
+static void ivpu_boot_pwr_island_isolation_drive(struct ivpu_device *vdev, bool enable)
+{
+	u32 val = REGV_RD32(MTL_VPU_HOST_SS_AON_PWR_ISO_EN0);
+
+	if (enable)
+		val = REG_SET_FLD(MTL_VPU_HOST_SS_AON_PWR_ISO_EN0, MSS_CPU, val);
+	else
+		val = REG_CLR_FLD(MTL_VPU_HOST_SS_AON_PWR_ISO_EN0, MSS_CPU, val);
+
+	REGV_WR32(MTL_VPU_HOST_SS_AON_PWR_ISO_EN0, val);
+}
+
+static void ivpu_boot_dpu_active_drive(struct ivpu_device *vdev, bool enable)
+{
+	u32 val = REGV_RD32(MTL_VPU_HOST_SS_AON_DPU_ACTIVE);
+
+	if (enable)
+		val = REG_SET_FLD(MTL_VPU_HOST_SS_AON_DPU_ACTIVE, DPU_ACTIVE, val);
+	else
+		val = REG_CLR_FLD(MTL_VPU_HOST_SS_AON_DPU_ACTIVE, DPU_ACTIVE, val);
+
+	REGV_WR32(MTL_VPU_HOST_SS_AON_DPU_ACTIVE, val);
+}
+
+static int ivpu_boot_pwr_domain_disable(struct ivpu_device *vdev)
+{
+	ivpu_boot_dpu_active_drive(vdev, false);
+	ivpu_boot_pwr_island_isolation_drive(vdev, true);
+	ivpu_boot_pwr_island_trickle_drive(vdev, false);
+	ivpu_boot_pwr_island_drive(vdev, false);
+
+	return ivpu_boot_wait_for_pwr_island_status(vdev, 0x0);
+}
+
+static int ivpu_boot_pwr_domain_enable(struct ivpu_device *vdev)
+{
+	int ret;
+
+	ivpu_boot_pwr_island_trickle_drive(vdev, true);
+	ivpu_boot_pwr_island_drive(vdev, true);
+
+	ret = ivpu_boot_wait_for_pwr_island_status(vdev, 0x1);
+	if (ret) {
+		ivpu_err(vdev, "Timed out waiting for power island status\n");
+		return ret;
+	}
+
+	ret = ivpu_boot_top_noc_qrenqn_check(vdev, 0x0);
+	if (ret) {
+		ivpu_err(vdev, "Failed qrenqn check %d\n", ret);
+		return ret;
+	}
+
+	ivpu_boot_host_ss_clk_drive(vdev, true);
+	ivpu_boot_pwr_island_isolation_drive(vdev, false);
+	ivpu_boot_host_ss_rst_drive(vdev, true);
+	ivpu_boot_dpu_active_drive(vdev, true);
+
+	return ret;
+}
+
+static void ivpu_boot_no_snoop_enable(struct ivpu_device *vdev)
+{
+	u32 val = REGV_RD32(MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES);
+
+	val = REG_SET_FLD(MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES, NOSNOOP_OVERRIDE_EN, val);
+	val = REG_SET_FLD(MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES, AW_NOSNOOP_OVERRIDE, val);
+	val = REG_SET_FLD(MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES, AR_NOSNOOP_OVERRIDE, val);
+
+	REGV_WR32(MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES, val);
+}
+
+static void ivpu_boot_tbu_mmu_enable(struct ivpu_device *vdev)
+{
+	u32 val = REGV_RD32(MTL_VPU_HOST_IF_TBU_MMUSSIDV);
+
+	if (ivpu_is_fpga(vdev)) {
+		val = REG_SET_FLD(MTL_VPU_HOST_IF_TBU_MMUSSIDV, TBU0_AWMMUSSIDV, val);
+		val = REG_SET_FLD(MTL_VPU_HOST_IF_TBU_MMUSSIDV, TBU0_ARMMUSSIDV, val);
+		val = REG_SET_FLD(MTL_VPU_HOST_IF_TBU_MMUSSIDV, TBU2_AWMMUSSIDV, val);
+		val = REG_SET_FLD(MTL_VPU_HOST_IF_TBU_MMUSSIDV, TBU2_ARMMUSSIDV, val);
+	} else {
+		val = REG_SET_FLD(MTL_VPU_HOST_IF_TBU_MMUSSIDV, TBU0_AWMMUSSIDV, val);
+		val = REG_SET_FLD(MTL_VPU_HOST_IF_TBU_MMUSSIDV, TBU0_ARMMUSSIDV, val);
+		val = REG_SET_FLD(MTL_VPU_HOST_IF_TBU_MMUSSIDV, TBU1_AWMMUSSIDV, val);
+		val = REG_SET_FLD(MTL_VPU_HOST_IF_TBU_MMUSSIDV, TBU1_ARMMUSSIDV, val);
+		val = REG_SET_FLD(MTL_VPU_HOST_IF_TBU_MMUSSIDV, TBU2_AWMMUSSIDV, val);
+		val = REG_SET_FLD(MTL_VPU_HOST_IF_TBU_MMUSSIDV, TBU2_ARMMUSSIDV, val);
+		val = REG_SET_FLD(MTL_VPU_HOST_IF_TBU_MMUSSIDV, TBU3_AWMMUSSIDV, val);
+		val = REG_SET_FLD(MTL_VPU_HOST_IF_TBU_MMUSSIDV, TBU3_ARMMUSSIDV, val);
+	}
+
+	REGV_WR32(MTL_VPU_HOST_IF_TBU_MMUSSIDV, val);
+}
+
+static void ivpu_boot_soc_cpu_boot(struct ivpu_device *vdev)
+{
+	u32 val;
+
+	val = REGV_RD32(MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC);
+	val = REG_SET_FLD(MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC, IRQI_RSTRUN0, val);
+
+	val = REG_CLR_FLD(MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC, IRQI_RSTVEC, val);
+	REGV_WR32(MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC, val);
+
+	val = REG_SET_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 = 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);
+}
+
+static int ivpu_boot_d0i3_drive(struct ivpu_device *vdev, bool enable)
+{
+	int ret;
+	u32 val;
+
+	ret = REGB_POLL_FLD(MTL_BUTTRESS_VPU_D0I3_CONTROL, INPROGRESS, 0, TIMEOUT_US);
+	if (ret) {
+		ivpu_err(vdev, "Failed to sync before D0i3 tansition: %d\n", ret);
+		return ret;
+	}
+
+	val = REGB_RD32(MTL_BUTTRESS_VPU_D0I3_CONTROL);
+	if (enable)
+		val = REG_SET_FLD(MTL_BUTTRESS_VPU_D0I3_CONTROL, I3, val);
+	else
+		val = REG_CLR_FLD(MTL_BUTTRESS_VPU_D0I3_CONTROL, I3, val);
+	REGB_WR32(MTL_BUTTRESS_VPU_D0I3_CONTROL, val);
+
+	ret = REGB_POLL_FLD(MTL_BUTTRESS_VPU_D0I3_CONTROL, INPROGRESS, 0, TIMEOUT_US);
+	if (ret)
+		ivpu_err(vdev, "Failed to sync after D0i3 tansition: %d\n", ret);
+
+	return ret;
+}
+
+static int ivpu_hw_mtl_info_init(struct ivpu_device *vdev)
+{
+	struct ivpu_hw_info *hw = vdev->hw;
+	u32 tile_fuse;
+
+	tile_fuse = REGB_RD32(MTL_BUTTRESS_TILE_FUSE);
+	if (!REG_TEST_FLD(MTL_BUTTRESS_TILE_FUSE, VALID, tile_fuse))
+		ivpu_warn(vdev, "Tile Fuse: Invalid (0x%x)\n", tile_fuse);
+
+	hw->tile_fuse = REG_GET_FLD(MTL_BUTTRESS_TILE_FUSE, SKU, tile_fuse);
+	switch (hw->tile_fuse) {
+	case TILE_FUSE_ENABLE_LOWER:
+		hw->sku = TILE_SKU_LOWER_MTL;
+		hw->config = WP_CONFIG_1_TILE_5_3_RATIO;
+		ivpu_dbg(MISC, "Tile Fuse: Enable Lower\n");
+		break;
+	case TILE_FUSE_ENABLE_UPPER:
+		hw->sku = TILE_SKU_UPPER_MTL;
+		hw->config = WP_CONFIG_1_TILE_4_3_RATIO;
+		ivpu_dbg(MISC, "Tile Fuse: Enable Upper\n");
+		break;
+	case TILE_FUSE_ENABLE_BOTH:
+		hw->sku = TILE_SKU_BOTH_MTL;
+		hw->config = WP_CONFIG_2_TILE_5_3_RATIO;
+		ivpu_dbg(MISC, "Tile Fuse: Enable Both\n");
+		break;
+	default:
+		hw->config = WP_CONFIG_0_TILE_PLL_OFF;
+		ivpu_dbg(MISC, "Tile Fuse: Disable\n");
+		break;
+	}
+
+	ivpu_hw_init_range(&hw->ranges.global_low, 0x80000000, SZ_512M);
+	ivpu_hw_init_range(&hw->ranges.global_high, 0x180000000, SZ_2M);
+	ivpu_hw_init_range(&hw->ranges.user_low, 0xc0000000, 255 * SZ_1M);
+	ivpu_hw_init_range(&hw->ranges.user_high, 0x180000000, SZ_2G);
+	hw->ranges.global_aliased_pio = hw->ranges.user_low;
+
+	return 0;
+}
+
+static int ivpu_hw_mtl_reset(struct ivpu_device *vdev)
+{
+	int ret;
+	u32 val;
+
+	if (IVPU_WA(punit_disabled))
+		return 0;
+
+	ret = REGB_POLL_FLD(MTL_BUTTRESS_VPU_IP_RESET, TRIGGER, 0, TIMEOUT_US);
+	if (ret) {
+		ivpu_err(vdev, "Timed out waiting for TRIGGER bit\n");
+		return ret;
+	}
+
+	val = REGB_RD32(MTL_BUTTRESS_VPU_IP_RESET);
+	val = REG_SET_FLD(MTL_BUTTRESS_VPU_IP_RESET, TRIGGER, val);
+	REGB_WR32(MTL_BUTTRESS_VPU_IP_RESET, val);
+
+	ret = REGB_POLL_FLD(MTL_BUTTRESS_VPU_IP_RESET, TRIGGER, 0, TIMEOUT_US);
+	if (ret)
+		ivpu_err(vdev, "Timed out waiting for RESET completion\n");
+
+	return ret;
+}
+
+static int ivpu_hw_mtl_d0i3_enable(struct ivpu_device *vdev)
+{
+	int ret;
+
+	ret = ivpu_boot_d0i3_drive(vdev, true);
+	if (ret)
+		ivpu_err(vdev, "Failed to enable D0i3: %d\n", ret);
+
+	udelay(5); /* VPU requires 5 us to complete the transition */
+
+	return ret;
+}
+
+static int ivpu_hw_mtl_d0i3_disable(struct ivpu_device *vdev)
+{
+	int ret;
+
+	ret = ivpu_boot_d0i3_drive(vdev, false);
+	if (ret)
+		ivpu_err(vdev, "Failed to disable D0i3: %d\n", ret);
+
+	return ret;
+}
+
+static int ivpu_hw_mtl_power_up(struct ivpu_device *vdev)
+{
+	int ret;
+
+	ivpu_hw_read_platform(vdev);
+	ivpu_hw_wa_init(vdev);
+	ivpu_hw_timeouts_init(vdev);
+
+	ret = ivpu_hw_mtl_reset(vdev);
+	if (ret)
+		ivpu_warn(vdev, "Failed to reset HW: %d\n", ret);
+
+	ret = ivpu_hw_mtl_d0i3_disable(vdev);
+	if (ret)
+		ivpu_warn(vdev, "Failed to disable D0I3: %d\n", ret);
+
+	ret = ivpu_pll_enable(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to enable PLL: %d\n", ret);
+		return ret;
+	}
+
+	ret = ivpu_boot_host_ss_configure(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to configure host SS: %d\n", ret);
+		return ret;
+	}
+
+	/*
+	 * The control circuitry for vpu_idle indication logic powers up active.
+	 * To ensure unnecessary low power mode signal from LRT during bring up,
+	 * KMD disables the circuitry prior to bringing up the Main Power island.
+	 */
+	ivpu_boot_vpu_idle_gen_disable(vdev);
+
+	ret = ivpu_boot_pwr_domain_enable(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to enable power domain: %d\n", ret);
+		return ret;
+	}
+
+	ret = ivpu_boot_host_ss_axi_enable(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to enable AXI: %d\n", ret);
+		return ret;
+	}
+
+	ret = ivpu_boot_host_ss_top_noc_enable(vdev);
+	if (ret)
+		ivpu_err(vdev, "Failed to enable TOP NOC: %d\n", ret);
+
+	return ret;
+}
+
+static int ivpu_hw_mtl_boot_fw(struct ivpu_device *vdev)
+{
+	ivpu_boot_no_snoop_enable(vdev);
+	ivpu_boot_tbu_mmu_enable(vdev);
+	ivpu_boot_soc_cpu_boot(vdev);
+
+	return 0;
+}
+
+static bool ivpu_hw_mtl_is_idle(struct ivpu_device *vdev)
+{
+	u32 val;
+
+	if (IVPU_WA(punit_disabled))
+		return true;
+
+	val = REGB_RD32(MTL_BUTTRESS_VPU_STATUS);
+	return REG_TEST_FLD(MTL_BUTTRESS_VPU_STATUS, READY, val) &&
+	       REG_TEST_FLD(MTL_BUTTRESS_VPU_STATUS, IDLE, val);
+}
+
+static int ivpu_hw_mtl_power_down(struct ivpu_device *vdev)
+{
+	int ret = 0;
+
+	/* FPGA requires manual clearing of IP_Reset bit by enabling quiescent state */
+	if (ivpu_is_fpga(vdev)) {
+		if (ivpu_boot_host_ss_top_noc_disable(vdev)) {
+			ivpu_err(vdev, "Failed to disable TOP NOC\n");
+			ret = -EIO;
+		}
+
+		if (ivpu_boot_host_ss_axi_disable(vdev)) {
+			ivpu_err(vdev, "Failed to disable AXI\n");
+			ret = -EIO;
+		}
+	}
+
+	if (ivpu_boot_pwr_domain_disable(vdev)) {
+		ivpu_err(vdev, "Failed to disable power domain\n");
+		ret = -EIO;
+	}
+
+	if (ivpu_pll_disable(vdev)) {
+		ivpu_err(vdev, "Failed to disable PLL\n");
+		ret = -EIO;
+	}
+
+	if (ivpu_hw_mtl_d0i3_enable(vdev))
+		ivpu_warn(vdev, "Failed to enable D0I3\n");
+
+	return ret;
+}
+
+static void ivpu_hw_mtl_wdt_disable(struct ivpu_device *vdev)
+{
+	u32 val;
+
+	/* Enable writing and set non-zero WDT value */
+	REGV_WR32(MTL_VPU_CPU_SS_TIM_SAFE, TIM_SAFE_ENABLE);
+	REGV_WR32(MTL_VPU_CPU_SS_TIM_WATCHDOG, TIM_WATCHDOG_RESET_VALUE);
+
+	/* Enable writing and disable watchdog timer */
+	REGV_WR32(MTL_VPU_CPU_SS_TIM_SAFE, TIM_SAFE_ENABLE);
+	REGV_WR32(MTL_VPU_CPU_SS_TIM_WDOG_EN, 0);
+
+	/* Now clear the timeout interrupt */
+	val = REGV_RD32(MTL_VPU_CPU_SS_TIM_GEN_CONFIG);
+	val = REG_CLR_FLD(MTL_VPU_CPU_SS_TIM_GEN_CONFIG, WDOG_TO_INT_CLR, val);
+	REGV_WR32(MTL_VPU_CPU_SS_TIM_GEN_CONFIG, val);
+}
+
+/* Register indirect accesses */
+static u32 ivpu_hw_mtl_reg_pll_freq_get(struct ivpu_device *vdev)
+{
+	u32 pll_curr_ratio;
+
+	pll_curr_ratio = REGB_RD32(MTL_BUTTRESS_CURRENT_PLL);
+	pll_curr_ratio &= MTL_BUTTRESS_CURRENT_PLL_RATIO_MASK;
+
+	if (!ivpu_is_silicon(vdev))
+		return PLL_SIMULATION_FREQ;
+
+	return PLL_RATIO_TO_FREQ(pll_curr_ratio);
+}
+
+static u32 ivpu_hw_mtl_reg_telemetry_offset_get(struct ivpu_device *vdev)
+{
+	return REGB_RD32(MTL_BUTTRESS_VPU_TELEMETRY_OFFSET);
+}
+
+static u32 ivpu_hw_mtl_reg_telemetry_size_get(struct ivpu_device *vdev)
+{
+	return REGB_RD32(MTL_BUTTRESS_VPU_TELEMETRY_SIZE);
+}
+
+static u32 ivpu_hw_mtl_reg_telemetry_enable_get(struct ivpu_device *vdev)
+{
+	return REGB_RD32(MTL_BUTTRESS_VPU_TELEMETRY_ENABLE);
+}
+
+static void ivpu_hw_mtl_reg_db_set(struct ivpu_device *vdev, u32 db_id)
+{
+	u32 reg_stride = MTL_VPU_CPU_SS_DOORBELL_1 - MTL_VPU_CPU_SS_DOORBELL_0;
+	u32 val = BIT(MTL_VPU_CPU_SS_DOORBELL_0_SET_OFFSET);
+
+	REGV_WR32I(MTL_VPU_CPU_SS_DOORBELL_0, reg_stride, db_id, val);
+}
+
+static u32 ivpu_hw_mtl_reg_ipc_rx_addr_get(struct ivpu_device *vdev)
+{
+	return REGV_RD32(MTL_VPU_HOST_SS_TIM_IPC_FIFO_ATM);
+}
+
+static u32 ivpu_hw_mtl_reg_ipc_rx_count_get(struct ivpu_device *vdev)
+{
+	u32 count = REGV_RD32_SILENT(MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT);
+
+	return REG_GET_FLD(MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT, FILL_LEVEL, count);
+}
+
+static void ivpu_hw_mtl_reg_ipc_tx_set(struct ivpu_device *vdev, u32 vpu_addr)
+{
+	REGV_WR32(MTL_VPU_CPU_SS_TIM_IPC_FIFO, vpu_addr);
+}
+
+static void ivpu_hw_mtl_irq_clear(struct ivpu_device *vdev)
+{
+	REGV_WR64(MTL_VPU_HOST_SS_ICB_CLEAR_0, ICB_0_1_IRQ_MASK);
+}
+
+static void ivpu_hw_mtl_irq_enable(struct ivpu_device *vdev)
+{
+	REGV_WR32(MTL_VPU_HOST_SS_FW_SOC_IRQ_EN, ITF_FIREWALL_VIOLATION_MASK);
+	REGV_WR64(MTL_VPU_HOST_SS_ICB_ENABLE_0, ICB_0_1_IRQ_MASK);
+	REGB_WR32(MTL_BUTTRESS_LOCAL_INT_MASK, (u32)~(BIT(1) | BIT(2)));
+	REGB_WR32(MTL_BUTTRESS_GLOBAL_INT_MASK, 0x0);
+}
+
+static void ivpu_hw_mtl_irq_disable(struct ivpu_device *vdev)
+{
+	REGB_WR32(MTL_BUTTRESS_GLOBAL_INT_MASK, 0x1);
+	REGB_WR32(MTL_BUTTRESS_LOCAL_INT_MASK, BUTTRESS_IRQ_MASK);
+	REGV_WR64(MTL_VPU_HOST_SS_ICB_ENABLE_0, 0x0ull);
+	REGB_WR32(MTL_VPU_HOST_SS_FW_SOC_IRQ_EN, 0x0ul);
+}
+
+static irqreturn_t ivpu_hw_mtl_irq_wdt_nce_handler(struct ivpu_device *vdev)
+{
+	ivpu_err_ratelimited(vdev, "WDT NCE irq\n");
+
+	return IRQ_HANDLED;
+}
+
+static irqreturn_t ivpu_hw_mtl_irq_wdt_mss_handler(struct ivpu_device *vdev)
+{
+	ivpu_err_ratelimited(vdev, "WDT MSS irq\n");
+
+	ivpu_hw_wdt_disable(vdev);
+
+	return IRQ_HANDLED;
+}
+
+static irqreturn_t ivpu_hw_mtl_irq_noc_firewall_handler(struct ivpu_device *vdev)
+{
+	ivpu_err_ratelimited(vdev, "NOC Firewall irq\n");
+
+	return IRQ_HANDLED;
+}
+
+/* Handler for IRQs from VPU core (irqV) */
+static irqreturn_t ivpu_hw_mtl_irqv_handler(struct ivpu_device *vdev, int irq)
+{
+	irqreturn_t ret = IRQ_HANDLED;
+	u32 status = REGV_RD32(MTL_VPU_HOST_SS_ICB_STATUS_0) & ICB_0_IRQ_MASK;
+
+	REGV_WR32(MTL_VPU_HOST_SS_ICB_CLEAR_0, status);
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, CPU_INT_REDIRECT_0_INT, status))
+		ret &= ivpu_hw_mtl_irq_wdt_mss_handler(vdev);
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, CPU_INT_REDIRECT_1_INT, status))
+		ret &= ivpu_hw_mtl_irq_wdt_nce_handler(vdev);
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, NOC_FIREWALL_INT, status))
+		ret &= ivpu_hw_mtl_irq_noc_firewall_handler(vdev);
+
+	return ret;
+}
+
+/* Handler for IRQs from Buttress core (irqB) */
+static irqreturn_t ivpu_hw_mtl_irqb_handler(struct ivpu_device *vdev, int irq)
+{
+	u32 status = REGB_RD32(MTL_BUTTRESS_INTERRUPT_STAT) & BUTTRESS_IRQ_MASK;
+
+	REGB_WR32(MTL_BUTTRESS_INTERRUPT_STAT, status);
+
+	if (REG_TEST_FLD(MTL_BUTTRESS_INTERRUPT_STAT, FREQ_CHANGE, status))
+		ivpu_dbg(IRQ, "FREQ_CHANGE");
+
+	if (REG_TEST_FLD(MTL_BUTTRESS_INTERRUPT_STAT, ATS_ERR, status)) {
+		ivpu_dbg(IRQ, "ATS_ERR 0x%016llx", REGB_RD64(MTL_BUTTRESS_ATS_ERR_LOG_0));
+		REGB_WR32(MTL_BUTTRESS_ATS_ERR_CLEAR, 0x1);
+	}
+
+	if (REG_TEST_FLD(MTL_BUTTRESS_INTERRUPT_STAT, UFI_ERR, status)) {
+		ivpu_dbg(IRQ, "UFI_ERR 0x%08x", REGB_RD32(MTL_BUTTRESS_UFI_ERR_LOG));
+		REGB_WR32(MTL_BUTTRESS_UFI_ERR_CLEAR, 0x1);
+	}
+
+	return IRQ_HANDLED;
+}
+
+static irqreturn_t ivpu_hw_mtl_irq_handler(int irq, void *ptr)
+{
+	struct ivpu_device *vdev = ptr;
+	irqreturn_t ret_irqv;
+	irqreturn_t ret_irqb;
+
+	ivpu_hw_mtl_irq_disable(vdev);
+
+	ret_irqv = ivpu_hw_mtl_irqv_handler(vdev, irq);
+	ret_irqb = ivpu_hw_mtl_irqb_handler(vdev, irq);
+
+	ivpu_hw_mtl_irq_enable(vdev);
+
+	return ret_irqv & ret_irqb;
+}
+
+static void ivpu_hw_mtl_diagnose_tdr(struct ivpu_device *vdev)
+{
+	u32 status = REGV_RD32(MTL_VPU_HOST_SS_ICB_STATUS_0) & ICB_0_IRQ_MASK;
+
+	if (ivpu_hw_mtl_reg_ipc_rx_count_get(vdev))
+		ivpu_err(vdev, "IPC FIFO queue not empty, missed IPC IRQ");
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, CPU_INT_REDIRECT_0_INT, status))
+		ivpu_err(vdev, "WDT MSS timeout detected\n");
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, CPU_INT_REDIRECT_1_INT, status))
+		ivpu_err(vdev, "WDT NCE timeout detected\n");
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, NOC_FIREWALL_INT, status))
+		ivpu_err(vdev, "NOC Firewall irq detected\n");
+}
+
+const struct ivpu_hw_ops ivpu_hw_mtl_ops = {
+	.info_init = ivpu_hw_mtl_info_init,
+	.power_up = ivpu_hw_mtl_power_up,
+	.is_idle = ivpu_hw_mtl_is_idle,
+	.power_down = ivpu_hw_mtl_power_down,
+	.boot_fw = ivpu_hw_mtl_boot_fw,
+	.wdt_disable = ivpu_hw_mtl_wdt_disable,
+	.diagnose_tdr = ivpu_hw_mtl_diagnose_tdr,
+	.reg_pll_freq_get = ivpu_hw_mtl_reg_pll_freq_get,
+	.reg_telemetry_offset_get = ivpu_hw_mtl_reg_telemetry_offset_get,
+	.reg_telemetry_size_get = ivpu_hw_mtl_reg_telemetry_size_get,
+	.reg_telemetry_enable_get = ivpu_hw_mtl_reg_telemetry_enable_get,
+	.reg_db_set = ivpu_hw_mtl_reg_db_set,
+	.reg_ipc_rx_addr_get = ivpu_hw_mtl_reg_ipc_rx_addr_get,
+	.reg_ipc_rx_count_get = ivpu_hw_mtl_reg_ipc_rx_count_get,
+	.reg_ipc_tx_set = ivpu_hw_mtl_reg_ipc_tx_set,
+	.irq_clear = ivpu_hw_mtl_irq_clear,
+	.irq_enable = ivpu_hw_mtl_irq_enable,
+	.irq_disable = ivpu_hw_mtl_irq_disable,
+	.irq_handler = ivpu_hw_mtl_irq_handler,
+};
drivers/gpu/drm/ivpu/ivpu_hw_mtl_reg.h new file +468 -0
new file mode 100644
index 000000000000..2d591d0d3f15
@@ -0,0 +1,468 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#ifndef __IVPU_HW_MTL_REG_H__
+#define __IVPU_HW_MTL_REG_H__
+
+#define MTL_BUTTRESS_INTERRUPT_TYPE					0x00000000u
+
+#define MTL_BUTTRESS_INTERRUPT_STAT					0x00000004u
+#define MTL_BUTTRESS_INTERRUPT_STAT_FREQ_CHANGE_SHIFT			0u
+#define MTL_BUTTRESS_INTERRUPT_STAT_FREQ_CHANGE_MASK			0x00000001u
+#define MTL_BUTTRESS_INTERRUPT_STAT_ATS_ERR_SHIFT			1u
+#define MTL_BUTTRESS_INTERRUPT_STAT_ATS_ERR_MASK			0x00000002u
+#define MTL_BUTTRESS_INTERRUPT_STAT_UFI_ERR_SHIFT			2u
+#define MTL_BUTTRESS_INTERRUPT_STAT_UFI_ERR_MASK			0x00000004u
+
+#define MTL_BUTTRESS_WP_REQ_PAYLOAD0					0x00000008u
+#define MTL_BUTTRESS_WP_REQ_PAYLOAD0_MIN_RATIO_SHIFT			0u
+#define MTL_BUTTRESS_WP_REQ_PAYLOAD0_MIN_RATIO_MASK			0x0000ffffu
+#define MTL_BUTTRESS_WP_REQ_PAYLOAD0_MAX_RATIO_SHIFT			16u
+#define MTL_BUTTRESS_WP_REQ_PAYLOAD0_MAX_RATIO_MASK			0xffff0000u
+
+#define MTL_BUTTRESS_WP_REQ_PAYLOAD1					0x0000000cu
+#define MTL_BUTTRESS_WP_REQ_PAYLOAD1_TARGET_RATIO_SHIFT			0u
+#define MTL_BUTTRESS_WP_REQ_PAYLOAD1_TARGET_RATIO_MASK			0x0000ffffu
+#define MTL_BUTTRESS_WP_REQ_PAYLOAD1_EPP_SHIFT				16u
+#define MTL_BUTTRESS_WP_REQ_PAYLOAD1_EPP_MASK				0xffff0000u
+
+#define MTL_BUTTRESS_WP_REQ_PAYLOAD2					0x00000010u
+#define MTL_BUTTRESS_WP_REQ_PAYLOAD2_CONFIG_SHIFT			0u
+#define MTL_BUTTRESS_WP_REQ_PAYLOAD2_CONFIG_MASK			0x0000ffffu
+
+#define MTL_BUTTRESS_WP_REQ_CMD						0x00000014u
+#define MTL_BUTTRESS_WP_REQ_CMD_SEND_SHIFT				0u
+#define MTL_BUTTRESS_WP_REQ_CMD_SEND_MASK				0x00000001u
+
+#define MTL_BUTTRESS_WP_DOWNLOAD					0x00000018u
+#define MTL_BUTTRESS_WP_DOWNLOAD_TARGET_RATIO_SHIFT			0u
+#define MTL_BUTTRESS_WP_DOWNLOAD_TARGET_RATIO_MASK			0x0000ffffu
+
+#define MTL_BUTTRESS_CURRENT_PLL					0x0000001cu
+#define MTL_BUTTRESS_CURRENT_PLL_RATIO_SHIFT				0u
+#define MTL_BUTTRESS_CURRENT_PLL_RATIO_MASK				0x0000ffffu
+
+#define MTL_BUTTRESS_PLL_ENABLE						0x00000020u
+
+#define MTL_BUTTRESS_FMIN_FUSE						0x00000024u
+#define MTL_BUTTRESS_FMIN_FUSE_RATIO_SHIFT				0u
+#define MTL_BUTTRESS_FMIN_FUSE_RATIO_MASK				0x0000ffffu
+
+#define MTL_BUTTRESS_FMAX_FUSE						0x00000028u
+#define MTL_BUTTRESS_FMAX_FUSE_RATIO_SHIFT				0u
+#define MTL_BUTTRESS_FMAX_FUSE_RATIO_MASK				0x0000ffffu
+
+#define MTL_BUTTRESS_TILE_FUSE						0x0000002cu
+#define MTL_BUTTRESS_TILE_FUSE_VALID_SHIFT				0u
+#define MTL_BUTTRESS_TILE_FUSE_VALID_MASK				0x00000001u
+#define MTL_BUTTRESS_TILE_FUSE_SKU_SHIFT				1u
+#define MTL_BUTTRESS_TILE_FUSE_SKU_MASK					0x00000006u
+
+#define MTL_BUTTRESS_LOCAL_INT_MASK					0x00000030u
+#define MTL_BUTTRESS_GLOBAL_INT_MASK					0x00000034u
+
+#define MTL_BUTTRESS_PLL_STATUS						0x00000040u
+#define MTL_BUTTRESS_PLL_STATUS_LOCK_SHIFT				1u
+#define MTL_BUTTRESS_PLL_STATUS_LOCK_MASK				0x00000002u
+
+#define MTL_BUTTRESS_VPU_STATUS						0x00000044u
+#define MTL_BUTTRESS_VPU_STATUS_READY_SHIFT				0u
+#define MTL_BUTTRESS_VPU_STATUS_READY_MASK				0x00000001u
+#define MTL_BUTTRESS_VPU_STATUS_IDLE_SHIFT				1u
+#define MTL_BUTTRESS_VPU_STATUS_IDLE_MASK				0x00000002u
+
+#define MTL_BUTTRESS_VPU_D0I3_CONTROL					0x00000060u
+#define MTL_BUTTRESS_VPU_D0I3_CONTROL_INPROGRESS_SHIFT			0u
+#define MTL_BUTTRESS_VPU_D0I3_CONTROL_INPROGRESS_MASK			0x00000001u
+#define MTL_BUTTRESS_VPU_D0I3_CONTROL_I3_SHIFT				2u
+#define MTL_BUTTRESS_VPU_D0I3_CONTROL_I3_MASK				0x00000004u
+
+#define MTL_BUTTRESS_VPU_IP_RESET					0x00000050u
+#define MTL_BUTTRESS_VPU_IP_RESET_TRIGGER_SHIFT				0u
+#define MTL_BUTTRESS_VPU_IP_RESET_TRIGGER_MASK				0x00000001u
+
+#define MTL_BUTTRESS_VPU_TELEMETRY_OFFSET				0x00000080u
+#define MTL_BUTTRESS_VPU_TELEMETRY_SIZE					0x00000084u
+#define MTL_BUTTRESS_VPU_TELEMETRY_ENABLE				0x00000088u
+
+#define MTL_BUTTRESS_ATS_ERR_LOG_0					0x000000a0u
+#define MTL_BUTTRESS_ATS_ERR_LOG_1					0x000000a4u
+#define MTL_BUTTRESS_ATS_ERR_CLEAR					0x000000a8u
+#define MTL_BUTTRESS_UFI_ERR_LOG					0x000000b0u
+#define MTL_BUTTRESS_UFI_ERR_CLEAR					0x000000b4u
+
+#define MTL_VPU_HOST_SS_CPR_CLK_SET					0x00000084u
+#define MTL_VPU_HOST_SS_CPR_CLK_SET_TOP_NOC_SHIFT			1u
+#define MTL_VPU_HOST_SS_CPR_CLK_SET_TOP_NOC_MASK			0x00000002u
+#define MTL_VPU_HOST_SS_CPR_CLK_SET_DSS_MAS_SHIFT			10u
+#define MTL_VPU_HOST_SS_CPR_CLK_SET_DSS_MAS_MASK			0x00000400u
+#define MTL_VPU_HOST_SS_CPR_CLK_SET_MSS_MAS_SHIFT			11u
+#define MTL_VPU_HOST_SS_CPR_CLK_SET_MSS_MAS_MASK			0x00000800u
+
+#define MTL_VPU_HOST_SS_CPR_RST_SET					0x00000094u
+#define MTL_VPU_HOST_SS_CPR_RST_SET_TOP_NOC_SHIFT			1u
+#define MTL_VPU_HOST_SS_CPR_RST_SET_TOP_NOC_MASK			0x00000002u
+#define MTL_VPU_HOST_SS_CPR_RST_SET_DSS_MAS_SHIFT			10u
+#define MTL_VPU_HOST_SS_CPR_RST_SET_DSS_MAS_MASK			0x00000400u
+#define MTL_VPU_HOST_SS_CPR_RST_SET_MSS_MAS_SHIFT			11u
+#define MTL_VPU_HOST_SS_CPR_RST_SET_MSS_MAS_MASK			0x00000800u
+
+#define MTL_VPU_HOST_SS_CPR_RST_CLR					0x00000098u
+#define MTL_VPU_HOST_SS_CPR_RST_CLR_TOP_NOC_SHIFT			1u
+#define MTL_VPU_HOST_SS_CPR_RST_CLR_TOP_NOC_MASK			0x00000002u
+#define MTL_VPU_HOST_SS_CPR_RST_CLR_DSS_MAS_SHIFT			10u
+#define MTL_VPU_HOST_SS_CPR_RST_CLR_DSS_MAS_MASK			0x00000400u
+#define MTL_VPU_HOST_SS_CPR_RST_CLR_MSS_MAS_SHIFT			11u
+#define MTL_VPU_HOST_SS_CPR_RST_CLR_MSS_MAS_MASK			0x00000800u
+
+#define MTL_VPU_HOST_SS_HW_VERSION					0x00000108u
+#define MTL_VPU_HOST_SS_HW_VERSION_SOC_REVISION_SHIFT			0u
+#define MTL_VPU_HOST_SS_HW_VERSION_SOC_REVISION_MASK			0x000000ffu
+#define MTL_VPU_HOST_SS_HW_VERSION_SOC_NUMBER_SHIFT			8u
+#define MTL_VPU_HOST_SS_HW_VERSION_SOC_NUMBER_MASK			0x0000ff00u
+#define MTL_VPU_HOST_SS_HW_VERSION_VPU_GENERATION_SHIFT			16u
+#define MTL_VPU_HOST_SS_HW_VERSION_VPU_GENERATION_MASK			0x00ff0000u
+
+#define MTL_VPU_HOST_SS_SW_VERSION					0x0000010cu
+
+#define MTL_VPU_HOST_SS_GEN_CTRL					0x00000118u
+#define MTL_VPU_HOST_SS_GEN_CTRL_PS_SHIFT				0x0000001du
+#define MTL_VPU_HOST_SS_GEN_CTRL_PS_MASK				0xe0000000u
+
+#define MTL_VPU_HOST_SS_NOC_QREQN					0x00000154u
+#define MTL_VPU_HOST_SS_NOC_QREQN_TOP_SOCMMIO_SHIFT			0u
+#define MTL_VPU_HOST_SS_NOC_QREQN_TOP_SOCMMIO_MASK			0x00000001u
+
+#define MTL_VPU_HOST_SS_NOC_QACCEPTN					0x00000158u
+#define MTL_VPU_HOST_SS_NOC_QACCEPTN_TOP_SOCMMIO_SHIFT			0u
+#define MTL_VPU_HOST_SS_NOC_QACCEPTN_TOP_SOCMMIO_MASK			0x00000001u
+
+#define MTL_VPU_HOST_SS_NOC_QDENY					0x0000015cu
+#define MTL_VPU_HOST_SS_NOC_QDENY_TOP_SOCMMIO_SHIFT			0u
+#define MTL_VPU_HOST_SS_NOC_QDENY_TOP_SOCMMIO_MASK			0x00000001u
+
+#define MTL_VPU_TOP_NOC_QREQN						0x00000160u
+#define MTL_VPU_TOP_NOC_QREQN_CPU_CTRL_SHIFT				0u
+#define MTL_VPU_TOP_NOC_QREQN_CPU_CTRL_MASK				0x00000001u
+#define MTL_VPU_TOP_NOC_QREQN_HOSTIF_L2CACHE_SHIFT			1u
+#define MTL_VPU_TOP_NOC_QREQN_HOSTIF_L2CACHE_MASK			0x00000002u
+
+#define MTL_VPU_TOP_NOC_QACCEPTN					0x00000164u
+#define MTL_VPU_TOP_NOC_QACCEPTN_CPU_CTRL_SHIFT				0u
+#define MTL_VPU_TOP_NOC_QACCEPTN_CPU_CTRL_MASK				0x00000001u
+#define MTL_VPU_TOP_NOC_QACCEPTN_HOSTIF_L2CACHE_SHIFT			1u
+#define MTL_VPU_TOP_NOC_QACCEPTN_HOSTIF_L2CACHE_MASK			0x00000002u
+
+#define MTL_VPU_TOP_NOC_QDENY						0x00000168u
+#define MTL_VPU_TOP_NOC_QDENY_CPU_CTRL_SHIFT				0u
+#define MTL_VPU_TOP_NOC_QDENY_CPU_CTRL_MASK				0x00000001u
+#define MTL_VPU_TOP_NOC_QDENY_HOSTIF_L2CACHE_SHIFT			1u
+#define MTL_VPU_TOP_NOC_QDENY_HOSTIF_L2CACHE_MASK			0x00000002u
+
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN					0x00000170u
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN_CSS_ROM_CMX_SHIFT			0u
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN_CSS_ROM_CMX_MASK			0x00000001u
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN_CSS_DBG_SHIFT			1u
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN_CSS_DBG_MASK			0x00000002u
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN_CSS_CTRL_SHIFT			2u
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN_CSS_CTRL_MASK			0x00000004u
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN_DEC400_SHIFT			3u
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN_DEC400_MASK			0x00000008u
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN_MSS_NCE_SHIFT			4u
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN_MSS_NCE_MASK			0x00000010u
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN_MSS_MBI_SHIFT			5u
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN_MSS_MBI_MASK			0x00000020u
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN_MSS_MBI_CMX_SHIFT			6u
+#define MTL_VPU_HOST_SS_FW_SOC_IRQ_EN_MSS_MBI_CMX_MASK			0x00000040u
+
+#define MTL_VPU_HOST_SS_ICB_STATUS_0					0x00010210u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_TIMER_0_INT_SHIFT			0u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_TIMER_0_INT_MASK			0x00000001u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_TIMER_1_INT_SHIFT			1u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_TIMER_1_INT_MASK			0x00000002u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_TIMER_2_INT_SHIFT			2u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_TIMER_2_INT_MASK			0x00000004u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_TIMER_3_INT_SHIFT			3u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_TIMER_3_INT_MASK			0x00000008u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_HOST_IPC_FIFO_INT_SHIFT		4u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_HOST_IPC_FIFO_INT_MASK		0x00000010u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_MMU_IRQ_0_INT_SHIFT		5u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_MMU_IRQ_0_INT_MASK			0x00000020u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_MMU_IRQ_1_INT_SHIFT		6u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_MMU_IRQ_1_INT_MASK			0x00000040u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_MMU_IRQ_2_INT_SHIFT		7u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_MMU_IRQ_2_INT_MASK			0x00000080u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_NOC_FIREWALL_INT_SHIFT		8u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_NOC_FIREWALL_INT_MASK		0x00000100u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_CPU_INT_REDIRECT_0_INT_SHIFT	30u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_CPU_INT_REDIRECT_0_INT_MASK	0x40000000u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_CPU_INT_REDIRECT_1_INT_SHIFT	31u
+#define MTL_VPU_HOST_SS_ICB_STATUS_0_CPU_INT_REDIRECT_1_INT_MASK	0x80000000u
+
+#define MTL_VPU_HOST_SS_ICB_STATUS_1					0x00010214u
+#define MTL_VPU_HOST_SS_ICB_STATUS_1_CPU_INT_REDIRECT_2_INT_SHIFT	0u
+#define MTL_VPU_HOST_SS_ICB_STATUS_1_CPU_INT_REDIRECT_2_INT_MASK	0x00000001u
+#define MTL_VPU_HOST_SS_ICB_STATUS_1_CPU_INT_REDIRECT_3_INT_SHIFT	1u
+#define MTL_VPU_HOST_SS_ICB_STATUS_1_CPU_INT_REDIRECT_3_INT_MASK	0x00000002u
+#define MTL_VPU_HOST_SS_ICB_STATUS_1_CPU_INT_REDIRECT_4_INT_SHIFT	2u
+#define MTL_VPU_HOST_SS_ICB_STATUS_1_CPU_INT_REDIRECT_4_INT_MASK	0x00000004u
+
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0					0x00010220u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_TIMER_0_INT_SHIFT			0u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_TIMER_0_INT_MASK			0x00000001u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_TIMER_1_INT_SHIFT			1u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_TIMER_1_INT_MASK			0x00000002u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_TIMER_2_INT_SHIFT			2u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_TIMER_2_INT_MASK			0x00000004u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_TIMER_3_INT_SHIFT			3u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_TIMER_3_INT_MASK			0x00000008u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_HOST_IPC_FIFO_INT_SHIFT		4u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_HOST_IPC_FIFO_INT_MASK		0x00000010u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_MMU_IRQ_0_INT_SHIFT			5u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_MMU_IRQ_0_INT_MASK			0x00000020u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_MMU_IRQ_1_INT_SHIFT			6u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_MMU_IRQ_1_INT_MASK			0x00000040u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_MMU_IRQ_2_INT_SHIFT			7u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_MMU_IRQ_2_INT_MASK			0x00000080u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_NOC_FIREWALL_INT_SHIFT		8u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_NOC_FIREWALL_INT_MASK		0x00000100u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_CPU_INT_REDIRECT_0_INT_SHIFT	30u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_CPU_INT_REDIRECT_0_INT_MASK		0x40000000u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_CPU_INT_REDIRECT_1_INT_SHIFT	31u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_0_CPU_INT_REDIRECT_1_INT_MASK		0x80000000u
+
+#define MTL_VPU_HOST_SS_ICB_CLEAR_1					0x00010224u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_1_CPU_INT_REDIRECT_2_INT_SHIFT	0u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_1_CPU_INT_REDIRECT_2_INT_MASK		0x00000001u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_1_CPU_INT_REDIRECT_3_INT_SHIFT	1u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_1_CPU_INT_REDIRECT_3_INT_MASK		0x00000002u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_1_CPU_INT_REDIRECT_4_INT_SHIFT	2u
+#define MTL_VPU_HOST_SS_ICB_CLEAR_1_CPU_INT_REDIRECT_4_INT_MASK		0x00000004u
+
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0					0x00010240u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_TIMER_0_INT_SHIFT			0u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_TIMER_0_INT_MASK			0x00000001u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_TIMER_1_INT_SHIFT			1u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_TIMER_1_INT_MASK			0x00000002u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_TIMER_2_INT_SHIFT			2u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_TIMER_2_INT_MASK			0x00000004u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_TIMER_3_INT_SHIFT			3u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_TIMER_3_INT_MASK			0x00000008u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_HOST_IPC_FIFO_INT_SHIFT		4u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_HOST_IPC_FIFO_INT_MASK		0x00000010u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_MMU_IRQ_0_INT_SHIFT		5u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_MMU_IRQ_0_INT_MASK			0x00000020u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_MMU_IRQ_1_INT_SHIFT		6u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_MMU_IRQ_1_INT_MASK			0x00000040u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_MMU_IRQ_2_INT_SHIFT		7u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_MMU_IRQ_2_INT_MASK			0x00000080u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_NOC_FIREWALL_INT_SHIFT		8u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_NOC_FIREWALL_INT_MASK		0x00000100u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_CPU_INT_REDIRECT_0_INT_SHIFT	30u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_CPU_INT_REDIRECT_0_INT_MASK	0x40000000u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_CPU_INT_REDIRECT_1_INT_SHIFT	31u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_0_CPU_INT_REDIRECT_1_INT_MASK	0x80000000u
+
+#define MTL_VPU_HOST_SS_ICB_ENABLE_1					0x00010244u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_1_CPU_INT_REDIRECT_2_INT_SHIFT	0u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_1_CPU_INT_REDIRECT_2_INT_MASK	0x00000001u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_1_CPU_INT_REDIRECT_3_INT_SHIFT	1u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_1_CPU_INT_REDIRECT_3_INT_MASK	0x00000002u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_1_CPU_INT_REDIRECT_4_INT_SHIFT	2u
+#define MTL_VPU_HOST_SS_ICB_ENABLE_1_CPU_INT_REDIRECT_4_INT_MASK	0x00000004u
+
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_ATM				0x000200f4u
+
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT				0x000200fcu
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT_DEFAULT			0x00000000u
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT_READ_POINTER_SHIFT		0u
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT_READ_POINTER_MASK		0x000000ffu
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT_READ_POINTER_EFAULT		0x00000000u
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT_WRITE_POINTER_SHIFT		8u
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT_WRITE_POINTER_MASK		0x0000ff00u
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT_WRITE_POINTER_DEFAULT		0x00000000u
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT_FILL_LEVEL_SHIFT		16u
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT_FILL_LEVEL_MASK		0x00ff0000u
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT_FILL_LEVEL_DEFAULT		0x00000000u
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT_RSVD0_SHIFT			24u
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT_RSVD0_MASK			0xff000000u
+#define MTL_VPU_HOST_SS_TIM_IPC_FIFO_STAT_RSVD0_DEFAULT			0x00000000u
+
+#define MTL_VPU_HOST_SS_AON_PWR_ISO_EN0					0x00030020u
+#define MTL_VPU_HOST_SS_AON_PWR_ISO_EN0_MSS_CPU_SHIFT			3u
+#define MTL_VPU_HOST_SS_AON_PWR_ISO_EN0_MSS_CPU_MASK			0x00000008u
+
+#define MTL_VPU_HOST_SS_AON_PWR_ISLAND_EN0				0x00030024u
+#define MTL_VPU_HOST_SS_AON_PWR_ISLAND_EN0_MSS_CPU_SHIFT		3u
+#define MTL_VPU_HOST_SS_AON_PWR_ISLAND_EN0_MSS_CPU_MASK			0x00000008u
+
+#define MTL_VPU_HOST_SS_AON_PWR_ISLAND_TRICKLE_EN0			0x00030028u
+#define MTL_VPU_HOST_SS_AON_PWR_ISLAND_TRICKLE_EN0_MSS_CPU_SHIFT	3u
+#define MTL_VPU_HOST_SS_AON_PWR_ISLAND_TRICKLE_EN0_MSS_CPU_MASK		0x00000008u
+
+#define MTL_VPU_HOST_SS_AON_PWR_ISLAND_STATUS0				0x0003002cu
+#define MTL_VPU_HOST_SS_AON_PWR_ISLAND_STATUS0_MSS_CPU_SHIFT		3u
+#define MTL_VPU_HOST_SS_AON_PWR_ISLAND_STATUS0_MSS_CPU_MASK		0x00000008u
+
+#define MTL_VPU_HOST_SS_AON_VPU_IDLE_GEN				0x00030200u
+#define MTL_VPU_HOST_SS_AON_VPU_IDLE_GEN_EN_SHIFT			0u
+#define MTL_VPU_HOST_SS_AON_VPU_IDLE_GEN_EN_MASK			0x00000001u
+
+#define MTL_VPU_HOST_SS_AON_DPU_ACTIVE					0x00030204u
+#define MTL_VPU_HOST_SS_AON_DPU_ACTIVE_DPU_ACTIVE_SHIFT			0u
+#define MTL_VPU_HOST_SS_AON_DPU_ACTIVE_DPU_ACTIVE_MASK			0x00000001u
+
+#define MTL_VPU_HOST_SS_LOADING_ADDRESS_LO				0x00041040u
+#define MTL_VPU_HOST_SS_LOADING_ADDRESS_LO_DONE_SHIFT			0u
+#define MTL_VPU_HOST_SS_LOADING_ADDRESS_LO_DONE_MASK			0x00000001u
+#define MTL_VPU_HOST_SS_LOADING_ADDRESS_LO_IOSF_RS_ID_SHIFT		1u
+#define MTL_VPU_HOST_SS_LOADING_ADDRESS_LO_IOSF_RS_ID_MASK		0x00000006u
+#define MTL_VPU_HOST_SS_LOADING_ADDRESS_LO_IMAGE_LOCATION_SHIFT		3u
+#define MTL_VPU_HOST_SS_LOADING_ADDRESS_LO_IMAGE_LOCATION_MASK		0xfffffff8u
+
+#define MTL_VPU_HOST_SS_WORKPOINT_CONFIG_MIRROR				0x00082020u
+#define MTL_VPU_HOST_SS_WORKPOINT_CONFIG_MIRROR_FINAL_PLL_FREQ_SHIFT	0u
+#define MTL_VPU_HOST_SS_WORKPOINT_CONFIG_MIRROR_FINAL_PLL_FREQ_MASK	0x0000ffffu
+#define MTL_VPU_HOST_SS_WORKPOINT_CONFIG_MIRROR_CONFIG_ID_SHIFT		16u
+#define MTL_VPU_HOST_SS_WORKPOINT_CONFIG_MIRROR_CONFIG_ID_MASK		0xffff0000u
+
+#define MTL_VPU_HOST_MMU_IDR0						0x00200000u
+#define MTL_VPU_HOST_MMU_IDR1						0x00200004u
+#define MTL_VPU_HOST_MMU_IDR3						0x0020000cu
+#define MTL_VPU_HOST_MMU_IDR5						0x00200014u
+#define MTL_VPU_HOST_MMU_CR0						0x00200020u
+#define MTL_VPU_HOST_MMU_CR0ACK						0x00200024u
+#define MTL_VPU_HOST_MMU_CR1						0x00200028u
+#define MTL_VPU_HOST_MMU_CR2						0x0020002cu
+#define MTL_VPU_HOST_MMU_IRQ_CTRL					0x00200050u
+#define MTL_VPU_HOST_MMU_IRQ_CTRLACK					0x00200054u
+
+#define MTL_VPU_HOST_MMU_GERROR						0x00200060u
+#define MTL_VPU_HOST_MMU_GERROR_CMDQ_SHIFT				0u
+#define MTL_VPU_HOST_MMU_GERROR_CMDQ_MASK				0x00000001u
+#define MTL_VPU_HOST_MMU_GERROR_EVTQ_ABT_SHIFT				2u
+#define MTL_VPU_HOST_MMU_GERROR_EVTQ_ABT_MASK				0x00000004u
+#define MTL_VPU_HOST_MMU_GERROR_PRIQ_ABT_SHIFT				3u
+#define MTL_VPU_HOST_MMU_GERROR_PRIQ_ABT_MASK				0x00000008u
+#define MTL_VPU_HOST_MMU_GERROR_MSI_CMDQ_ABT_SHIFT			4u
+#define MTL_VPU_HOST_MMU_GERROR_MSI_CMDQ_ABT_MASK			0x00000010u
+#define MTL_VPU_HOST_MMU_GERROR_MSI_EVTQ_ABT_SHIFT			5u
+#define MTL_VPU_HOST_MMU_GERROR_MSI_EVTQ_ABT_MASK			0x00000020u
+#define MTL_VPU_HOST_MMU_GERROR_MSI_PRIQ_ABT_SHIFT			6u
+#define MTL_VPU_HOST_MMU_GERROR_MSI_PRIQ_ABT_MASK			0x00000040u
+#define MTL_VPU_HOST_MMU_GERROR_MSI_ABT_SHIFT				7u
+#define MTL_VPU_HOST_MMU_GERROR_MSI_ABT_MASK				0x00000080u
+#define MTL_VPU_HOST_MMU_GERROR_SFM_SHIFT				8u
+#define MTL_VPU_HOST_MMU_GERROR_SFM_MASK				0x00000100u
+#define MTL_VPU_HOST_MMU_GERRORN					0x00200064u
+
+#define MTL_VPU_HOST_MMU_STRTAB_BASE					0x00200080u
+#define MTL_VPU_HOST_MMU_STRTAB_BASE_CFG				0x00200088u
+#define MTL_VPU_HOST_MMU_CMDQ_BASE					0x00200090u
+#define MTL_VPU_HOST_MMU_CMDQ_PROD					0x00200098u
+#define MTL_VPU_HOST_MMU_CMDQ_CONS					0x0020009cu
+#define MTL_VPU_HOST_MMU_EVTQ_BASE					0x002000a0u
+#define MTL_VPU_HOST_MMU_EVTQ_PROD					0x002000a8u
+#define MTL_VPU_HOST_MMU_EVTQ_CONS					0x002000acu
+#define MTL_VPU_HOST_MMU_EVTQ_PROD_SEC					(0x002000a8u + SZ_64K)
+#define MTL_VPU_HOST_MMU_EVTQ_CONS_SEC					(0x002000acu + SZ_64K)
+
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES				0x00360000u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_CACHE_OVERRIDE_EN_SHIFT	0u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_CACHE_OVERRIDE_EN_MASK	0x00000001u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_AWCACHE_OVERRIDE_SHIFT	1u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_AWCACHE_OVERRIDE_MASK		0x00000002u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_ARCACHE_OVERRIDE_SHIFT	2u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_ARCACHE_OVERRIDE_MASK		0x00000004u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_NOSNOOP_OVERRIDE_EN_SHIFT	3u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_NOSNOOP_OVERRIDE_EN_MASK	0x00000008u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_AW_NOSNOOP_OVERRIDE_SHIFT	4u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_AW_NOSNOOP_OVERRIDE_MASK	0x00000010u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_AR_NOSNOOP_OVERRIDE_SHIFT	5u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_AR_NOSNOOP_OVERRIDE_MASK	0x00000020u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_PTW_AW_CONTEXT_FLAG_SHIFT	6u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_PTW_AW_CONTEXT_FLAG_MASK	0x000007C0u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_PTW_AR_CONTEXT_FLAG_SHIFT	11u
+#define MTL_VPU_HOST_IF_TCU_PTW_OVERRIDES_PTW_AR_CONTEXT_FLAG_MASK	0x0000f800u
+
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV					0x00360004u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU0_AWMMUSSIDV_SHIFT		0u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU0_AWMMUSSIDV_MASK		0x00000001u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU0_ARMMUSSIDV_SHIFT		1u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU0_ARMMUSSIDV_MASK		0x00000002u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU1_AWMMUSSIDV_SHIFT		2u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU1_AWMMUSSIDV_MASK		0x00000004u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU1_ARMMUSSIDV_SHIFT		3u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU1_ARMMUSSIDV_MASK		0x00000008u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU2_AWMMUSSIDV_SHIFT		4u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU2_AWMMUSSIDV_MASK		0x00000010u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU2_ARMMUSSIDV_SHIFT		5u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU2_ARMMUSSIDV_MASK		0x00000020u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU3_AWMMUSSIDV_SHIFT		6u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU3_AWMMUSSIDV_MASK		0x00000040u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU3_ARMMUSSIDV_SHIFT		7u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU3_ARMMUSSIDV_MASK		0x00000080u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU4_AWMMUSSIDV_SHIFT		8u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU4_AWMMUSSIDV_MASK		0x00000100u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU4_ARMMUSSIDV_SHIFT		9u
+#define MTL_VPU_HOST_IF_TBU_MMUSSIDV_TBU4_ARMMUSSIDV_MASK		0x00000200u
+
+#define MTL_VPU_CPU_SS_DSU_LEON_RT_BASE					0x04000000u
+#define MTL_VPU_CPU_SS_DSU_LEON_RT_DSU_CTRL				0x04000000u
+#define MTL_VPU_CPU_SS_DSU_LEON_RT_PC_REG				0x04400010u
+#define MTL_VPU_CPU_SS_DSU_LEON_RT_NPC_REG				0x04400014u
+#define MTL_VPU_CPU_SS_DSU_LEON_RT_DSU_TRAP_REG				0x04400020u
+
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_CLK_SET				0x06010004u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_CLK_SET_CPU_DSU_SHIFT			1u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_CLK_SET_CPU_DSU_MASK			0x00000002u
+
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_RST_CLR				0x06010018u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_RST_CLR_CPU_DSU_SHIFT			1u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_RST_CLR_CPU_DSU_MASK			0x00000002u
+
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC				0x06010040u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RSTRUN0_SHIFT	0u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RSTRUN0_MASK		0x00000001u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RESUME0_SHIFT	1u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RESUME0_MASK		0x00000002u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RSTRUN1_SHIFT	2u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RSTRUN1_MASK		0x00000004u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RESUME1_SHIFT	3u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RESUME1_MASK		0x00000008u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RSTVEC_SHIFT		4u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RSTVEC_MASK		0xfffffff0u
+
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC				0x06010040u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RSTRUN0_SHIFT	0u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RSTRUN0_MASK		0x00000001u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RESUME0_SHIFT	1u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RESUME0_MASK		0x00000002u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RSTRUN1_SHIFT	2u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RSTRUN1_MASK		0x00000004u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RESUME1_SHIFT	3u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RESUME1_MASK		0x00000008u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RSTVEC_SHIFT		4u
+#define MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC_IRQI_RSTVEC_MASK		0xfffffff0u
+
+#define MTL_VPU_CPU_SS_TIM_WATCHDOG					0x0602009cu
+#define MTL_VPU_CPU_SS_TIM_WDOG_EN					0x060200a4u
+#define MTL_VPU_CPU_SS_TIM_SAFE						0x060200a8u
+
+#define MTL_VPU_CPU_SS_TIM_IPC_FIFO					0x060200f0u
+#define MTL_VPU_CPU_SS_TIM_IPC_FIFO_OF_FLAG0				0x06020100u
+#define MTL_VPU_CPU_SS_TIM_IPC_FIFO_OF_FLAG1				0x06020104u
+
+#define MTL_VPU_CPU_SS_TIM_GEN_CONFIG					0x06021008u
+#define MTL_VPU_CPU_SS_TIM_GEN_CONFIG_WDOG_TO_INT_CLR_OFFSET		9u
+#define MTL_VPU_CPU_SS_TIM_GEN_CONFIG_WDOG_TO_INT_CLR_MASK		0x00000200u
+
+#define MTL_VPU_CPU_SS_DOORBELL_0					0x06300000u
+#define MTL_VPU_CPU_SS_DOORBELL_0_SET_OFFSET				0u
+#define MTL_VPU_CPU_SS_DOORBELL_0_SET_MASK				0x00000001u
+
+#define MTL_VPU_CPU_SS_DOORBELL_1					0x06301000u
+
+#endif /* __IVPU_HW_MTL_REG_H__ */
drivers/gpu/drm/ivpu/ivpu_hw_reg_io.h new file +115 -0
new file mode 100644
index 000000000000..c32d54cd65b6
@@ -0,0 +1,115 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#ifndef __IVPU_HW_REG_IO_H__
+#define __IVPU_HW_REG_IO_H__
+
+#include <linux/bitfield.h>
+#include <linux/io.h>
+#include <linux/iopoll.h>
+
+#include "ivpu_drv.h"
+
+#define REG_POLL_SLEEP_US 50
+#define REG_IO_ERROR      0xffffffff
+
+#define REGB_RD32(reg)          ivpu_hw_reg_rd32(vdev, vdev->regb, (reg), #reg, __func__)
+#define REGB_RD32_SILENT(reg)   readl(vdev->regb + (reg))
+#define REGB_RD64(reg)          ivpu_hw_reg_rd64(vdev, vdev->regb, (reg), #reg, __func__)
+#define REGB_WR32(reg, val)     ivpu_hw_reg_wr32(vdev, vdev->regb, (reg), (val), #reg, __func__)
+#define REGB_WR64(reg, val)     ivpu_hw_reg_wr64(vdev, vdev->regb, (reg), (val), #reg, __func__)
+
+#define REGV_RD32(reg)          ivpu_hw_reg_rd32(vdev, vdev->regv, (reg), #reg, __func__)
+#define REGV_RD32_SILENT(reg)   readl(vdev->regv + (reg))
+#define REGV_RD64(reg)          ivpu_hw_reg_rd64(vdev, vdev->regv, (reg), #reg, __func__)
+#define REGV_WR32(reg, val)     ivpu_hw_reg_wr32(vdev, vdev->regv, (reg), (val), #reg, __func__)
+#define REGV_WR64(reg, val)     ivpu_hw_reg_wr64(vdev, vdev->regv, (reg), (val), #reg, __func__)
+
+#define REGV_WR32I(reg, stride, index, val) \
+	ivpu_hw_reg_wr32_index(vdev, vdev->regv, (reg), (stride), (index), (val), #reg, __func__)
+
+#define REG_FLD(REG, FLD) \
+	(REG##_##FLD##_MASK)
+#define REG_FLD_NUM(REG, FLD, num) \
+	FIELD_PREP(REG##_##FLD##_MASK, num)
+#define REG_GET_FLD(REG, FLD, val) \
+	FIELD_GET(REG##_##FLD##_MASK, val)
+#define REG_CLR_FLD(REG, FLD, val) \
+	((val) & ~(REG##_##FLD##_MASK))
+#define REG_SET_FLD(REG, FLD, val) \
+	((val) | (REG##_##FLD##_MASK))
+#define REG_SET_FLD_NUM(REG, FLD, num, val) \
+	(((val) & ~(REG##_##FLD##_MASK)) | FIELD_PREP(REG##_##FLD##_MASK, num))
+#define REG_TEST_FLD(REG, FLD, val) \
+	((REG##_##FLD##_MASK) == ((val) & (REG##_##FLD##_MASK)))
+#define REG_TEST_FLD_NUM(REG, FLD, num, val) \
+	((num) == FIELD_GET(REG##_##FLD##_MASK, val))
+
+#define REGB_POLL(reg, var, cond, timeout_us) \
+	read_poll_timeout(REGB_RD32_SILENT, var, cond, REG_POLL_SLEEP_US, timeout_us, false, reg)
+
+#define REGV_POLL(reg, var, cond, timeout_us) \
+	read_poll_timeout(REGV_RD32_SILENT, var, cond, REG_POLL_SLEEP_US, timeout_us, false, reg)
+
+#define REGB_POLL_FLD(reg, fld, val, timeout_us) \
+({ \
+	u32 var; \
+	REGB_POLL(reg, var, (FIELD_GET(reg##_##fld##_MASK, var) == (val)), timeout_us); \
+})
+
+#define REGV_POLL_FLD(reg, fld, val, timeout_us) \
+({ \
+	u32 var; \
+	REGV_POLL(reg, var, (FIELD_GET(reg##_##fld##_MASK, var) == (val)), timeout_us); \
+})
+
+static inline u32
+ivpu_hw_reg_rd32(struct ivpu_device *vdev, void __iomem *base, u32 reg,
+		 const char *name, const char *func)
+{
+	u32 val = readl(base + reg);
+
+	ivpu_dbg(REG, "%s RD: %s (0x%08x) => 0x%08x\n", func, name, reg, val);
+	return val;
+}
+
+static inline u64
+ivpu_hw_reg_rd64(struct ivpu_device *vdev, void __iomem *base, u32 reg,
+		 const char *name, const char *func)
+{
+	u64 val = readq(base + reg);
+
+	ivpu_dbg(REG, "%s RD: %s (0x%08x) => 0x%016llx\n", func, name, reg, val);
+	return val;
+}
+
+static inline void
+ivpu_hw_reg_wr32(struct ivpu_device *vdev, void __iomem *base, u32 reg, u32 val,
+		 const char *name, const char *func)
+{
+	ivpu_dbg(REG, "%s WR: %s (0x%08x) <= 0x%08x\n", func, name, reg, val);
+	writel(val, base + reg);
+}
+
+static inline void
+ivpu_hw_reg_wr64(struct ivpu_device *vdev, void __iomem *base, u32 reg, u64 val,
+		 const char *name, const char *func)
+{
+	ivpu_dbg(REG, "%s WR: %s (0x%08x) <= 0x%016llx\n", func, name, reg, val);
+	writeq(val, base + reg);
+}
+
+static inline void
+ivpu_hw_reg_wr32_index(struct ivpu_device *vdev, void __iomem *base, u32 reg,
+		       u32 stride, u32 index, u32 val, const char *name,
+		      const char *func)
+{
+	reg += index * stride;
+
+	ivpu_dbg(REG, "%s WR: %s_%d (0x%08x) <= 0x%08x\n", func, name, index, reg, val);
+	writel(val, base + reg);
+}
+
+#endif /* __IVPU_HW_REG_IO_H__ */
include/uapi/drm/ivpu_drm.h new file +95 -0
new file mode 100644
index 000000000000..7a7dbe2f521b
@@ -0,0 +1,95 @@
+/* SPDX-License-Identifier: GPL-2.0-only WITH Linux-syscall-note */
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#ifndef __UAPI_IVPU_DRM_H__
+#define __UAPI_IVPU_DRM_H__
+
+#include "drm.h"
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+#define DRM_IVPU_DRIVER_MAJOR 1
+#define DRM_IVPU_DRIVER_MINOR 0
+
+#define DRM_IVPU_GET_PARAM		  0x00
+#define DRM_IVPU_SET_PARAM		  0x01
+
+#define DRM_IOCTL_IVPU_GET_PARAM                                               \
+	DRM_IOWR(DRM_COMMAND_BASE + DRM_IVPU_GET_PARAM, struct drm_ivpu_param)
+
+#define DRM_IOCTL_IVPU_SET_PARAM                                               \
+	DRM_IOW(DRM_COMMAND_BASE + DRM_IVPU_SET_PARAM, struct drm_ivpu_param)
+
+/**
+ * DOC: contexts
+ *
+ * VPU contexts have private virtual address space, job queues and priority.
+ * Each context is identified by an unique ID. Context is created on open().
+ */
+
+#define DRM_IVPU_PARAM_DEVICE_ID	    0
+#define DRM_IVPU_PARAM_DEVICE_REVISION	    1
+#define DRM_IVPU_PARAM_PLATFORM_TYPE	    2
+#define DRM_IVPU_PARAM_CORE_CLOCK_RATE	    3
+#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_PLATFORM_TYPE_SILICON	    0
+
+#define DRM_IVPU_CONTEXT_PRIORITY_IDLE	    0
+#define DRM_IVPU_CONTEXT_PRIORITY_NORMAL    1
+#define DRM_IVPU_CONTEXT_PRIORITY_FOCUS	    2
+#define DRM_IVPU_CONTEXT_PRIORITY_REALTIME  3
+
+/**
+ * struct drm_ivpu_param - Get/Set VPU parameters
+ */
+struct drm_ivpu_param {
+	/**
+	 * @param:
+	 *
+	 * Supported params:
+	 *
+	 * %DRM_IVPU_PARAM_DEVICE_ID:
+	 * PCI Device ID of the VPU device (read-only)
+	 *
+	 * %DRM_IVPU_PARAM_DEVICE_REVISION:
+	 * VPU device revision (read-only)
+	 *
+	 * %DRM_IVPU_PARAM_PLATFORM_TYPE:
+	 * Returns %DRM_IVPU_PLATFORM_TYPE_SILICON on real hardware or device specific
+	 * platform type when executing on a simulator or emulator (read-only)
+	 *
+	 * %DRM_IVPU_PARAM_CORE_CLOCK_RATE:
+	 * Current PLL frequency (read-only)
+	 *
+	 * %DRM_IVPU_PARAM_NUM_CONTEXTS:
+	 * Maximum number of simultaneously existing contexts (read-only)
+	 *
+	 * %DRM_IVPU_PARAM_CONTEXT_BASE_ADDRESS:
+	 * Lowest VPU virtual address available in the current context (read-only)
+	 *
+	 * %DRM_IVPU_PARAM_CONTEXT_PRIORITY:
+	 * Value of current context scheduling priority (read-write).
+	 * See DRM_IVPU_CONTEXT_PRIORITY_* for possible values.
+	 *
+	 */
+	__u32 param;
+
+	/** @index: Index for params that have multiple instances */
+	__u32 index;
+
+	/** @value: Param value */
+	__u64 value;
+};
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif /* __UAPI_IVPU_DRM_H__ */
Signature · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Maxfeldstr. 5, 90409 Nürnberg, Germany
(HRB 36809, AG Nürnberg)
Geschäftsführer: Ivo Totev
#10 lore raw

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

Hi, thanks for in-depth review.

Quoted 14 lines · Thomas Zimmermann

Hi,

please find some review comments below.

Am 24.09.22 um 17:11 schrieb Jacek Lawrynowicz:

Quoted 9 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +9 -0
+static int ivpu_irq_init(struct ivpu_device *vdev)
+{
+    struct pci_dev *pdev = to_pci_dev(vdev->drm.dev);
+    int ret;
+
+    ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI | PCI_IRQ_MSIX);
+    if (ret < 0) {
+        ivpu_err(vdev, "Failed to allocate and MSI IRQ: %d\n", ret);
+        return ret;
Quoted 8 lines

drivers/gpu/drm/ivpu/ivpu_drv.c line 206

Quoted 6 lines

+    }

drivers/gpu/drm/ivpu/ivpu_drv.c new file +5 -0
+
+    vdev->irq = pci_irq_vector(pdev, 0);
+
+    ret = request_irq(vdev->irq, vdev->hw->ops->irq_handler, IRQF_SHARED,
+              DRIVER_NAME, vdev);

There's devm_request_irq(). Within DRM, we have mostly adopted managed cleanup. New drivers should be written that way.

Sure, makes sense.

Quoted 11 lines

drivers/gpu/drm/ivpu/ivpu_drv.c line 215

Quoted 9 lines

+    if (ret) {
+        ivpu_err(vdev, "Failed to request an IRQ %d\n", ret); +        pci_free_irq_vectors(pdev);
+    }

drivers/gpu/drm/ivpu/ivpu_drv.c new file +5 -0
+
+    return ret;
+}
+
+static void ivpu_irq_fini(struct ivpu_device *vdev)

All these _fini functions should be replaced by managed cleanups with the devm_, drmm_ and pcim_. There are sometimes multiple ways of supporting this, but manually calling _fini should be avoided.

Yes, with devm ivpu_irq_fini() is no longer needed.

Quoted 5 lines
Quoted 3 lines

+{
+    free_irq(vdev->irq, vdev);
+    pci_free_irq_vectors(to_pci_dev(vdev->drm.dev));

There's no pcim_alloc_irq_vectors(). It's better to add one or at least provide such an implementation within your driver.

It is not actually needed. pci_alloc_irq_vectors() is implicitly managed. It calls pcim_setup_msi_release() that will free irq vectors.

Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +8 -0
+}
+
+static int ivpu_pci_init(struct ivpu_device *vdev)
+{
+    struct pci_dev *pdev = to_pci_dev(vdev->drm.dev);
+    struct resource *bar0 = &pdev->resource[0];
+    struct resource *bar4 = &pdev->resource[4];
+    int ret;
Quoted 8 lines
Quoted 6 lines

+
+    ivpu_dbg(MISC, "Mapping BAR0 (RegV) %pR\n", bar0); +    vdev->regv = devm_ioremap_resource(vdev->drm.dev, bar0); +    if (IS_ERR(vdev->regv)) {
+        ivpu_err(vdev, "Failed to map bar 0\n");
+        return -ENOMEM;

Why not return PTR_ERR(vdev->regv) ?

Yes, PTR_ERR should be returned here.

Quoted 9 lines
Quoted 7 lines

+    }
+
+    ivpu_dbg(MISC, "Mapping BAR4 (RegB) %pR\n", bar4); +    vdev->regb = devm_ioremap_resource(vdev->drm.dev, bar4); +    if (IS_ERR(vdev->regb)) {
+        ivpu_err(vdev, "Failed to map bar 4\n");
+        return -ENOMEM;

Same

OK

Quoted 6 lines
Quoted 6 lines

+    }
+
+    ret = dma_set_mask_and_coherent(vdev->drm.dev, DMA_BIT_MASK(38)); +    if (ret) {
+        ivpu_err(vdev, "Failed to set DMA mask: %d\n", ret); +        return ret;

Quoted 8 lines

drivers/gpu/drm/ivpu/ivpu_drv.c line 251

Quoted 6 lines

+    }

drivers/gpu/drm/ivpu/ivpu_drv.c new file +5 -0
+
+    /* Clear any pending errors */
+    pcie_capability_clear_word(pdev, PCI_EXP_DEVSTA, 0x3f);
+
+    ret = pci_enable_device(pdev);

pcim_enable device()

OK

Quoted 5 lines
Quoted 5 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +5 -0
+static int ivpu_probe(struct pci_dev *pdev, const struct pci_device_id *id)
+{
+    struct ivpu_device *vdev;
+    int ret;
+
Quoted 8 lines
Quoted 6 lines

+    vdev = devm_drm_dev_alloc(&pdev->dev, &driver, struct ivpu_device, drm); +    if (IS_ERR(vdev))
+        return PTR_ERR(vdev);
+
+    pci_set_drvdata(pdev, vdev);
+    vdev->drm.dev_private = vdev;

No more use of dev_private in new drivers. Your struct ivpu_device should rather embed an instance of struct drm_device and you should upcast if necessary.

OK, we are not using it anyway.

Quoted 28 lines
Quoted 28 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +28 -0
+
+    ret = ivpu_dev_init(vdev);
+    if (ret) {
+        dev_err(&pdev->dev, "Failed to initialize VPU device: %d\n", ret);
+        return ret;
+    }
+
+    ret = drm_dev_register(&vdev->drm, 0);
+    if (ret) {
+        dev_err(&pdev->dev, "Failed to register DRM device: %d\n", ret);
+        ivpu_dev_fini(vdev);
+    }
+
+    return ret;
+}
+
+static void ivpu_remove(struct pci_dev *pdev)
+{
+    struct ivpu_device *vdev = pci_get_drvdata(pdev);
+
+    drm_dev_unregister(&vdev->drm);
+    ivpu_dev_fini(vdev);
+}
+
+static struct pci_driver ivpu_pci_driver = {
+    .name = KBUILD_MODNAME,
+    .id_table = ivpu_pci_ids,
+    .probe = ivpu_probe,
Quoted 8 lines

drivers/gpu/drm/ivpu/ivpu_drv.c line 376

Quoted 6 lines

+    .remove = ivpu_remove,

drivers/gpu/drm/ivpu/ivpu_drv.c new file +5 -0
+};
+
+static __init int ivpu_init(void)
+{
+    pr_info("Intel VPU driver version: %s", DRIVER_VERSION_STR);

No log spam please.

This is gone after using module_pci_driver().

Quoted 14 lines

drivers/gpu/drm/ivpu/ivpu_drv.c line 387

Quoted 11 lines
drivers/gpu/drm/ivpu/ivpu_drv.c new file +11 -0
+
+    return pci_register_driver(&ivpu_pci_driver);
+}
+
+static __exit void ivpu_fini(void)
+{
+    pci_unregister_driver(&ivpu_pci_driver);
+}
+
+module_init(ivpu_init);
+module_exit(ivpu_fini);

Maybe just module_pci_driver().

Quoted 1 line

[1] https://elixir.bootlin.com/linux/latest/source/include/linux/pci.h#L1480

Sure, make sense.

Quoted 5 lines

drivers/gpu/drm/ivpu/ivpu_drv.h line 47

Quoted 3 lines
drivers/gpu/drm/ivpu/ivpu_drv.h new file +3 -0
+
+#define ivpu_err(vdev, fmt, ...) \
+    dev_err((vdev)->drm.dev, "[%s] ERROR: " fmt, __func__, ##__VA_ARGS__)

I see why you want your own dbg macros. But why do you duplicate DRM's print helpers?

No idea :)
I'm gonna just use drm helpers for err, warn and info.

Quoted 9 lines
Quoted 9 lines
drivers/gpu/drm/ivpu/ivpu_drv.h new file +9 -0
+
+#define ivpu_err_ratelimited(vdev, fmt, ...) \
+    dev_err_ratelimited((vdev)->drm.dev, "[%s] ERROR: " fmt, __func__, ##__VA_ARGS__)
+
+#define ivpu_warn(vdev, fmt, ...) \
+    dev_warn((vdev)->drm.dev, "[%s] WARNING: " fmt, __func__, ##__VA_ARGS__)
+
+#define ivpu_warn_ratelimited(vdev, fmt, ...) \
+    dev_warn_ratelimited((vdev)->drm.dev, "[%s] WARNING: " fmt, __func__, ##__VA_ARGS__)
Quoted 8 lines

drivers/gpu/drm/ivpu/ivpu_drv.h line 62

Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_drv.h new file +6 -0
+
+#define ivpu_info(vdev, fmt, ...) dev_info((vdev)->drm.dev, fmt, ##__VA_ARGS__)
+
+#define ivpu_dbg(type, fmt, args...) do {                                      \
+    if (unlikely(IVPU_DBG_##type & ivpu_dbg_mask))                         \
+        dev_dbg((vdev)->drm.dev, "[%s] " fmt, #type, ##args);          \

Why not drm_dbg()?

Mostly to control the format, so messages can be a little shorter. We will probably switch to drm_dbg once the dyndbg implementation matures a bit.

Quoted 16 lines

drivers/gpu/drm/ivpu/ivpu_drv.h line 75

Quoted 13 lines
drivers/gpu/drm/ivpu/ivpu_drv.h new file +13 -0
+} while (0)
+
+#define IVPU_WA(wa_name) (vdev->wa.wa_name)
+
+struct ivpu_wa_table {
+    bool punit_disabled;
+    bool clear_runtime_mem;
+};
+
+struct ivpu_hw_info;
+
+struct ivpu_device {
+    struct drm_device drm; /* Must be first */

Does not really have to be the first. We use upcast helpers that convert from the DRM type to the driver types.

Quoted 6 lines

static inline struct ivpu_device *to_ivpu_device(struct drm_device *drm) {
    return container_of(drm, struct ivpu_device, drm);
}

Just use that to get the ipvu device.

OK

Quoted 6 lines

drivers/gpu/drm/ivpu/ivpu_hw.h line 35

Quoted 4 lines
drivers/gpu/drm/ivpu/ivpu_hw.h new file +4 -0
+
+struct ivpu_addr_range {
+    u64 start;
+    u64 end;

resource_size_t is the canonical type here. TBH I'm surprised that linux doesn't already have a type for address ranges. There must be plenty of drivers defining such a type.

OK, I will use resource_size_t. And yes, I'm also surprised. It is such basic type.

Quoted 7 lines

drivers/gpu/drm/ivpu/ivpu_hw_mtl.c line 87

Quoted 5 lines
drivers/gpu/drm/ivpu/ivpu_hw_mtl.c new file +5 -0
+};
+
+static void ivpu_hw_timeouts_init(struct ivpu_device *vdev)
+{
+    if (ivpu_is_simics(vdev) || ivpu_is_fpga(vdev)) {

I have seen such tests in multiple locations. Wouldn't it be better to write entirely different helpers for each?  Handling different models/revisions in the same function is usually bad advice.

I understand your point but we have just a couple usages in the whole driver and most of them affect just a single line. We have separated all generation specific code in struct ivpu_hw_ops and have struct ivpu_wa_table to handle revision specific WAs, so ivpu_is_*() usages are minimized.

Regards,
Jacek

#11 lore raw

PATCH v3 2/7 drm/ivpu: Add Intel VPU MMU support

In reply to Jacek Lawrynowicz

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

Address space is implemented as a struct 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 to 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/gpu/drm/ivpu/Makefile           |   4 +-
 drivers/gpu/drm/ivpu/ivpu_drv.c         |  59 +-
 drivers/gpu/drm/ivpu/ivpu_drv.h         |   7 +
 drivers/gpu/drm/ivpu/ivpu_hw_mtl.c      |  10 +
 drivers/gpu/drm/ivpu/ivpu_mmu.c         | 883 ++++++++++++++++++++++++
 drivers/gpu/drm/ivpu/ivpu_mmu.h         |  53 ++
 drivers/gpu/drm/ivpu/ivpu_mmu_context.c | 419 +++++++++++
 drivers/gpu/drm/ivpu/ivpu_mmu_context.h |  49 ++
 include/uapi/drm/ivpu_drm.h             |   4 +
 9 files changed, 1485 insertions(+), 3 deletions(-)
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu.c
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu.h
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu_context.c
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu_context.h
drivers/gpu/drm/ivpu/Makefile +3 -1
index e59dc65abe6a..95bb04f26296 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_IVPU) += intel_vpu.o
drivers/gpu/drm/ivpu/ivpu_drv.c +57 -2

136 lines not on this page · raw message

drivers/gpu/drm/ivpu/ivpu_drv.h +7 -0
index 43dfa78544c6..6eec3eb76c2f 100644
@@ -14,6 +14,8 @@
 #include <linux/xarray.h>
 #include <uapi/drm/ivpu_drm.h>
 
+#include "ivpu_mmu_context.h"
+
 #define DRIVER_NAME "intel_vpu"
 #define DRIVER_DESC "Driver for Intel Versatile Processing Unit (VPU)"
 #define DRIVER_DATE "20220913"
@@ -70,6 +72,7 @@ struct ivpu_wa_table {
 };
 
 struct ivpu_hw_info;
+struct ivpu_mmu_info;
 
 struct ivpu_device {
 	struct drm_device drm; /* Must be first */
@@ -80,7 +83,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;
 
@@ -95,6 +100,8 @@ struct ivpu_device {
 struct ivpu_file_priv {
 	struct kref ref;
 	struct ivpu_device *vdev;
+	struct mutex lock;
+	struct ivpu_mmu_context ctx;
 	u32 priority;
 };
 
drivers/gpu/drm/ivpu/ivpu_hw_mtl.c +10 -0
index 17fc8e1104df..525b57c4029c 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 irqreturn_t 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))
+		ret &= ivpu_mmu_irq_evtq_handler(vdev);
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_1_INT, status))
+		ivpu_dbg(IRQ, "MMU sync complete\n");
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_2_INT, status))
+		ret &= ivpu_mmu_irq_gerr_handler(vdev);
+
 	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, CPU_INT_REDIRECT_0_INT, status))
 		ret &= ivpu_hw_mtl_irq_wdt_mss_handler(vdev);
 
drivers/gpu/drm/ivpu/ivpu_mmu.c new file +883 -0

883 lines not on this page · raw message

drivers/gpu/drm/ivpu/ivpu_mmu.h new file +53 -0

53 lines not on this page · raw message

drivers/gpu/drm/ivpu/ivpu_mmu_context.c new file +419 -0

419 lines not on this page · raw message

drivers/gpu/drm/ivpu/ivpu_mmu_context.h new file +49 -0

49 lines not on this page · raw message

include/uapi/drm/ivpu_drm.h +4 -0
index 7a7dbe2f521b..cb48d6118412 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
#12 lore raw

Re: [PATCH v3 2/7] drm/ivpu: Add Intel VPU MMU support

Quoted 2 lines · Jacek Lawrynowicz

VPU Memory Management Unit is based on ARM MMU-600.
It allows to create multiple virtual address spaces for the device and

"It allows the creation of"?

Quoted 14 lines

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.

Quoted 7 lines

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

So, this doesn't look valid. This indicates that Karol wrote this patch, then it was handed off to Krystian, and then to you. However, if that were the case, then the patch should be from Karol (which is then the author in git). It is not. It is from you.

What I suspect is happening is that the three of you all co-authroed this change. In that case, there should probably be Co-developed-by tags in addition to SOB to make that clear.

Also, I missed this on the first patch I reviewed eariler, but it looks to have the same issue.

Quoted 45 lines
---
  drivers/gpu/drm/ivpu/Makefile           |   4 +-
  drivers/gpu/drm/ivpu/ivpu_drv.c         |  59 +-
  drivers/gpu/drm/ivpu/ivpu_drv.h         |   7 +
  drivers/gpu/drm/ivpu/ivpu_hw_mtl.c      |  10 +
  drivers/gpu/drm/ivpu/ivpu_mmu.c         | 883 ++++++++++++++++++++++++
  drivers/gpu/drm/ivpu/ivpu_mmu.h         |  53 ++
  drivers/gpu/drm/ivpu/ivpu_mmu_context.c | 419 +++++++++++
  drivers/gpu/drm/ivpu/ivpu_mmu_context.h |  49 ++
  include/uapi/drm/ivpu_drm.h             |   4 +
  9 files changed, 1485 insertions(+), 3 deletions(-)

create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu_context.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu_context.h

drivers/gpu/drm/ivpu/Makefile +3 -1
index e59dc65abe6a..95bb04f26296 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_IVPU) += intel_vpu.o
drivers/gpu/drm/ivpu/ivpu_drv.c +2 -0
index a01c7244f6e5..cbeb9a801a31 100644
@@ -14,6 +14,8 @@
 
  #include "ivpu_drv.h"
  #include "ivpu_hw.h"
+#include "ivpu_mmu.h"
+#include "ivpu_mmu_context.h"
 
  #ifndef DRIVER_VERSION_STR
  #define DRIVER_VERSION_STR __stringify(DRM_IVPU_DRIVER_MAJOR) "." \
@@ -50,6 +52,11 @@ char *ivpu_platform_to_str(u32 platform)
 

drivers/gpu/drm/ivpu/ivpu_drv.c char *ivpu_platform_to_str(u32 platform) · line 62

Quoted 10 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +5 -0
  void ivpu_file_priv_get(struct ivpu_file_priv *file_priv, struct ivpu_file_priv **link)
  {
+	struct ivpu_device *vdev = file_priv->vdev;
+
+	ivpu_dbg(KREF, "file_priv get: ctx %u refcount %u\n",
+		 file_priv->ctx.id, kref_read(&file_priv->ref));
+
  	kref_get(&file_priv->ref);
  	*link = file_priv;
  }

This hunk doesn't appear related to $SUBJECT. It looks like it would fit better as part of patch 1.

drivers/gpu/drm/ivpu/ivpu_drv.c void ivpu_file_priv_get(struct ivpu_file_priv *file_priv, struct ivpu_file_priv · line 67

Quoted 5 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +1 -0
@@ -57,6 +64,12 @@ void ivpu_file_priv_get(struct ivpu_file_priv *file_priv, struct ivpu_file_priv
  static void file_priv_release(struct kref *ref)
  {
  	struct ivpu_file_priv *file_priv = container_of(ref, struct ivpu_file_priv, ref);
+	struct ivpu_device *vdev = file_priv->vdev;

Why do we need this? It doesn't appear to be used at this time. If it is needed later in the series, add it then.

Quoted 9 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +5 -0
+
+	ivpu_dbg(FILE, "file_priv release: ctx %u\n", file_priv->ctx.id);
+
+	if (file_priv->ctx.id)
+		ivpu_mmu_user_context_fini(file_priv);
 
  	kfree(file_priv);
  }

drivers/gpu/drm/ivpu/ivpu_drv.c static void file_priv_release(struct kref *ref) · line 84

Quoted 8 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +4 -0
@@ -64,6 +77,10 @@ static void file_priv_release(struct kref *ref)
  void ivpu_file_priv_put(struct ivpu_file_priv **link)
  {
  	struct ivpu_file_priv *file_priv = *link;
+	struct ivpu_device *vdev = file_priv->vdev;
+
+	ivpu_dbg(KREF, "file_priv put: ctx %u refcount %u\n",
+		 file_priv->ctx.id, kref_read(&file_priv->ref));
 

vdev doesn't appear to be used, and the log message doesn't appear related to $SUBJECT

drivers/gpu/drm/ivpu/ivpu_drv.c static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_f · line 99

Quoted 12 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +5 -1
  	*link = NULL;
  	kref_put(&file_priv->ref, file_priv_release);
@@ -75,7 +92,11 @@ static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_f
  	struct ivpu_device *vdev = file_priv->vdev;
  	struct pci_dev *pdev = to_pci_dev(vdev->drm.dev);
  	struct drm_ivpu_param *args = data;
-	int ret = 0;
+	int ret;
+
+	ret = ivpu_mmu_user_context_init(file_priv);
+	if (ret)
+		return ret;

I fail to understand why we are initializing a user context in an ioctl.
This will happen N times, for N ioctl calls that a particular user
does. Feels like something that should occur in ivpu_open(). What am I missing?

Quoted 14 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +3 -0
  	switch (args->param) {
  	case DRM_IVPU_PARAM_DEVICE_ID:
@@ -99,6 +120,9 @@ static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_f
  	case DRM_IVPU_PARAM_CONTEXT_PRIORITY:
  		args->value = file_priv->priority;
  		break;
+	case DRM_IVPU_PARAM_CONTEXT_ID:
+		args->value = file_priv->ctx.id;
+		break;
  	default:
  		ret = -EINVAL;
  	}

drivers/gpu/drm/ivpu/ivpu_drv.c static int ivpu_set_param_ioctl(struct drm_device *dev, void *data, struct drm_f · line 142

Quoted 10 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +5 -1
@@ -110,7 +134,11 @@ static int ivpu_set_param_ioctl(struct drm_device *dev, void *data, struct drm_f
  {
  	struct ivpu_file_priv *file_priv = file->driver_priv;
  	struct drm_ivpu_param *args = data;
-	int ret = 0;
+	int ret;
+
+	ret = ivpu_mmu_user_context_init(file_priv);
+	if (ret)
+		return ret;
 

Again, initializing a context in an ioctl isn't making sense to me.

drivers/gpu/drm/ivpu/ivpu_drv.c static int ivpu_open(struct drm_device *dev, struct drm_file *file) · line 176

Quoted 13 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +4 -0
  	switch (args->param) {
  	case DRM_IVPU_PARAM_CONTEXT_PRIORITY:
@@ -139,9 +167,13 @@ static int ivpu_open(struct drm_device *dev, struct drm_file *file)
  	file_priv->priority = DRM_IVPU_CONTEXT_PRIORITY_NORMAL;
 
  	kref_init(&file_priv->ref);
+	mutex_init(&file_priv->lock);
 
  	file->driver_priv = file_priv;
 
+	ivpu_dbg(FILE, "file_priv alloc: process %s pid %d\n",
+		 current->comm, task_pid_nr(current));
+

As it is, this doesn't seem related to $SUBJECT and seems to belong in patch 1.

However, this seems dubious. Trace gives you the task name and pid for the process that triggered a trace, and you don't need to add anything to your code to be able to trace any symbol.

drivers/gpu/drm/ivpu/ivpu_drv.c int ivpu_shutdown(struct ivpu_device *vdev) · line 201

Quoted 10 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +1 -0
  	return 0;
  }
 
@@ -164,6 +196,7 @@ int ivpu_shutdown(struct ivpu_device *vdev)
  	int ret;
 
  	ivpu_hw_irq_disable(vdev);
+	ivpu_mmu_disable(vdev);
 
  	ret = ivpu_hw_power_down(vdev);

I'm a bit confused. It is good practice to balance your init and cleanup (fini in this series). Everything that is done in init, is undone in fini in the reverse order. This makes it easy to make sure things haven't been forgotten.

I don't understand what ivpu_shutdown() is suposed to be paired with. I thought it was ivpu_hw_power_up(), but you have power_down() in shutdown().

I can't tell what mmu_disable() is suposed to pair with. dev_init()?

Quoted 60 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +22 -0
  	if (ret)
@@ -272,6 +305,10 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
  	if (!vdev->hw)
  		return -ENOMEM;
 
+	vdev->mmu = devm_kzalloc(vdev->drm.dev, sizeof(*vdev->mmu), GFP_KERNEL);
+	if (!vdev->mmu)
+		return -ENOMEM;
+
  	vdev->hw->ops = &ivpu_hw_mtl_ops;
  	vdev->platform = IVPU_PLATFORM_INVALID;
 
@@ -303,8 +340,24 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
  		goto err_irq_fini;
  	}
 
+	ret = ivpu_mmu_global_context_init(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to initialize global MMU context: %d\n", ret);
+		goto err_power_down;
+	}
+
+	ret = ivpu_mmu_init(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to initialize MMU device: %d\n", ret);
+		goto err_mmu_gctx_fini;
+	}
+
  	return 0;
 
+err_mmu_gctx_fini:
+	ivpu_mmu_global_context_fini(vdev);
+err_power_down:
+	ivpu_hw_power_down(vdev);
  err_irq_fini:
  	ivpu_irq_fini(vdev);
  err_pci_fini:
@@ -316,6 +369,8 @@ static void ivpu_dev_fini(struct ivpu_device *vdev)
  {
  	ivpu_shutdown(vdev);
 
+	ivpu_mmu_fini(vdev);
+	ivpu_mmu_global_context_fini(vdev);
  	ivpu_irq_fini(vdev);
  	ivpu_pci_fini(vdev);
 
drivers/gpu/drm/ivpu/ivpu_drv.h +2 -0
index 43dfa78544c6..6eec3eb76c2f 100644
@@ -14,6 +14,8 @@
  #include <linux/xarray.h>
  #include <uapi/drm/ivpu_drm.h>
 
+#include "ivpu_mmu_context.h"
+
  #define DRIVER_NAME "intel_vpu"
  #define DRIVER_DESC "Driver for Intel Versatile Processing Unit (VPU)"
  #define DRIVER_DATE "20220913"

drivers/gpu/drm/ivpu/ivpu_drv.h struct ivpu_wa_table { · line 75

Quoted 4 lines
drivers/gpu/drm/ivpu/ivpu_drv.h +1 -0
@@ -70,6 +72,7 @@ struct ivpu_wa_table {
  };
 
  struct ivpu_hw_info;
+struct ivpu_mmu_info;

Why not just include ivpu_mmu.h?

Quoted 14 lines
drivers/gpu/drm/ivpu/ivpu_drv.h +2 -0
  struct ivpu_device {
  	struct drm_device drm; /* Must be first */
@@ -80,7 +83,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;
 

drivers/gpu/drm/ivpu/ivpu_drv.h struct ivpu_device { · line 103

Quoted 4 lines
drivers/gpu/drm/ivpu/ivpu_drv.h +1 -0
@@ -95,6 +100,8 @@ struct ivpu_device {
  struct ivpu_file_priv {
  	struct kref ref;
  	struct ivpu_device *vdev;
+	struct mutex lock;

A comment indicating what this protects would be extremely useful.

Quoted 38 lines
drivers/gpu/drm/ivpu/ivpu_drv.h +1 -0
+	struct ivpu_mmu_context ctx;
  	u32 priority;
  };
 
drivers/gpu/drm/ivpu/ivpu_hw_mtl.c +10 -0
index 17fc8e1104df..525b57c4029c 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 irqreturn_t 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))
+		ret &= ivpu_mmu_irq_evtq_handler(vdev);
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_1_INT, status))
+		ivpu_dbg(IRQ, "MMU sync complete\n");
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_2_INT, status))
+		ret &= ivpu_mmu_irq_gerr_handler(vdev);
+
  	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, CPU_INT_REDIRECT_0_INT, status))
  		ret &= ivpu_hw_mtl_irq_wdt_mss_handler(vdev);
 
drivers/gpu/drm/ivpu/ivpu_mmu.c new file +0 -0
new file mode 100644
index 000000000000..b9f225a83c45

drivers/gpu/drm/ivpu/ivpu_mmu.c line 7

Quoted 7 lines
drivers/gpu/drm/ivpu/ivpu_mmu.c new file +7 -0
@@ -0,0 +1,883 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#include <linux/highmem.h>
+#include <linux/circ_buf.h>

Alphabetical order?

Quoted 8 lines
drivers/gpu/drm/ivpu/ivpu_mmu.c new file +8 -0
+
+#include "ivpu_drv.h"
+#include "ivpu_hw_mtl_reg.h"
+#include "ivpu_hw_reg_io.h"
+#include "ivpu_mmu.h"
+#include "ivpu_mmu_context.h"
+
+#define IVPU_MMU_IDR0_REF		0x080f3e0f

drivers/gpu/drm/ivpu/ivpu_mmu.c line 25

Quoted 10 lines
drivers/gpu/drm/ivpu/ivpu_mmu.c new file +10 -0
+#define IVPU_MMU_IDR0_REF_SIMICS	0x080f3e1f
+#define IVPU_MMU_IDR1_REF		0x0e739d18
+#define IVPU_MMU_IDR3_REF		0x0000003c
+#define IVPU_MMU_IDR5_REF		0x00040070
+#define IVPU_MMU_IDR5_REF_SIMICS	0x00000075
+#define IVPU_MMU_IDR5_REF_FPGA		0x00800075
+
+#define IVPU_MMU_CDTAB_ENT_SIZE		64
+#define IVPU_MMU_CDTAB_ENT_COUNT_LOG2	8 /* 256 entries */
+#define IVPU_MMU_CDTAB_ENT_COUNT	((u32)1 << IVPU_MMU_CDTAB_ENT_COUNT_LOG2)

Isn't this just BIT(IVPU_MMU_CDTAB_ENT_COUNT_LOG2)?

Quoted 351 lines
drivers/gpu/drm/ivpu/ivpu_mmu.c new file +351 -0
+
+#define IVPU_MMU_STREAM_ID0		0
+#define IVPU_MMU_STREAM_ID3		3
+
+#define IVPU_MMU_STRTAB_ENT_SIZE	64
+#define IVPU_MMU_STRTAB_ENT_COUNT	4
+#define IVPU_MMU_STRTAB_CFG_LOG2SIZE	2
+#define IVPU_MMU_STRTAB_CFG		IVPU_MMU_STRTAB_CFG_LOG2SIZE
+
+#define IVPU_MMU_Q_COUNT_LOG2		4 /* 16 entries */
+#define IVPU_MMU_Q_COUNT		((u32)1 << IVPU_MMU_Q_COUNT_LOG2)
+#define IVPU_MMU_Q_WRAP_BIT		(IVPU_MMU_Q_COUNT << 1)
+#define IVPU_MMU_Q_WRAP_MASK		(IVPU_MMU_Q_WRAP_BIT - 1)
+#define IVPU_MMU_Q_IDX_MASK		(IVPU_MMU_Q_COUNT - 1)
+#define IVPU_MMU_Q_IDX(val)		((val) & IVPU_MMU_Q_IDX_MASK)
+
+#define IVPU_MMU_CMDQ_CMD_SIZE		16
+#define IVPU_MMU_CMDQ_SIZE		(IVPU_MMU_Q_COUNT * IVPU_MMU_CMDQ_CMD_SIZE)
+
+#define IVPU_MMU_EVTQ_CMD_SIZE		32
+#define IVPU_MMU_EVTQ_SIZE		(IVPU_MMU_Q_COUNT * IVPU_MMU_EVTQ_CMD_SIZE)
+
+#define IVPU_MMU_CMD_OPCODE		GENMASK(8, 0)
+
+#define IVPU_MMU_CMD_SYNC_0_CS		GENMASK(13, 12)
+#define IVPU_MMU_CMD_SYNC_0_MSH		GENMASK(23, 22)
+#define IVPU_MMU_CMD_SYNC_0_MSI_ATTR	GENMASK(27, 24)
+#define IVPU_MMU_CMD_SYNC_0_MSI_ATTR	GENMASK(27, 24)
+#define IVPU_MMU_CMD_SYNC_0_MSI_DATA	GENMASK(63, 32)
+
+#define IVPU_MMU_CMD_CFGI_0_SSEC	BIT(10)
+#define IVPU_MMU_CMD_CFGI_0_SSV		BIT(11)
+#define IVPU_MMU_CMD_CFGI_0_SSID	GENMASK(31, 12)
+#define IVPU_MMU_CMD_CFGI_0_SID		GENMASK(63, 32)
+#define IVPU_MMU_CMD_CFGI_1_RANGE	GENMASK(4, 0)
+
+#define IVPU_MMU_CMD_TLBI_0_ASID	GENMASK(63, 48)
+#define IVPU_MMU_CMD_TLBI_0_VMID	GENMASK(47, 32)
+
+#define CMD_PREFETCH_CFG		0x1
+#define CMD_CFGI_STE			0x3
+#define CMD_CFGI_ALL			0x4
+#define CMD_CFGI_CD			0x5
+#define CMD_CFGI_CD_ALL			0x6
+#define CMD_TLBI_NH_ASID		0x11
+#define CMD_TLBI_EL2_ALL		0x20
+#define CMD_TLBI_NSNH_ALL		0x30
+#define CMD_SYNC			0x46
+
+#define IVPU_MMU_EVT_F_UUT		0x01
+#define IVPU_MMU_EVT_C_BAD_STREAMID	0x02
+#define IVPU_MMU_EVT_F_STE_FETCH	0x03
+#define IVPU_MMU_EVT_C_BAD_STE		0x04
+#define IVPU_MMU_EVT_F_BAD_ATS_TREQ	0x05
+#define IVPU_MMU_EVT_F_STREAM_DISABLED	0x06
+#define IVPU_MMU_EVT_F_TRANSL_FORBIDDEN	0x07
+#define IVPU_MMU_EVT_C_BAD_SUBSTREAMID	0x08
+#define IVPU_MMU_EVT_F_CD_FETCH		0x09
+#define IVPU_MMU_EVT_C_BAD_CD		0x0a
+#define IVPU_MMU_EVT_F_WALK_EABT	0x0b
+#define IVPU_MMU_EVT_F_TRANSLATION	0x10
+#define IVPU_MMU_EVT_F_ADDR_SIZE	0x11
+#define IVPU_MMU_EVT_F_ACCESS		0x12
+#define IVPU_MMU_EVT_F_PERMISSION	0x13
+#define IVPU_MMU_EVT_F_TLB_CONFLICT	0x20
+#define IVPU_MMU_EVT_F_CFG_CONFLICT	0x21
+#define IVPU_MMU_EVT_E_PAGE_REQUEST	0x24
+#define IVPU_MMU_EVT_F_VMS_FETCH	0x25
+
+#define IVPU_MMU_EVTS_MAX		8
+
+#define IVPU_MMU_EVT_OP_MASK		GENMASK_ULL(7, 0)
+#define IVPU_MMU_EVT_SSID_MASK		GENMASK_ULL(31, 12)
+
+#define IVPU_MMU_Q_BASE_RWA		BIT(62)
+#define IVPU_MMU_Q_BASE_ADDR_MASK	GENMASK_ULL(51, 5)
+#define IVPU_MMU_STRTAB_BASE_RA		BIT(62)
+#define IVPU_MMU_STRTAB_BASE_ADDR_MASK	GENMASK_ULL(51, 6)
+
+#define IVPU_MMU_IRQ_EVTQ_EN		BIT(2)
+#define IVPU_MMU_IRQ_GERROR_EN		BIT(0)
+
+#define IVPU_MMU_CR0_ATSCHK		BIT(4)
+#define IVPU_MMU_CR0_CMDQEN		BIT(3)
+#define IVPU_MMU_CR0_EVTQEN		BIT(2)
+#define IVPU_MMU_CR0_PRIQEN		BIT(1)
+#define IVPU_MMU_CR0_SMMUEN		BIT(0)
+
+#define IVPU_MMU_CR1_TABLE_SH		GENMASK(11, 10)
+#define IVPU_MMU_CR1_TABLE_OC		GENMASK(9, 8)
+#define IVPU_MMU_CR1_TABLE_IC		GENMASK(7, 6)
+#define IVPU_MMU_CR1_QUEUE_SH		GENMASK(5, 4)
+#define IVPU_MMU_CR1_QUEUE_OC		GENMASK(3, 2)
+#define IVPU_MMU_CR1_QUEUE_IC		GENMASK(1, 0)
+#define IVPU_MMU_CACHE_NC		0
+#define IVPU_MMU_CACHE_WB		1
+#define IVPU_MMU_CACHE_WT		2
+#define IVPU_MMU_SH_NSH			0
+#define IVPU_MMU_SH_OSH			2
+#define IVPU_MMU_SH_ISH			3
+
+#define IVPU_MMU_CMDQ_OP		GENMASK_ULL(7, 0)
+
+#define IVPU_MMU_CD_0_TCR_T0SZ		GENMASK_ULL(5, 0)
+#define IVPU_MMU_CD_0_TCR_TG0		GENMASK_ULL(7, 6)
+#define IVPU_MMU_CD_0_TCR_IRGN0		GENMASK_ULL(9, 8)
+#define IVPU_MMU_CD_0_TCR_ORGN0		GENMASK_ULL(11, 10)
+#define IVPU_MMU_CD_0_TCR_SH0		GENMASK_ULL(13, 12)
+#define IVPU_MMU_CD_0_TCR_EPD0		BIT_ULL(14)
+#define IVPU_MMU_CD_0_TCR_EPD1		BIT_ULL(30)
+#define IVPU_MMU_CD_0_ENDI		BIT(15)
+#define IVPU_MMU_CD_0_V			BIT(31)
+#define IVPU_MMU_CD_0_TCR_IPS		GENMASK_ULL(34, 32)
+#define IVPU_MMU_CD_0_TCR_TBI0		BIT_ULL(38)
+#define IVPU_MMU_CD_0_AA64		BIT(41)
+#define IVPU_MMU_CD_0_S			BIT(44)
+#define IVPU_MMU_CD_0_R			BIT(45)
+#define IVPU_MMU_CD_0_A			BIT(46)
+#define IVPU_MMU_CD_0_ASET		BIT(47)
+#define IVPU_MMU_CD_0_ASID		GENMASK_ULL(63, 48)
+
+#define IVPU_MMU_CD_1_TTB0_MASK		GENMASK_ULL(51, 4)
+
+#define IVPU_MMU_STE_0_S1CDMAX		GENMASK_ULL(63, 59)
+#define IVPU_MMU_STE_0_S1FMT		GENMASK_ULL(5, 4)
+#define IVPU_MMU_STE_0_S1FMT_LINEAR	0
+#define IVPU_MMU_STE_DWORDS		8
+#define IVPU_MMU_STE_0_CFG_S1_TRANS	5
+#define IVPU_MMU_STE_0_CFG		GENMASK_ULL(3, 1)
+#define IVPU_MMU_STE_0_S1CTXPTR_MASK	GENMASK_ULL(51, 6)
+#define IVPU_MMU_STE_0_V			BIT(0)
+
+#define IVPU_MMU_STE_1_STRW_NSEL1	0ul
+#define IVPU_MMU_STE_1_CONT		GENMASK_ULL(16, 13)
+#define IVPU_MMU_STE_1_STRW		GENMASK_ULL(31, 30)
+#define IVPU_MMU_STE_1_PRIVCFG		GENMASK_ULL(49, 48)
+#define IVPU_MMU_STE_1_PRIVCFG_UNPRIV	2ul
+#define IVPU_MMU_STE_1_INSTCFG		GENMASK_ULL(51, 50)
+#define IVPU_MMU_STE_1_INSTCFG_DATA	2ul
+#define IVPU_MMU_STE_1_MEV		BIT(19)
+#define IVPU_MMU_STE_1_S1STALLD		BIT(27)
+#define IVPU_MMU_STE_1_S1C_CACHE_NC	0ul
+#define IVPU_MMU_STE_1_S1C_CACHE_WBRA	1ul
+#define IVPU_MMU_STE_1_S1C_CACHE_WT	2ul
+#define IVPU_MMU_STE_1_S1C_CACHE_WB	3ul
+#define IVPU_MMU_STE_1_S1CIR		GENMASK_ULL(3, 2)
+#define IVPU_MMU_STE_1_S1COR		GENMASK_ULL(5, 4)
+#define IVPU_MMU_STE_1_S1CSH		GENMASK_ULL(7, 6)
+#define IVPU_MMU_STE_1_S1DSS		GENMASK_ULL(1, 0)
+#define IVPU_MMU_STE_1_S1DSS_TERMINATE	0x0
+
+#define IVPU_MMU_REG_TIMEOUT_US		(10 * USEC_PER_MSEC)
+#define IVPU_MMU_QUEUE_TIMEOUT_US	(100 * USEC_PER_MSEC)
+
+#define IVPU_MMU_GERROR_ERR_MASK ((REG_FLD(MTL_VPU_HOST_MMU_GERROR, CMDQ)) | \
+				  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, EVTQ_ABT)) | \
+				  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, PRIQ_ABT)) | \
+				  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_CMDQ_ABT)) | \
+				  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_EVTQ_ABT)) | \
+				  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_PRIQ_ABT)) | \
+				  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_ABT)) | \
+				  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, SFM)))
+
+static char *ivpu_mmu_evt_to_str(u32 cmd)
+{
+	switch (cmd) {
+	case IVPU_MMU_EVT_F_UUT:
+		return "Unsupported Upstream Transaction";
+	case IVPU_MMU_EVT_C_BAD_STREAMID:
+		return "Transaction StreamID out of range";
+	case IVPU_MMU_EVT_F_STE_FETCH:
+		return "Fetch of STE caused external abort";
+	case IVPU_MMU_EVT_C_BAD_STE:
+		return "Used STE invalid";
+	case IVPU_MMU_EVT_F_BAD_ATS_TREQ:
+		return "Address Request disallowed for a StreamID";
+	case IVPU_MMU_EVT_F_STREAM_DISABLED:
+		return "Transaction marks non-substream disabled";
+	case IVPU_MMU_EVT_F_TRANSL_FORBIDDEN:
+		return "MMU bypass is disallowed for this StreamID";
+	case IVPU_MMU_EVT_C_BAD_SUBSTREAMID:
+		return "Invalid StreamID";
+	case IVPU_MMU_EVT_F_CD_FETCH:
+		return "Fetch of CD caused external abort";
+	case IVPU_MMU_EVT_C_BAD_CD:
+		return "Fetched CD invalid";
+	case IVPU_MMU_EVT_F_WALK_EABT:
+		return " An external abort occurred fetching a TLB";
+	case IVPU_MMU_EVT_F_TRANSLATION:
+		return "Translation fault";
+	case IVPU_MMU_EVT_F_ADDR_SIZE:
+		return " Output address caused address size fault";
+	case IVPU_MMU_EVT_F_ACCESS:
+		return "Access flag fault";
+	case IVPU_MMU_EVT_F_PERMISSION:
+		return "Permission fault occurred on page access";
+	case IVPU_MMU_EVT_F_TLB_CONFLICT:
+		return "A TLB conflict";
+	case IVPU_MMU_EVT_F_CFG_CONFLICT:
+		return "A configuration cache conflict";
+	case IVPU_MMU_EVT_E_PAGE_REQUEST:
+		return "Page request hint from a client device";
+	case IVPU_MMU_EVT_F_VMS_FETCH:
+		return "Fetch of VMS caused external abort";
+	default:
+		return "Unknown CMDQ command";
+	}
+}
+
+static int ivpu_mmu_config_check(struct ivpu_device *vdev)
+{
+	u32 val_ref;
+	u32 val;
+
+	if (ivpu_is_simics(vdev))
+		val_ref = IVPU_MMU_IDR0_REF_SIMICS;
+	else
+		val_ref = IVPU_MMU_IDR0_REF;
+
+	val = REGV_RD32(MTL_VPU_HOST_MMU_IDR0);
+	if (val != val_ref)
+		ivpu_err(vdev, "IDR0 0x%x != IDR0_REF 0x%x\n", val, val_ref);
+
+	val = REGV_RD32(MTL_VPU_HOST_MMU_IDR1);
+	if (val != IVPU_MMU_IDR1_REF)
+		ivpu_warn(vdev, "IDR1 0x%x != IDR1_REF 0x%x\n", val, IVPU_MMU_IDR1_REF);
+
+	val = REGV_RD32(MTL_VPU_HOST_MMU_IDR3);
+	if (val != IVPU_MMU_IDR3_REF)
+		ivpu_warn(vdev, "IDR3 0x%x != IDR3_REF 0x%x\n", val, IVPU_MMU_IDR3_REF);
+
+	if (ivpu_is_simics(vdev))
+		val_ref = IVPU_MMU_IDR5_REF_SIMICS;
+	else if (ivpu_is_fpga(vdev))
+		val_ref = IVPU_MMU_IDR5_REF_FPGA;
+	else
+		val_ref = IVPU_MMU_IDR5_REF;
+
+	val = REGV_RD32(MTL_VPU_HOST_MMU_IDR5);
+	if (val != val_ref)
+		ivpu_warn(vdev, "IDR5 0x%x != IDR5_REF 0x%x\n", val, val_ref);
+
+	return 0;
+}
+
+static int ivpu_mmu_cdtab_alloc(struct ivpu_device *vdev)
+{
+	struct ivpu_mmu_info *mmu = vdev->mmu;
+	struct ivpu_mmu_cdtab *cdtab = &mmu->cdtab;
+	size_t size = IVPU_MMU_CDTAB_ENT_COUNT * IVPU_MMU_CDTAB_ENT_SIZE;
+
+	cdtab->base = dmam_alloc_coherent(vdev->drm.dev, size, &cdtab->dma, GFP_KERNEL);
+	if (!cdtab->base)
+		return -ENOMEM;
+
+	ivpu_dbg(MMU, "CDTAB alloc: dma=%pad size=%zu\n", &cdtab->dma, size);
+
+	return 0;
+}
+
+static int ivpu_mmu_strtab_alloc(struct ivpu_device *vdev)
+{
+	struct ivpu_mmu_info *mmu = vdev->mmu;
+	struct ivpu_mmu_strtab *strtab = &mmu->strtab;
+	size_t size = IVPU_MMU_STRTAB_ENT_COUNT * IVPU_MMU_STRTAB_ENT_SIZE;
+
+	strtab->base = dmam_alloc_coherent(vdev->drm.dev, size, &strtab->dma, GFP_KERNEL);
+	if (!strtab->base)
+		return -ENOMEM;
+
+	strtab->base_cfg = IVPU_MMU_STRTAB_CFG;
+	strtab->dma_q = IVPU_MMU_STRTAB_BASE_RA;
+	strtab->dma_q |= strtab->dma & IVPU_MMU_STRTAB_BASE_ADDR_MASK;
+
+	ivpu_dbg(MMU, "STRTAB alloc: dma=%pad dma_q=%pad size=%zu\n",
+		 &strtab->dma, &strtab->dma_q, size);
+
+	return 0;
+}
+
+static int ivpu_mmu_cmdq_alloc(struct ivpu_device *vdev)
+{
+	struct ivpu_mmu_info *mmu = vdev->mmu;
+	struct ivpu_mmu_queue *q = &mmu->cmdq;
+
+	q->base = dmam_alloc_coherent(vdev->drm.dev, IVPU_MMU_CMDQ_SIZE, &q->dma, GFP_KERNEL);
+	if (!q->base)
+		return -ENOMEM;
+
+	q->dma_q = IVPU_MMU_Q_BASE_RWA;
+	q->dma_q |= q->dma & IVPU_MMU_Q_BASE_ADDR_MASK;
+	q->dma_q |= IVPU_MMU_Q_COUNT_LOG2;
+
+	ivpu_dbg(MMU, "CMDQ alloc: dma=%pad dma_q=%pad size=%u\n",
+		 &q->dma, &q->dma_q, IVPU_MMU_CMDQ_SIZE);
+
+	return 0;
+}
+
+static int ivpu_mmu_evtq_alloc(struct ivpu_device *vdev)
+{
+	struct ivpu_mmu_info *mmu = vdev->mmu;
+	struct ivpu_mmu_queue *q = &mmu->evtq;
+
+	q->base = dmam_alloc_coherent(vdev->drm.dev, IVPU_MMU_EVTQ_SIZE, &q->dma, GFP_KERNEL);
+	if (!q->base)
+		return -ENOMEM;
+
+	q->dma_q = IVPU_MMU_Q_BASE_RWA;
+	q->dma_q |= q->dma & IVPU_MMU_Q_BASE_ADDR_MASK;
+	q->dma_q |= IVPU_MMU_Q_COUNT_LOG2;
+
+	ivpu_dbg(MMU, "EVTQ alloc: dma=%pad dma_q=%pad size=%u\n",
+		 &q->dma, &q->dma_q, IVPU_MMU_EVTQ_SIZE);
+
+	return 0;
+}
+
+static int ivpu_mmu_structs_alloc(struct ivpu_device *vdev)
+{
+	int ret;
+
+	ret = ivpu_mmu_cdtab_alloc(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to allocate cdtab: %d\n", ret);
+		return ret;
+	}
+
+	ret = ivpu_mmu_strtab_alloc(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to allocate strtab: %d\n", ret);
+		return ret;
+	}
+
+	ret = ivpu_mmu_cmdq_alloc(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to allocate cmdq: %d\n", ret);
+		return ret;
+	}
+
+	ret = ivpu_mmu_evtq_alloc(vdev);
+	if (ret)
+		ivpu_err(vdev, "Failed to allocate evtq: %d\n", ret);
+
+	return ret;
+}
+
+static int ivpu_mmu_reg_write(struct ivpu_device *vdev, u32 reg, u32 val)
+{
+	u32 reg_ack = reg + 4; /* ACK register is 4B after base register */
+	u32 val_ack;

drivers/gpu/drm/ivpu/ivpu_mmu.c line 386

Quoted 10 lines
drivers/gpu/drm/ivpu/ivpu_mmu.c new file +10 -0
+	int ret;
+
+	REGV_WR32(reg, val);
+
+	ret = REGV_POLL(reg_ack, val_ack, (val == val_ack), IVPU_MMU_REG_TIMEOUT_US);
+	if (ret)
+		ivpu_err(vdev, "Failed to write register 0x%x\n", reg);
+
+	return ret;
+}

It really seems like you are reinventing regmap. Any particular reason that mechanism doesn't work for you?

Also, given that this is a PCI device, I'm suprised there is no 0xFFFFFFFF handling.

#13 lore raw

Re: [PATCH v3 2/7] drm/ivpu: Add Intel VPU MMU support

Hi,

Thanks for an in-depth review. My responses are inline.

Quoted 5 lines · Jeffrey Hugo
Quoted 2 lines · Jacek Lawrynowicz

VPU Memory Management Unit is based on ARM MMU-600.
It allows to create multiple virtual address spaces for the device and

"It allows the creation of"?

OK

Quoted 22 lines
Quoted 14 lines

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.

Quoted 7 lines

This patch is has to 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

So, this doesn't look valid.  This indicates that Karol wrote this patch, then it was handed off to Krystian, and then to you.  However, if that were the case, then the patch should be from Karol (which is then the author in git).  It is not.  It is from you.

What I suspect is happening is that the three of you all co-authroed this change.  In that case, there should probably be Co-developed-by tags in addition to SOB to make that clear.

Also, I missed this on the first patch I reviewed eariler, but it looks to have the same issue.

OK, I will change this in all the patches.

Quoted 44 lines
Quoted 44 lines

---
  drivers/gpu/drm/ivpu/Makefile           |   4 +- drivers/gpu/drm/ivpu/ivpu_drv.c         |  59 +- drivers/gpu/drm/ivpu/ivpu_drv.h         |   7 + drivers/gpu/drm/ivpu/ivpu_hw_mtl.c      |  10 + drivers/gpu/drm/ivpu/ivpu_mmu.c         | 883 ++++++++++++++++++++++++ drivers/gpu/drm/ivpu/ivpu_mmu.h         |  53 ++ drivers/gpu/drm/ivpu/ivpu_mmu_context.c | 419 +++++++++++ drivers/gpu/drm/ivpu/ivpu_mmu_context.h |  49 ++ include/uapi/drm/ivpu_drm.h             |   4 +

  9 files changed, 1485 insertions(+), 3 deletions(-)

  create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu_context.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu_context.h

drivers/gpu/drm/ivpu/Makefile +3 -1
index e59dc65abe6a..95bb04f26296 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_IVPU) += intel_vpu.o

drivers/gpu/drm/ivpu/ivpu_drv.c +4 -0
index a01c7244f6e5..cbeb9a801a31 100644
@@ -14,6 +14,8 @@
     #include "ivpu_drv.h"
   #include "ivpu_hw.h"
+#include "ivpu_mmu.h"
+#include "ivpu_mmu_context.h"
     #ifndef DRIVER_VERSION_STR
   #define DRIVER_VERSION_STR __stringify(DRM_IVPU_DRIVER_MAJOR) "." \
@@ -50,6 +52,11 @@ char *ivpu_platform_to_str(u32 platform)
     void ivpu_file_priv_get(struct ivpu_file_priv *file_priv, struct ivpu_file_priv **link)
   {
+    struct ivpu_device *vdev = file_priv->vdev;
+
Quoted 8 lines

drivers/gpu/drm/ivpu/ivpu_drv.c char *ivpu_platform_to_str(u32 platform) · line 62

Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +3 -0
+    ivpu_dbg(KREF, "file_priv get: ctx %u refcount %u\n",
+         file_priv->ctx.id, kref_read(&file_priv->ref));
+
       kref_get(&file_priv->ref);
       *link = file_priv;
   }

This hunk doesn't appear related to $SUBJECT.  It looks like it would fit better as part of patch 1.

file_priv->ctx is added in this patch and without context id this message does not make sense so this patch is the earliest the message can be printed.

Quoted 7 lines

drivers/gpu/drm/ivpu/ivpu_drv.c void ivpu_file_priv_get(struct ivpu_file_priv *file_priv, struct ivpu_file_priv · line 67

Quoted 5 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +1 -0
@@ -57,6 +64,12 @@ void ivpu_file_priv_get(struct ivpu_file_priv *file_priv, struct ivpu_file_priv
   static void file_priv_release(struct kref *ref)
   {
       struct ivpu_file_priv *file_priv = container_of(ref, struct ivpu_file_priv, ref);
+    struct ivpu_device *vdev = file_priv->vdev;

Why do we need this?  It doesn't appear to be used at this time.  If it is needed later in the series, add it then.

vdev is implicitly used by ivpu_dbg(). This allows us to keep messages a bit longer and cleaner.

Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +5 -0
+
+    ivpu_dbg(FILE, "file_priv release: ctx %u\n", file_priv->ctx.id);
+
+    if (file_priv->ctx.id)
+        ivpu_mmu_user_context_fini(file_priv);
         kfree(file_priv);
   }
@@ -64,6 +77,10 @@ static void file_priv_release(struct kref *ref)
   void ivpu_file_priv_put(struct ivpu_file_priv **link)
   {
Quoted 8 lines
Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +4 -0
       struct ivpu_file_priv *file_priv = *link;
+    struct ivpu_device *vdev = file_priv->vdev;
+
+    ivpu_dbg(KREF, "file_priv put: ctx %u refcount %u\n",
+         file_priv->ctx.id, kref_read(&file_priv->ref));

vdev doesn't appear to be used, and the log message doesn't appear related to $SUBJECT

vdev is implicitly used by ivpu_dbg() and file_priv->ctx.id is added in this patch.

Quoted 6 lines
Quoted 6 lines

      *link = NULL; kref_put(&file_priv->ref, file_priv_release);

drivers/gpu/drm/ivpu/ivpu_drv.c +0 -0
@@ -75,7 +92,11 @@ static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_f
       struct ivpu_device *vdev = file_priv->vdev;
       struct pci_dev *pdev = to_pci_dev(vdev->drm.dev);
       struct drm_ivpu_param *args = data;
Quoted 8 lines

drivers/gpu/drm/ivpu/ivpu_drv.c static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_f · line 99

Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +5 -1
-    int ret = 0;
+    int ret;
+
+    ret = ivpu_mmu_user_context_init(file_priv);
+    if (ret)
+        return ret;

I fail to understand why we are initializing a user context in an ioctl.  This will happen N times, for N ioctl calls that a particular user does.  Feels like something that should occur in ivpu_open().  What am I missing?

The context is lazy initialized and it will be only initialized once during first buffer related ioctl. We don't want to allocate context ids and page tables for random user processes (Xorg, chrome, etc.). Currently user space has to open /dev/dri device in order to figure out what it is so we are optimizing for this case.

Quoted 17 lines
Quoted 17 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +0 -0
         switch (args->param) {

      case DRM_IVPU_PARAM_DEVICE_ID:

drivers/gpu/drm/ivpu/ivpu_drv.c +0 -0
@@ -99,6 +120,9 @@ static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_f

      case DRM_IVPU_PARAM_CONTEXT_PRIORITY:
          args->value = file_priv->priority;
          break;
+    case DRM_IVPU_PARAM_CONTEXT_ID:
+        args->value = file_priv->ctx.id;
+        break;
      default:
          ret = -EINVAL;
      }

drivers/gpu/drm/ivpu/ivpu_drv.c +0 -1
@@ -110,7 +134,11 @@ static int ivpu_set_param_ioctl(struct drm_device *dev, void *data, struct drm_f
   {
       struct ivpu_file_priv *file_priv = file->driver_priv;
       struct drm_ivpu_param *args = data;
-    int ret = 0;
Quoted 8 lines
Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +5 -0
+    int ret;
+
+    ret = ivpu_mmu_user_context_init(file_priv);
+    if (ret)
+        return ret;

Again, initializing a context in an ioctl isn't making sense to me.

See above.

Quoted 13 lines

drivers/gpu/drm/ivpu/ivpu_drv.c static int ivpu_open(struct drm_device *dev, struct drm_file *file) · line 173

Quoted 10 lines

      switch (args->param) {
      case DRM_IVPU_PARAM_CONTEXT_PRIORITY:

drivers/gpu/drm/ivpu/ivpu_drv.c +3 -0
@@ -139,9 +167,13 @@ static int ivpu_open(struct drm_device *dev, struct drm_file *file)
       file_priv->priority = DRM_IVPU_CONTEXT_PRIORITY_NORMAL;
         kref_init(&file_priv->ref);
+    mutex_init(&file_priv->lock);
         file->driver_priv = file_priv;
   +    ivpu_dbg(FILE, "file_priv alloc: process %s pid %d\n",
+         current->comm, task_pid_nr(current));
+

As it is, this doesn't seem related to $SUBJECT and seems to belong in patch 1.

Quoted 1 line

However, this seems dubious. Trace gives you the task name and pid for the process that triggered a trace, and you don't need to add anything to your code to be able to trace any symbol.

Sure but then it would be difficult to correlate this with the rest of dbg messages.
In order to properly use ftrace we have to implement tracing in the whole driver.
This is a first iteration of the driver that supposed to be as minimal as possible to make upstreaming easy :P

Quoted 8 lines
Quoted 7 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +1 -0
       return 0;
   }
   @@ -164,6 +196,7 @@ int ivpu_shutdown(struct ivpu_device *vdev)
       int ret;
         ivpu_hw_irq_disable(vdev);
+    ivpu_mmu_disable(vdev);

        ret = ivpu_hw_power_down(vdev);

Quoted 5 lines

I'm a bit confused.  It is good practice to balance your init and cleanup (fini in this series).  Everything that is done in init, is undone in fini in the reverse order.  This makes it easy to make sure things haven't been forgotten.

I don't understand what ivpu_shutdown() is suposed to be paired with.  I thought it was ivpu_hw_power_up(), but you have power_down() in shutdown().

I can't tell what mmu_disable() is suposed to pair with.  dev_init()?

Yeah I know this is a bit confusing in this patch. ivpu_shutdown() will be paired by ivpu_boot() in patch 5. I've added ivpu_shutdown() here so next patches in the series only add new code hoping it will be easier to review :/ Still it's gonna be confusing also later on. We have different boot flows for initial driver load and runtime resume. ivpu_dev_init() cannot power up the vpu in ivpu_boot() as it needs access to registers before boot. Also MMU has to be initialized early so internal allocations can be done in page tables.
Please take a look ivpu_dev_init() and ivpu_resume(), maybe you could suggest a better approach.

Quoted 50 lines
Quoted 50 lines

      if (ret)

drivers/gpu/drm/ivpu/ivpu_drv.c +3 -0
@@ -272,6 +305,10 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
       if (!vdev->hw)
           return -ENOMEM;
   +    vdev->mmu = devm_kzalloc(vdev->drm.dev, sizeof(*vdev->mmu), GFP_KERNEL);
+    if (!vdev->mmu)
+        return -ENOMEM;
+
       vdev->hw->ops = &ivpu_hw_mtl_ops;
       vdev->platform = IVPU_PLATFORM_INVALID;
   @@ -303,8 +340,24 @@ static int ivpu_dev_init(struct ivpu_device *vdev)

          goto err_irq_fini;
      }
  +    ret = ivpu_mmu_global_context_init(vdev);
+    if (ret) {
+        ivpu_err(vdev, "Failed to initialize global MMU context: %d\n", ret); +        goto err_power_down;
+    }

drivers/gpu/drm/ivpu/ivpu_drv.c +7 -0
+
+    ret = ivpu_mmu_init(vdev);
+    if (ret) {
+        ivpu_err(vdev, "Failed to initialize MMU device: %d\n", ret);
+        goto err_mmu_gctx_fini;
+    }
+

      return 0;
  +err_mmu_gctx_fini:
+    ivpu_mmu_global_context_fini(vdev);
+err_power_down:
+    ivpu_hw_power_down(vdev);
  err_irq_fini:
      ivpu_irq_fini(vdev);
  err_pci_fini:

drivers/gpu/drm/ivpu/ivpu_drv.c +1 -0
@@ -316,6 +369,8 @@ static void ivpu_dev_fini(struct ivpu_device *vdev)
   {
       ivpu_shutdown(vdev);
   +    ivpu_mmu_fini(vdev);
+    ivpu_mmu_global_context_fini(vdev);
       ivpu_irq_fini(vdev);
       ivpu_pci_fini(vdev);

  diff --git a/drivers/gpu/drm/ivpu/ivpu_drv.h b/drivers/gpu/drm/ivpu/ivpu_drv.h index 43dfa78544c6..6eec3eb76c2f 100644

drivers/gpu/drm/ivpu/ivpu_drv.h +0 -0
@@ -14,6 +14,8 @@
   #include <linux/xarray.h>
   #include <uapi/drm/ivpu_drm.h>

  +#include "ivpu_mmu_context.h"
+
  #define DRIVER_NAME "intel_vpu"

Quoted 8 lines

drivers/gpu/drm/ivpu/ivpu_drv.h struct ivpu_wa_table { · line 74

Quoted 6 lines

  #define DRIVER_DESC "Driver for Intel Versatile Processing Unit (VPU)" #define DRIVER_DATE "20220913"

drivers/gpu/drm/ivpu/ivpu_drv.h +1 -0
@@ -70,6 +72,7 @@ struct ivpu_wa_table {
   };
     struct ivpu_hw_info;
+struct ivpu_mmu_info;

Why not just include ivpu_mmu.h?

To minimize dependencies between c files. We don't want all driver headers included in ivpu_drv.h.

Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/ivpu/ivpu_drv.h +0 -0
     struct ivpu_device {

      struct drm_device drm; /* Must be first */

drivers/gpu/drm/ivpu/ivpu_drv.h +1 -0
@@ -80,7 +83,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;
Quoted 8 lines
Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_drv.h +0 -0
       struct xa_limit context_xa_limit;
   @@ -95,6 +100,8 @@ struct ivpu_device {
   struct ivpu_file_priv {

      struct kref ref;
      struct ivpu_device *vdev;
+    struct mutex lock;

A comment indicating what this protects would be extremely useful.

OK

Quoted 34 lines
Quoted 34 lines

+    struct ivpu_mmu_context ctx;
      u32 priority;
  };
  diff --git a/drivers/gpu/drm/ivpu/ivpu_hw_mtl.c b/drivers/gpu/drm/ivpu/ivpu_hw_mtl.c index 17fc8e1104df..525b57c4029c 100644

drivers/gpu/drm/ivpu/ivpu_hw_mtl.c +9 -0
@@ -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 irqreturn_t 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))
+        ret &= ivpu_mmu_irq_evtq_handler(vdev);
+
+    if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_1_INT, status))
+        ivpu_dbg(IRQ, "MMU sync complete\n");
+
+    if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_2_INT, status))
+        ret &= ivpu_mmu_irq_gerr_handler(vdev);
+
       if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, CPU_INT_REDIRECT_0_INT, status))
           ret &= ivpu_hw_mtl_irq_wdt_mss_handler(vdev);

  diff --git a/drivers/gpu/drm/ivpu/ivpu_mmu.c b/drivers/gpu/drm/ivpu/ivpu_mmu.c new file mode 100644
index 000000000000..b9f225a83c45

drivers/gpu/drm/ivpu/ivpu_mmu.c new file +1 -0
@@ -0,0 +1,883 @@
+// SPDX-License-Identifier: GPL-2.0-only
Quoted 8 lines

drivers/gpu/drm/ivpu/ivpu_mmu.c line 7

Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_mmu.c new file +6 -0
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#include <linux/highmem.h>
+#include <linux/circ_buf.h>

Alphabetical order?

OK

Quoted 12 lines
Quoted 12 lines
drivers/gpu/drm/ivpu/ivpu_mmu.c new file +12 -0
+
+#include "ivpu_drv.h"
+#include "ivpu_hw_mtl_reg.h"
+#include "ivpu_hw_reg_io.h"
+#include "ivpu_mmu.h"
+#include "ivpu_mmu_context.h"
+
+#define IVPU_MMU_IDR0_REF        0x080f3e0f
+#define IVPU_MMU_IDR0_REF_SIMICS    0x080f3e1f
+#define IVPU_MMU_IDR1_REF        0x0e739d18
+#define IVPU_MMU_IDR3_REF        0x0000003c
+#define IVPU_MMU_IDR5_REF        0x00040070
Quoted 8 lines

drivers/gpu/drm/ivpu/ivpu_mmu.c line 25

Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_mmu.c new file +6 -0
+#define IVPU_MMU_IDR5_REF_SIMICS    0x00000075
+#define IVPU_MMU_IDR5_REF_FPGA        0x00800075
+
+#define IVPU_MMU_CDTAB_ENT_SIZE        64
+#define IVPU_MMU_CDTAB_ENT_COUNT_LOG2    8 /* 256 entries */
+#define IVPU_MMU_CDTAB_ENT_COUNT    ((u32)1 << IVPU_MMU_CDTAB_ENT_COUNT_LOG2)

Isn't this just BIT(IVPU_MMU_CDTAB_ENT_COUNT_LOG2)?

It is but shift here is more readable in my opinion.

Quoted 355 lines
Quoted 355 lines
drivers/gpu/drm/ivpu/ivpu_mmu.c new file +355 -0
+
+#define IVPU_MMU_STREAM_ID0        0
+#define IVPU_MMU_STREAM_ID3        3
+
+#define IVPU_MMU_STRTAB_ENT_SIZE    64
+#define IVPU_MMU_STRTAB_ENT_COUNT    4
+#define IVPU_MMU_STRTAB_CFG_LOG2SIZE    2
+#define IVPU_MMU_STRTAB_CFG        IVPU_MMU_STRTAB_CFG_LOG2SIZE
+
+#define IVPU_MMU_Q_COUNT_LOG2        4 /* 16 entries */
+#define IVPU_MMU_Q_COUNT        ((u32)1 << IVPU_MMU_Q_COUNT_LOG2)
+#define IVPU_MMU_Q_WRAP_BIT        (IVPU_MMU_Q_COUNT << 1)
+#define IVPU_MMU_Q_WRAP_MASK        (IVPU_MMU_Q_WRAP_BIT - 1)
+#define IVPU_MMU_Q_IDX_MASK        (IVPU_MMU_Q_COUNT - 1)
+#define IVPU_MMU_Q_IDX(val)        ((val) & IVPU_MMU_Q_IDX_MASK)
+
+#define IVPU_MMU_CMDQ_CMD_SIZE        16
+#define IVPU_MMU_CMDQ_SIZE        (IVPU_MMU_Q_COUNT * IVPU_MMU_CMDQ_CMD_SIZE)
+
+#define IVPU_MMU_EVTQ_CMD_SIZE        32
+#define IVPU_MMU_EVTQ_SIZE        (IVPU_MMU_Q_COUNT * IVPU_MMU_EVTQ_CMD_SIZE)
+
+#define IVPU_MMU_CMD_OPCODE        GENMASK(8, 0)
+
+#define IVPU_MMU_CMD_SYNC_0_CS        GENMASK(13, 12)
+#define IVPU_MMU_CMD_SYNC_0_MSH        GENMASK(23, 22)
+#define IVPU_MMU_CMD_SYNC_0_MSI_ATTR    GENMASK(27, 24)
+#define IVPU_MMU_CMD_SYNC_0_MSI_ATTR    GENMASK(27, 24)
+#define IVPU_MMU_CMD_SYNC_0_MSI_DATA    GENMASK(63, 32)
+
+#define IVPU_MMU_CMD_CFGI_0_SSEC    BIT(10)
+#define IVPU_MMU_CMD_CFGI_0_SSV        BIT(11)
+#define IVPU_MMU_CMD_CFGI_0_SSID    GENMASK(31, 12)
+#define IVPU_MMU_CMD_CFGI_0_SID        GENMASK(63, 32)
+#define IVPU_MMU_CMD_CFGI_1_RANGE    GENMASK(4, 0)
+
+#define IVPU_MMU_CMD_TLBI_0_ASID    GENMASK(63, 48)
+#define IVPU_MMU_CMD_TLBI_0_VMID    GENMASK(47, 32)
+
+#define CMD_PREFETCH_CFG        0x1
+#define CMD_CFGI_STE            0x3
+#define CMD_CFGI_ALL            0x4
+#define CMD_CFGI_CD            0x5
+#define CMD_CFGI_CD_ALL            0x6
+#define CMD_TLBI_NH_ASID        0x11
+#define CMD_TLBI_EL2_ALL        0x20
+#define CMD_TLBI_NSNH_ALL        0x30
+#define CMD_SYNC            0x46
+
+#define IVPU_MMU_EVT_F_UUT        0x01
+#define IVPU_MMU_EVT_C_BAD_STREAMID    0x02
+#define IVPU_MMU_EVT_F_STE_FETCH    0x03
+#define IVPU_MMU_EVT_C_BAD_STE        0x04
+#define IVPU_MMU_EVT_F_BAD_ATS_TREQ    0x05
+#define IVPU_MMU_EVT_F_STREAM_DISABLED    0x06
+#define IVPU_MMU_EVT_F_TRANSL_FORBIDDEN    0x07
+#define IVPU_MMU_EVT_C_BAD_SUBSTREAMID    0x08
+#define IVPU_MMU_EVT_F_CD_FETCH        0x09
+#define IVPU_MMU_EVT_C_BAD_CD        0x0a
+#define IVPU_MMU_EVT_F_WALK_EABT    0x0b
+#define IVPU_MMU_EVT_F_TRANSLATION    0x10
+#define IVPU_MMU_EVT_F_ADDR_SIZE    0x11
+#define IVPU_MMU_EVT_F_ACCESS        0x12
+#define IVPU_MMU_EVT_F_PERMISSION    0x13
+#define IVPU_MMU_EVT_F_TLB_CONFLICT    0x20
+#define IVPU_MMU_EVT_F_CFG_CONFLICT    0x21
+#define IVPU_MMU_EVT_E_PAGE_REQUEST    0x24
+#define IVPU_MMU_EVT_F_VMS_FETCH    0x25
+
+#define IVPU_MMU_EVTS_MAX        8
+
+#define IVPU_MMU_EVT_OP_MASK        GENMASK_ULL(7, 0)
+#define IVPU_MMU_EVT_SSID_MASK        GENMASK_ULL(31, 12)
+
+#define IVPU_MMU_Q_BASE_RWA        BIT(62)
+#define IVPU_MMU_Q_BASE_ADDR_MASK    GENMASK_ULL(51, 5)
+#define IVPU_MMU_STRTAB_BASE_RA        BIT(62)
+#define IVPU_MMU_STRTAB_BASE_ADDR_MASK    GENMASK_ULL(51, 6)
+
+#define IVPU_MMU_IRQ_EVTQ_EN        BIT(2)
+#define IVPU_MMU_IRQ_GERROR_EN        BIT(0)
+
+#define IVPU_MMU_CR0_ATSCHK        BIT(4)
+#define IVPU_MMU_CR0_CMDQEN        BIT(3)
+#define IVPU_MMU_CR0_EVTQEN        BIT(2)
+#define IVPU_MMU_CR0_PRIQEN        BIT(1)
+#define IVPU_MMU_CR0_SMMUEN        BIT(0)
+
+#define IVPU_MMU_CR1_TABLE_SH        GENMASK(11, 10)
+#define IVPU_MMU_CR1_TABLE_OC        GENMASK(9, 8)
+#define IVPU_MMU_CR1_TABLE_IC        GENMASK(7, 6)
+#define IVPU_MMU_CR1_QUEUE_SH        GENMASK(5, 4)
+#define IVPU_MMU_CR1_QUEUE_OC        GENMASK(3, 2)
+#define IVPU_MMU_CR1_QUEUE_IC        GENMASK(1, 0)
+#define IVPU_MMU_CACHE_NC        0
+#define IVPU_MMU_CACHE_WB        1
+#define IVPU_MMU_CACHE_WT        2
+#define IVPU_MMU_SH_NSH            0
+#define IVPU_MMU_SH_OSH            2
+#define IVPU_MMU_SH_ISH            3
+
+#define IVPU_MMU_CMDQ_OP        GENMASK_ULL(7, 0)
+
+#define IVPU_MMU_CD_0_TCR_T0SZ        GENMASK_ULL(5, 0)
+#define IVPU_MMU_CD_0_TCR_TG0        GENMASK_ULL(7, 6)
+#define IVPU_MMU_CD_0_TCR_IRGN0        GENMASK_ULL(9, 8)
+#define IVPU_MMU_CD_0_TCR_ORGN0        GENMASK_ULL(11, 10)
+#define IVPU_MMU_CD_0_TCR_SH0        GENMASK_ULL(13, 12)
+#define IVPU_MMU_CD_0_TCR_EPD0        BIT_ULL(14)
+#define IVPU_MMU_CD_0_TCR_EPD1        BIT_ULL(30)
+#define IVPU_MMU_CD_0_ENDI        BIT(15)
+#define IVPU_MMU_CD_0_V            BIT(31)
+#define IVPU_MMU_CD_0_TCR_IPS        GENMASK_ULL(34, 32)
+#define IVPU_MMU_CD_0_TCR_TBI0        BIT_ULL(38)
+#define IVPU_MMU_CD_0_AA64        BIT(41)
+#define IVPU_MMU_CD_0_S            BIT(44)
+#define IVPU_MMU_CD_0_R            BIT(45)
+#define IVPU_MMU_CD_0_A            BIT(46)
+#define IVPU_MMU_CD_0_ASET        BIT(47)
+#define IVPU_MMU_CD_0_ASID        GENMASK_ULL(63, 48)
+
+#define IVPU_MMU_CD_1_TTB0_MASK        GENMASK_ULL(51, 4)
+
+#define IVPU_MMU_STE_0_S1CDMAX        GENMASK_ULL(63, 59)
+#define IVPU_MMU_STE_0_S1FMT        GENMASK_ULL(5, 4)
+#define IVPU_MMU_STE_0_S1FMT_LINEAR    0
+#define IVPU_MMU_STE_DWORDS        8
+#define IVPU_MMU_STE_0_CFG_S1_TRANS    5
+#define IVPU_MMU_STE_0_CFG        GENMASK_ULL(3, 1)
+#define IVPU_MMU_STE_0_S1CTXPTR_MASK    GENMASK_ULL(51, 6)
+#define IVPU_MMU_STE_0_V            BIT(0)
+
+#define IVPU_MMU_STE_1_STRW_NSEL1    0ul
+#define IVPU_MMU_STE_1_CONT        GENMASK_ULL(16, 13)
+#define IVPU_MMU_STE_1_STRW        GENMASK_ULL(31, 30)
+#define IVPU_MMU_STE_1_PRIVCFG        GENMASK_ULL(49, 48)
+#define IVPU_MMU_STE_1_PRIVCFG_UNPRIV    2ul
+#define IVPU_MMU_STE_1_INSTCFG        GENMASK_ULL(51, 50)
+#define IVPU_MMU_STE_1_INSTCFG_DATA    2ul
+#define IVPU_MMU_STE_1_MEV        BIT(19)
+#define IVPU_MMU_STE_1_S1STALLD        BIT(27)
+#define IVPU_MMU_STE_1_S1C_CACHE_NC    0ul
+#define IVPU_MMU_STE_1_S1C_CACHE_WBRA    1ul
+#define IVPU_MMU_STE_1_S1C_CACHE_WT    2ul
+#define IVPU_MMU_STE_1_S1C_CACHE_WB    3ul
+#define IVPU_MMU_STE_1_S1CIR        GENMASK_ULL(3, 2)
+#define IVPU_MMU_STE_1_S1COR        GENMASK_ULL(5, 4)
+#define IVPU_MMU_STE_1_S1CSH        GENMASK_ULL(7, 6)
+#define IVPU_MMU_STE_1_S1DSS        GENMASK_ULL(1, 0)
+#define IVPU_MMU_STE_1_S1DSS_TERMINATE    0x0
+
+#define IVPU_MMU_REG_TIMEOUT_US        (10 * USEC_PER_MSEC)
+#define IVPU_MMU_QUEUE_TIMEOUT_US    (100 * USEC_PER_MSEC)
+
+#define IVPU_MMU_GERROR_ERR_MASK ((REG_FLD(MTL_VPU_HOST_MMU_GERROR, CMDQ)) | \
+                  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, EVTQ_ABT)) | \
+                  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, PRIQ_ABT)) | \
+                  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_CMDQ_ABT)) | \
+                  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_EVTQ_ABT)) | \
+                  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_PRIQ_ABT)) | \
+                  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_ABT)) | \
+                  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, SFM)))
+
+static char *ivpu_mmu_evt_to_str(u32 cmd)
+{
+    switch (cmd) {
+    case IVPU_MMU_EVT_F_UUT:
+        return "Unsupported Upstream Transaction";
+    case IVPU_MMU_EVT_C_BAD_STREAMID:
+        return "Transaction StreamID out of range";
+    case IVPU_MMU_EVT_F_STE_FETCH:
+        return "Fetch of STE caused external abort";
+    case IVPU_MMU_EVT_C_BAD_STE:
+        return "Used STE invalid";
+    case IVPU_MMU_EVT_F_BAD_ATS_TREQ:
+        return "Address Request disallowed for a StreamID";
+    case IVPU_MMU_EVT_F_STREAM_DISABLED:
+        return "Transaction marks non-substream disabled";
+    case IVPU_MMU_EVT_F_TRANSL_FORBIDDEN:
+        return "MMU bypass is disallowed for this StreamID";
+    case IVPU_MMU_EVT_C_BAD_SUBSTREAMID:
+        return "Invalid StreamID";
+    case IVPU_MMU_EVT_F_CD_FETCH:
+        return "Fetch of CD caused external abort";
+    case IVPU_MMU_EVT_C_BAD_CD:
+        return "Fetched CD invalid";
+    case IVPU_MMU_EVT_F_WALK_EABT:
+        return " An external abort occurred fetching a TLB";
+    case IVPU_MMU_EVT_F_TRANSLATION:
+        return "Translation fault";
+    case IVPU_MMU_EVT_F_ADDR_SIZE:
+        return " Output address caused address size fault";
+    case IVPU_MMU_EVT_F_ACCESS:
+        return "Access flag fault";
+    case IVPU_MMU_EVT_F_PERMISSION:
+        return "Permission fault occurred on page access";
+    case IVPU_MMU_EVT_F_TLB_CONFLICT:
+        return "A TLB conflict";
+    case IVPU_MMU_EVT_F_CFG_CONFLICT:
+        return "A configuration cache conflict";
+    case IVPU_MMU_EVT_E_PAGE_REQUEST:
+        return "Page request hint from a client device";
+    case IVPU_MMU_EVT_F_VMS_FETCH:
+        return "Fetch of VMS caused external abort";
+    default:
+        return "Unknown CMDQ command";
+    }
+}
+
+static int ivpu_mmu_config_check(struct ivpu_device *vdev)
+{
+    u32 val_ref;
+    u32 val;
+
+    if (ivpu_is_simics(vdev))
+        val_ref = IVPU_MMU_IDR0_REF_SIMICS;
+    else
+        val_ref = IVPU_MMU_IDR0_REF;
+
+    val = REGV_RD32(MTL_VPU_HOST_MMU_IDR0);
+    if (val != val_ref)
+        ivpu_err(vdev, "IDR0 0x%x != IDR0_REF 0x%x\n", val, val_ref);
+
+    val = REGV_RD32(MTL_VPU_HOST_MMU_IDR1);
+    if (val != IVPU_MMU_IDR1_REF)
+        ivpu_warn(vdev, "IDR1 0x%x != IDR1_REF 0x%x\n", val, IVPU_MMU_IDR1_REF);
+
+    val = REGV_RD32(MTL_VPU_HOST_MMU_IDR3);
+    if (val != IVPU_MMU_IDR3_REF)
+        ivpu_warn(vdev, "IDR3 0x%x != IDR3_REF 0x%x\n", val, IVPU_MMU_IDR3_REF);
+
+    if (ivpu_is_simics(vdev))
+        val_ref = IVPU_MMU_IDR5_REF_SIMICS;
+    else if (ivpu_is_fpga(vdev))
+        val_ref = IVPU_MMU_IDR5_REF_FPGA;
+    else
+        val_ref = IVPU_MMU_IDR5_REF;
+
+    val = REGV_RD32(MTL_VPU_HOST_MMU_IDR5);
+    if (val != val_ref)
+        ivpu_warn(vdev, "IDR5 0x%x != IDR5_REF 0x%x\n", val, val_ref);
+
+    return 0;
+}
+
+static int ivpu_mmu_cdtab_alloc(struct ivpu_device *vdev)
+{
+    struct ivpu_mmu_info *mmu = vdev->mmu;
+    struct ivpu_mmu_cdtab *cdtab = &mmu->cdtab;
+    size_t size = IVPU_MMU_CDTAB_ENT_COUNT * IVPU_MMU_CDTAB_ENT_SIZE;
+
+    cdtab->base = dmam_alloc_coherent(vdev->drm.dev, size, &cdtab->dma, GFP_KERNEL);
+    if (!cdtab->base)
+        return -ENOMEM;
+
+    ivpu_dbg(MMU, "CDTAB alloc: dma=%pad size=%zu\n", &cdtab->dma, size);
+
+    return 0;
+}
+
+static int ivpu_mmu_strtab_alloc(struct ivpu_device *vdev)
+{
+    struct ivpu_mmu_info *mmu = vdev->mmu;
+    struct ivpu_mmu_strtab *strtab = &mmu->strtab;
+    size_t size = IVPU_MMU_STRTAB_ENT_COUNT * IVPU_MMU_STRTAB_ENT_SIZE;
+
+    strtab->base = dmam_alloc_coherent(vdev->drm.dev, size, &strtab->dma, GFP_KERNEL);
+    if (!strtab->base)
+        return -ENOMEM;
+
+    strtab->base_cfg = IVPU_MMU_STRTAB_CFG;
+    strtab->dma_q = IVPU_MMU_STRTAB_BASE_RA;
+    strtab->dma_q |= strtab->dma & IVPU_MMU_STRTAB_BASE_ADDR_MASK;
+
+    ivpu_dbg(MMU, "STRTAB alloc: dma=%pad dma_q=%pad size=%zu\n",
+         &strtab->dma, &strtab->dma_q, size);
+
+    return 0;
+}
+
+static int ivpu_mmu_cmdq_alloc(struct ivpu_device *vdev)
+{
+    struct ivpu_mmu_info *mmu = vdev->mmu;
+    struct ivpu_mmu_queue *q = &mmu->cmdq;
+
+    q->base = dmam_alloc_coherent(vdev->drm.dev, IVPU_MMU_CMDQ_SIZE, &q->dma, GFP_KERNEL);
+    if (!q->base)
+        return -ENOMEM;
+
+    q->dma_q = IVPU_MMU_Q_BASE_RWA;
+    q->dma_q |= q->dma & IVPU_MMU_Q_BASE_ADDR_MASK;
+    q->dma_q |= IVPU_MMU_Q_COUNT_LOG2;
+
+    ivpu_dbg(MMU, "CMDQ alloc: dma=%pad dma_q=%pad size=%u\n",
+         &q->dma, &q->dma_q, IVPU_MMU_CMDQ_SIZE);
+
+    return 0;
+}
+
+static int ivpu_mmu_evtq_alloc(struct ivpu_device *vdev)
+{
+    struct ivpu_mmu_info *mmu = vdev->mmu;
+    struct ivpu_mmu_queue *q = &mmu->evtq;
+
+    q->base = dmam_alloc_coherent(vdev->drm.dev, IVPU_MMU_EVTQ_SIZE, &q->dma, GFP_KERNEL);
+    if (!q->base)
+        return -ENOMEM;
+
+    q->dma_q = IVPU_MMU_Q_BASE_RWA;
+    q->dma_q |= q->dma & IVPU_MMU_Q_BASE_ADDR_MASK;
+    q->dma_q |= IVPU_MMU_Q_COUNT_LOG2;
+
+    ivpu_dbg(MMU, "EVTQ alloc: dma=%pad dma_q=%pad size=%u\n",
+         &q->dma, &q->dma_q, IVPU_MMU_EVTQ_SIZE);
+
+    return 0;
+}
+
+static int ivpu_mmu_structs_alloc(struct ivpu_device *vdev)
+{
+    int ret;
+
+    ret = ivpu_mmu_cdtab_alloc(vdev);
+    if (ret) {
+        ivpu_err(vdev, "Failed to allocate cdtab: %d\n", ret);
+        return ret;
+    }
+
+    ret = ivpu_mmu_strtab_alloc(vdev);
+    if (ret) {
+        ivpu_err(vdev, "Failed to allocate strtab: %d\n", ret);
+        return ret;
+    }
+
+    ret = ivpu_mmu_cmdq_alloc(vdev);
+    if (ret) {
+        ivpu_err(vdev, "Failed to allocate cmdq: %d\n", ret);
+        return ret;
+    }
+
+    ret = ivpu_mmu_evtq_alloc(vdev);
+    if (ret)
+        ivpu_err(vdev, "Failed to allocate evtq: %d\n", ret);
+
+    return ret;
+}
+
+static int ivpu_mmu_reg_write(struct ivpu_device *vdev, u32 reg, u32 val)
+{
+    u32 reg_ack = reg + 4; /* ACK register is 4B after base register */
+    u32 val_ack;
+    int ret;
+
+    REGV_WR32(reg, val);
+
Quoted 8 lines

drivers/gpu/drm/ivpu/ivpu_mmu.c line 386

Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_mmu.c new file +6 -0
+    ret = REGV_POLL(reg_ack, val_ack, (val == val_ack), IVPU_MMU_REG_TIMEOUT_US);
+    if (ret)
+        ivpu_err(vdev, "Failed to write register 0x%x\n", reg);
+
+    return ret;
+}

It really seems like you are reinventing regmap.  Any particular reason that mechanism doesn't work for you?

REGV_POLL() is actually just calling read_poll_timeout(), same as regmap, no reinvention here, just iopoll reuse. We don't need most of regmap features (locking, caching, etc) so I don't think is worth using it in our case.

Quoted 1 line

Also, given that this is a PCI device, I'm suprised there is no 0xFFFFFFFF handling.

We have some explicit handling of 0xFFFFFFFF (REG_IO_ERROR in the whole patchset) but it may be too little. I will review register reads and add handling where it seems to be needed.

Regards,
Jacek

#14 lore raw

Re: [PATCH v3 2/7] drm/ivpu: Add Intel VPU MMU support

Quoted 96 lines · Jacek Lawrynowicz

Hi,

Thanks for an in-depth review. My responses are inline.

Quoted 5 lines · Jeffrey Hugo
Quoted 2 lines · Jacek Lawrynowicz

VPU Memory Management Unit is based on ARM MMU-600.
It allows to create multiple virtual address spaces for the device and

"It allows the creation of"?

OK

Quoted 22 lines
Quoted 14 lines

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.

Quoted 7 lines

This patch is has to 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

So, this doesn't look valid.  This indicates that Karol wrote this patch, then it was handed off to Krystian, and then to you.  However, if that were the case, then the patch should be from Karol (which is then the author in git).  It is not.  It is from you.

What I suspect is happening is that the three of you all co-authroed this change.  In that case, there should probably be Co-developed-by tags in addition to SOB to make that clear.

Also, I missed this on the first patch I reviewed eariler, but it looks to have the same issue.

OK, I will change this in all the patches.

Quoted 44 lines
Quoted 44 lines
---
   drivers/gpu/drm/ivpu/Makefile           |   4 +-
   drivers/gpu/drm/ivpu/ivpu_drv.c         |  59 +-
   drivers/gpu/drm/ivpu/ivpu_drv.h         |   7 +
   drivers/gpu/drm/ivpu/ivpu_hw_mtl.c      |  10 +
   drivers/gpu/drm/ivpu/ivpu_mmu.c         | 883 ++++++++++++++++++++++++
   drivers/gpu/drm/ivpu/ivpu_mmu.h         |  53 ++
   drivers/gpu/drm/ivpu/ivpu_mmu_context.c | 419 +++++++++++
   drivers/gpu/drm/ivpu/ivpu_mmu_context.h |  49 ++
   include/uapi/drm/ivpu_drm.h             |   4 +
   9 files changed, 1485 insertions(+), 3 deletions(-)

  create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu_context.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu_context.h

drivers/gpu/drm/ivpu/Makefile +3 -1
index e59dc65abe6a..95bb04f26296 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_IVPU) += intel_vpu.o
drivers/gpu/drm/ivpu/ivpu_drv.c +4 -0
index a01c7244f6e5..cbeb9a801a31 100644
@@ -14,6 +14,8 @@
     #include "ivpu_drv.h"
   #include "ivpu_hw.h"
+#include "ivpu_mmu.h"
+#include "ivpu_mmu_context.h"
     #ifndef DRIVER_VERSION_STR
   #define DRIVER_VERSION_STR __stringify(DRM_IVPU_DRIVER_MAJOR) "." \
@@ -50,6 +52,11 @@ char *ivpu_platform_to_str(u32 platform)
     void ivpu_file_priv_get(struct ivpu_file_priv *file_priv, struct ivpu_file_priv **link)
   {
+    struct ivpu_device *vdev = file_priv->vdev;
+
Quoted 8 lines

drivers/gpu/drm/ivpu/ivpu_drv.c char *ivpu_platform_to_str(u32 platform) · line 62

Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +3 -0
+    ivpu_dbg(KREF, "file_priv get: ctx %u refcount %u\n",
+         file_priv->ctx.id, kref_read(&file_priv->ref));
+
       kref_get(&file_priv->ref);
       *link = file_priv;
   }

This hunk doesn't appear related to $SUBJECT.  It looks like it would fit better as part of patch 1.

Quoted 2 lines

file_priv->ctx is added in this patch and without context id this message does not make sense so this patch is the earliest the message can be printed.

I see

Quoted 10 lines
Quoted 8 lines

drivers/gpu/drm/ivpu/ivpu_drv.c void ivpu_file_priv_get(struct ivpu_file_priv *file_priv, struct ivpu_file_priv · line 67

Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +1 -0
@@ -57,6 +64,12 @@ void ivpu_file_priv_get(struct ivpu_file_priv *file_priv, struct ivpu_file_priv
   static void file_priv_release(struct kref *ref)
   {
       struct ivpu_file_priv *file_priv = container_of(ref, struct ivpu_file_priv, ref);
+    struct ivpu_device *vdev = file_priv->vdev;

Why do we need this?  It doesn't appear to be used at this time.  If it is needed later in the series, add it then.

vdev is implicitly used by ivpu_dbg(). This allows us to keep messages a bit longer and cleaner.

Violates coding-style -

12) Macros, Enums and RTL
-------------------------
.
.
Things to avoid when using macros:
.
.
2) macros that depend on having a local variable with a magic name:

.. code-block:: c

	#define FOO(val) bar(index, val)

might look like a good thing, but it's confusing as hell when one reads the code and it's prone to breakage from seemingly innocent changes.

Quoted 37 lines
Quoted 11 lines
Quoted 11 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +5 -0
+
+    ivpu_dbg(FILE, "file_priv release: ctx %u\n", file_priv->ctx.id);
+
+    if (file_priv->ctx.id)
+        ivpu_mmu_user_context_fini(file_priv);
         kfree(file_priv);
   }
@@ -64,6 +77,10 @@ static void file_priv_release(struct kref *ref)
   void ivpu_file_priv_put(struct ivpu_file_priv **link)
   {
Quoted 8 lines

drivers/gpu/drm/ivpu/ivpu_drv.c static void file_priv_release(struct kref *ref) · line 84

Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +4 -0
       struct ivpu_file_priv *file_priv = *link;
+    struct ivpu_device *vdev = file_priv->vdev;
+
+    ivpu_dbg(KREF, "file_priv put: ctx %u refcount %u\n",
+         file_priv->ctx.id, kref_read(&file_priv->ref));
 

vdev doesn't appear to be used, and the log message doesn't appear related to $SUBJECT

vdev is implicitly used by ivpu_dbg() and file_priv->ctx.id is added in this patch.

Quoted 6 lines
Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +0 -0
       *link = NULL;
       kref_put(&file_priv->ref, file_priv_release);
@@ -75,7 +92,11 @@ static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_f
       struct ivpu_device *vdev = file_priv->vdev;
       struct pci_dev *pdev = to_pci_dev(vdev->drm.dev);
       struct drm_ivpu_param *args = data;
Quoted 8 lines

drivers/gpu/drm/ivpu/ivpu_drv.c static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_f · line 99

Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +5 -1
-    int ret = 0;
+    int ret;
+
+    ret = ivpu_mmu_user_context_init(file_priv);
+    if (ret)
+        return ret;

I fail to understand why we are initializing a user context in an ioctl.  This will happen N times, for N ioctl calls that a particular user does.  Feels like something that should occur in ivpu_open().  What am I missing?

Quoted 3 lines

The context is lazy initialized and it will be only initialized once during first buffer related ioctl. We don't want to allocate context ids and page tables for random user processes (Xorg, chrome, etc.). Currently user space has to open /dev/dri device in order to figure out what it is so we are optimizing for this case.

Hmm. So, I haven't suggested this yet, but this driver looks like a good candidate for the newly proposed accel subsystem. Since that subsystem has different devnodes, it will probably address this concern.

Regardless, is there an actual observed performance hit or other downside to this, or was this just something that was optimized for the sake of being optimal?

Quoted 42 lines
Quoted 18 lines
Quoted 18 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +3 -1
         switch (args->param) {
       case DRM_IVPU_PARAM_DEVICE_ID:
@@ -99,6 +120,9 @@ static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_f
       case DRM_IVPU_PARAM_CONTEXT_PRIORITY:
           args->value = file_priv->priority;
           break;
+    case DRM_IVPU_PARAM_CONTEXT_ID:
+        args->value = file_priv->ctx.id;
+        break;
       default:
           ret = -EINVAL;
       }
@@ -110,7 +134,11 @@ static int ivpu_set_param_ioctl(struct drm_device *dev, void *data, struct drm_f
   {
       struct ivpu_file_priv *file_priv = file->driver_priv;
       struct drm_ivpu_param *args = data;
-    int ret = 0;
Quoted 8 lines

drivers/gpu/drm/ivpu/ivpu_drv.c static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_f · line 100

Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +5 -0
+    int ret;
+
+    ret = ivpu_mmu_user_context_init(file_priv);
+    if (ret)
+        return ret;
 

Again, initializing a context in an ioctl isn't making sense to me.

See above.

Quoted 13 lines

drivers/gpu/drm/ivpu/ivpu_drv.c static int ivpu_open(struct drm_device *dev, struct drm_file *file) · line 173

Quoted 10 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +3 -0
       switch (args->param) {
       case DRM_IVPU_PARAM_CONTEXT_PRIORITY:
@@ -139,9 +167,13 @@ static int ivpu_open(struct drm_device *dev, struct drm_file *file)
       file_priv->priority = DRM_IVPU_CONTEXT_PRIORITY_NORMAL;
         kref_init(&file_priv->ref);
+    mutex_init(&file_priv->lock);
         file->driver_priv = file_priv;
   +    ivpu_dbg(FILE, "file_priv alloc: process %s pid %d\n",
+         current->comm, task_pid_nr(current));
+

As it is, this doesn't seem related to $SUBJECT and seems to belong in patch 1.

Quoted 5 lines
Quoted 1 line

However, this seems dubious. Trace gives you the task name and pid for the process that triggered a trace, and you don't need to add anything to your code to be able to trace any symbol.

Sure but then it would be difficult to correlate this with the rest of dbg messages.
In order to properly use ftrace we have to implement tracing in the whole driver.
This is a first iteration of the driver that supposed to be as minimal as possible to make upstreaming easy :P

You could remove all the debug info if "minimal" is the goal :P

I see improvements listed in the TODO, so I guess thats fair.

Quoted 13 lines
Quoted 13 lines
Quoted 8 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +1 -0
       return 0;
   }
   @@ -164,6 +196,7 @@ int ivpu_shutdown(struct ivpu_device *vdev)
       int ret;
         ivpu_hw_irq_disable(vdev);
+    ivpu_mmu_disable(vdev);

        ret = ivpu_hw_power_down(vdev);

I'm a bit confused.  It is good practice to balance your init and cleanup (fini in this series).  Everything that is done in init, is undone in fini in the reverse order.  This makes it easy to make sure things haven't been forgotten.

I don't understand what ivpu_shutdown() is suposed to be paired with.  I thought it was ivpu_hw_power_up(), but you have power_down() in shutdown().

Quoted 8 lines
Quoted 1 line

I can't tell what mmu_disable() is suposed to pair with.  dev_init()?

Yeah I know this is a bit confusing in this patch. ivpu_shutdown() will be paired by ivpu_boot() in patch 5. I've added ivpu_shutdown() here so next patches in the series only add new code hoping it will be easier to review :/ Still it's gonna be confusing also later on. We have different boot flows for initial driver load and runtime resume. ivpu_dev_init() cannot power up the vpu in ivpu_boot() as it needs access to registers before boot. Also MMU has to be initialized early so internal allocations can be done in page tables.
Please take a look ivpu_dev_init() and ivpu_resume(), maybe you could suggest a better approach.

I went an looked at the final code result at the end of the series, and it doesn't look too bad. I'm not seeing an easy alternative. A few comments stating in code what you just explained about why things are how they are as little helper pointers to the reader might be good.

Quoted 523 lines
Quoted 51 lines
Quoted 51 lines

      if (ret)

drivers/gpu/drm/ivpu/ivpu_drv.c +18 -0
@@ -272,6 +305,10 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
       if (!vdev->hw)
           return -ENOMEM;
   +    vdev->mmu = devm_kzalloc(vdev->drm.dev, sizeof(*vdev->mmu), GFP_KERNEL);
+    if (!vdev->mmu)
+        return -ENOMEM;
+
       vdev->hw->ops = &ivpu_hw_mtl_ops;
       vdev->platform = IVPU_PLATFORM_INVALID;
   @@ -303,8 +340,24 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
           goto err_irq_fini;
       }
   +    ret = ivpu_mmu_global_context_init(vdev);
+    if (ret) {
+        ivpu_err(vdev, "Failed to initialize global MMU context: %d\n", ret);
+        goto err_power_down;
+    }
+
+    ret = ivpu_mmu_init(vdev);
+    if (ret) {
+        ivpu_err(vdev, "Failed to initialize MMU device: %d\n", ret);
+        goto err_mmu_gctx_fini;
+    }
+
       return 0;
   +err_mmu_gctx_fini:
+    ivpu_mmu_global_context_fini(vdev);
+err_power_down:
+    ivpu_hw_power_down(vdev);
   err_irq_fini:
       ivpu_irq_fini(vdev);
   err_pci_fini:
@@ -316,6 +369,8 @@ static void ivpu_dev_fini(struct ivpu_device *vdev)
   {
       ivpu_shutdown(vdev);
   +    ivpu_mmu_fini(vdev);
+    ivpu_mmu_global_context_fini(vdev);
       ivpu_irq_fini(vdev);
       ivpu_pci_fini(vdev);
   diff --git a/drivers/gpu/drm/ivpu/ivpu_drv.h b/drivers/gpu/drm/ivpu/ivpu_drv.h

index 43dfa78544c6..6eec3eb76c2f 100644

drivers/gpu/drm/ivpu/ivpu_drv.h +1 -0
@@ -14,6 +14,8 @@
   #include <linux/xarray.h>
   #include <uapi/drm/ivpu_drm.h>
   +#include "ivpu_mmu_context.h"
+
   #define DRIVER_NAME "intel_vpu"
Quoted 8 lines

drivers/gpu/drm/ivpu/ivpu_drv.h struct ivpu_wa_table { · line 74

Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_drv.h +1 -0
   #define DRIVER_DESC "Driver for Intel Versatile Processing Unit (VPU)"
   #define DRIVER_DATE "20220913"
@@ -70,6 +72,7 @@ struct ivpu_wa_table {
   };
     struct ivpu_hw_info;
+struct ivpu_mmu_info;

Why not just include ivpu_mmu.h?

To minimize dependencies between c files. We don't want all driver headers included in ivpu_drv.h.

Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/ivpu/ivpu_drv.h +1 -0
     struct ivpu_device {
       struct drm_device drm; /* Must be first */
@@ -80,7 +83,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;
Quoted 8 lines

drivers/gpu/drm/ivpu/ivpu_drv.h struct ivpu_device { · line 103

Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_drv.h +1 -0
       struct xa_limit context_xa_limit;
   @@ -95,6 +100,8 @@ struct ivpu_device {
   struct ivpu_file_priv {
       struct kref ref;
       struct ivpu_device *vdev;
+    struct mutex lock;

A comment indicating what this protects would be extremely useful.

OK

Quoted 34 lines
Quoted 34 lines

+    struct ivpu_mmu_context ctx;
      u32 priority;
  };
  diff --git a/drivers/gpu/drm/ivpu/ivpu_hw_mtl.c b/drivers/gpu/drm/ivpu/ivpu_hw_mtl.c
index 17fc8e1104df..525b57c4029c 100644

drivers/gpu/drm/ivpu/ivpu_hw_mtl.c +9 -0
@@ -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 irqreturn_t 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))
+        ret &= ivpu_mmu_irq_evtq_handler(vdev);
+
+    if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_1_INT, status))
+        ivpu_dbg(IRQ, "MMU sync complete\n");
+
+    if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_2_INT, status))
+        ret &= ivpu_mmu_irq_gerr_handler(vdev);
+
       if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, CPU_INT_REDIRECT_0_INT, status))
           ret &= ivpu_hw_mtl_irq_wdt_mss_handler(vdev);
   diff --git a/drivers/gpu/drm/ivpu/ivpu_mmu.c b/drivers/gpu/drm/ivpu/ivpu_mmu.c

new file mode 100644
index 000000000000..b9f225a83c45

drivers/gpu/drm/ivpu/ivpu_mmu.c new file +1 -0
@@ -0,0 +1,883 @@
+// SPDX-License-Identifier: GPL-2.0-only
Quoted 8 lines

drivers/gpu/drm/ivpu/ivpu_mmu.c line 7

Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_mmu.c new file +6 -0
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#include <linux/highmem.h>
+#include <linux/circ_buf.h>

Alphabetical order?

OK

Quoted 12 lines
Quoted 12 lines
drivers/gpu/drm/ivpu/ivpu_mmu.c new file +12 -0
+
+#include "ivpu_drv.h"
+#include "ivpu_hw_mtl_reg.h"
+#include "ivpu_hw_reg_io.h"
+#include "ivpu_mmu.h"
+#include "ivpu_mmu_context.h"
+
+#define IVPU_MMU_IDR0_REF        0x080f3e0f
+#define IVPU_MMU_IDR0_REF_SIMICS    0x080f3e1f
+#define IVPU_MMU_IDR1_REF        0x0e739d18
+#define IVPU_MMU_IDR3_REF        0x0000003c
+#define IVPU_MMU_IDR5_REF        0x00040070
Quoted 8 lines

drivers/gpu/drm/ivpu/ivpu_mmu.c line 25

Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_mmu.c new file +6 -0
+#define IVPU_MMU_IDR5_REF_SIMICS    0x00000075
+#define IVPU_MMU_IDR5_REF_FPGA        0x00800075
+
+#define IVPU_MMU_CDTAB_ENT_SIZE        64
+#define IVPU_MMU_CDTAB_ENT_COUNT_LOG2    8 /* 256 entries */
+#define IVPU_MMU_CDTAB_ENT_COUNT    ((u32)1 << IVPU_MMU_CDTAB_ENT_COUNT_LOG2)

Isn't this just BIT(IVPU_MMU_CDTAB_ENT_COUNT_LOG2)?

It is but shift here is more readable in my opinion.

Quoted 355 lines
Quoted 355 lines
drivers/gpu/drm/ivpu/ivpu_mmu.c new file +355 -0
+
+#define IVPU_MMU_STREAM_ID0        0
+#define IVPU_MMU_STREAM_ID3        3
+
+#define IVPU_MMU_STRTAB_ENT_SIZE    64
+#define IVPU_MMU_STRTAB_ENT_COUNT    4
+#define IVPU_MMU_STRTAB_CFG_LOG2SIZE    2
+#define IVPU_MMU_STRTAB_CFG        IVPU_MMU_STRTAB_CFG_LOG2SIZE
+
+#define IVPU_MMU_Q_COUNT_LOG2        4 /* 16 entries */
+#define IVPU_MMU_Q_COUNT        ((u32)1 << IVPU_MMU_Q_COUNT_LOG2)
+#define IVPU_MMU_Q_WRAP_BIT        (IVPU_MMU_Q_COUNT << 1)
+#define IVPU_MMU_Q_WRAP_MASK        (IVPU_MMU_Q_WRAP_BIT - 1)
+#define IVPU_MMU_Q_IDX_MASK        (IVPU_MMU_Q_COUNT - 1)
+#define IVPU_MMU_Q_IDX(val)        ((val) & IVPU_MMU_Q_IDX_MASK)
+
+#define IVPU_MMU_CMDQ_CMD_SIZE        16
+#define IVPU_MMU_CMDQ_SIZE        (IVPU_MMU_Q_COUNT * IVPU_MMU_CMDQ_CMD_SIZE)
+
+#define IVPU_MMU_EVTQ_CMD_SIZE        32
+#define IVPU_MMU_EVTQ_SIZE        (IVPU_MMU_Q_COUNT * IVPU_MMU_EVTQ_CMD_SIZE)
+
+#define IVPU_MMU_CMD_OPCODE        GENMASK(8, 0)
+
+#define IVPU_MMU_CMD_SYNC_0_CS        GENMASK(13, 12)
+#define IVPU_MMU_CMD_SYNC_0_MSH        GENMASK(23, 22)
+#define IVPU_MMU_CMD_SYNC_0_MSI_ATTR    GENMASK(27, 24)
+#define IVPU_MMU_CMD_SYNC_0_MSI_ATTR    GENMASK(27, 24)
+#define IVPU_MMU_CMD_SYNC_0_MSI_DATA    GENMASK(63, 32)
+
+#define IVPU_MMU_CMD_CFGI_0_SSEC    BIT(10)
+#define IVPU_MMU_CMD_CFGI_0_SSV        BIT(11)
+#define IVPU_MMU_CMD_CFGI_0_SSID    GENMASK(31, 12)
+#define IVPU_MMU_CMD_CFGI_0_SID        GENMASK(63, 32)
+#define IVPU_MMU_CMD_CFGI_1_RANGE    GENMASK(4, 0)
+
+#define IVPU_MMU_CMD_TLBI_0_ASID    GENMASK(63, 48)
+#define IVPU_MMU_CMD_TLBI_0_VMID    GENMASK(47, 32)
+
+#define CMD_PREFETCH_CFG        0x1
+#define CMD_CFGI_STE            0x3
+#define CMD_CFGI_ALL            0x4
+#define CMD_CFGI_CD            0x5
+#define CMD_CFGI_CD_ALL            0x6
+#define CMD_TLBI_NH_ASID        0x11
+#define CMD_TLBI_EL2_ALL        0x20
+#define CMD_TLBI_NSNH_ALL        0x30
+#define CMD_SYNC            0x46
+
+#define IVPU_MMU_EVT_F_UUT        0x01
+#define IVPU_MMU_EVT_C_BAD_STREAMID    0x02
+#define IVPU_MMU_EVT_F_STE_FETCH    0x03
+#define IVPU_MMU_EVT_C_BAD_STE        0x04
+#define IVPU_MMU_EVT_F_BAD_ATS_TREQ    0x05
+#define IVPU_MMU_EVT_F_STREAM_DISABLED    0x06
+#define IVPU_MMU_EVT_F_TRANSL_FORBIDDEN    0x07
+#define IVPU_MMU_EVT_C_BAD_SUBSTREAMID    0x08
+#define IVPU_MMU_EVT_F_CD_FETCH        0x09
+#define IVPU_MMU_EVT_C_BAD_CD        0x0a
+#define IVPU_MMU_EVT_F_WALK_EABT    0x0b
+#define IVPU_MMU_EVT_F_TRANSLATION    0x10
+#define IVPU_MMU_EVT_F_ADDR_SIZE    0x11
+#define IVPU_MMU_EVT_F_ACCESS        0x12
+#define IVPU_MMU_EVT_F_PERMISSION    0x13
+#define IVPU_MMU_EVT_F_TLB_CONFLICT    0x20
+#define IVPU_MMU_EVT_F_CFG_CONFLICT    0x21
+#define IVPU_MMU_EVT_E_PAGE_REQUEST    0x24
+#define IVPU_MMU_EVT_F_VMS_FETCH    0x25
+
+#define IVPU_MMU_EVTS_MAX        8
+
+#define IVPU_MMU_EVT_OP_MASK        GENMASK_ULL(7, 0)
+#define IVPU_MMU_EVT_SSID_MASK        GENMASK_ULL(31, 12)
+
+#define IVPU_MMU_Q_BASE_RWA        BIT(62)
+#define IVPU_MMU_Q_BASE_ADDR_MASK    GENMASK_ULL(51, 5)
+#define IVPU_MMU_STRTAB_BASE_RA        BIT(62)
+#define IVPU_MMU_STRTAB_BASE_ADDR_MASK    GENMASK_ULL(51, 6)
+
+#define IVPU_MMU_IRQ_EVTQ_EN        BIT(2)
+#define IVPU_MMU_IRQ_GERROR_EN        BIT(0)
+
+#define IVPU_MMU_CR0_ATSCHK        BIT(4)
+#define IVPU_MMU_CR0_CMDQEN        BIT(3)
+#define IVPU_MMU_CR0_EVTQEN        BIT(2)
+#define IVPU_MMU_CR0_PRIQEN        BIT(1)
+#define IVPU_MMU_CR0_SMMUEN        BIT(0)
+
+#define IVPU_MMU_CR1_TABLE_SH        GENMASK(11, 10)
+#define IVPU_MMU_CR1_TABLE_OC        GENMASK(9, 8)
+#define IVPU_MMU_CR1_TABLE_IC        GENMASK(7, 6)
+#define IVPU_MMU_CR1_QUEUE_SH        GENMASK(5, 4)
+#define IVPU_MMU_CR1_QUEUE_OC        GENMASK(3, 2)
+#define IVPU_MMU_CR1_QUEUE_IC        GENMASK(1, 0)
+#define IVPU_MMU_CACHE_NC        0
+#define IVPU_MMU_CACHE_WB        1
+#define IVPU_MMU_CACHE_WT        2
+#define IVPU_MMU_SH_NSH            0
+#define IVPU_MMU_SH_OSH            2
+#define IVPU_MMU_SH_ISH            3
+
+#define IVPU_MMU_CMDQ_OP        GENMASK_ULL(7, 0)
+
+#define IVPU_MMU_CD_0_TCR_T0SZ        GENMASK_ULL(5, 0)
+#define IVPU_MMU_CD_0_TCR_TG0        GENMASK_ULL(7, 6)
+#define IVPU_MMU_CD_0_TCR_IRGN0        GENMASK_ULL(9, 8)
+#define IVPU_MMU_CD_0_TCR_ORGN0        GENMASK_ULL(11, 10)
+#define IVPU_MMU_CD_0_TCR_SH0        GENMASK_ULL(13, 12)
+#define IVPU_MMU_CD_0_TCR_EPD0        BIT_ULL(14)
+#define IVPU_MMU_CD_0_TCR_EPD1        BIT_ULL(30)
+#define IVPU_MMU_CD_0_ENDI        BIT(15)
+#define IVPU_MMU_CD_0_V            BIT(31)
+#define IVPU_MMU_CD_0_TCR_IPS        GENMASK_ULL(34, 32)
+#define IVPU_MMU_CD_0_TCR_TBI0        BIT_ULL(38)
+#define IVPU_MMU_CD_0_AA64        BIT(41)
+#define IVPU_MMU_CD_0_S            BIT(44)
+#define IVPU_MMU_CD_0_R            BIT(45)
+#define IVPU_MMU_CD_0_A            BIT(46)
+#define IVPU_MMU_CD_0_ASET        BIT(47)
+#define IVPU_MMU_CD_0_ASID        GENMASK_ULL(63, 48)
+
+#define IVPU_MMU_CD_1_TTB0_MASK        GENMASK_ULL(51, 4)
+
+#define IVPU_MMU_STE_0_S1CDMAX        GENMASK_ULL(63, 59)
+#define IVPU_MMU_STE_0_S1FMT        GENMASK_ULL(5, 4)
+#define IVPU_MMU_STE_0_S1FMT_LINEAR    0
+#define IVPU_MMU_STE_DWORDS        8
+#define IVPU_MMU_STE_0_CFG_S1_TRANS    5
+#define IVPU_MMU_STE_0_CFG        GENMASK_ULL(3, 1)
+#define IVPU_MMU_STE_0_S1CTXPTR_MASK    GENMASK_ULL(51, 6)
+#define IVPU_MMU_STE_0_V            BIT(0)
+
+#define IVPU_MMU_STE_1_STRW_NSEL1    0ul
+#define IVPU_MMU_STE_1_CONT        GENMASK_ULL(16, 13)
+#define IVPU_MMU_STE_1_STRW        GENMASK_ULL(31, 30)
+#define IVPU_MMU_STE_1_PRIVCFG        GENMASK_ULL(49, 48)
+#define IVPU_MMU_STE_1_PRIVCFG_UNPRIV    2ul
+#define IVPU_MMU_STE_1_INSTCFG        GENMASK_ULL(51, 50)
+#define IVPU_MMU_STE_1_INSTCFG_DATA    2ul
+#define IVPU_MMU_STE_1_MEV        BIT(19)
+#define IVPU_MMU_STE_1_S1STALLD        BIT(27)
+#define IVPU_MMU_STE_1_S1C_CACHE_NC    0ul
+#define IVPU_MMU_STE_1_S1C_CACHE_WBRA    1ul
+#define IVPU_MMU_STE_1_S1C_CACHE_WT    2ul
+#define IVPU_MMU_STE_1_S1C_CACHE_WB    3ul
+#define IVPU_MMU_STE_1_S1CIR        GENMASK_ULL(3, 2)
+#define IVPU_MMU_STE_1_S1COR        GENMASK_ULL(5, 4)
+#define IVPU_MMU_STE_1_S1CSH        GENMASK_ULL(7, 6)
+#define IVPU_MMU_STE_1_S1DSS        GENMASK_ULL(1, 0)
+#define IVPU_MMU_STE_1_S1DSS_TERMINATE    0x0
+
+#define IVPU_MMU_REG_TIMEOUT_US        (10 * USEC_PER_MSEC)
+#define IVPU_MMU_QUEUE_TIMEOUT_US    (100 * USEC_PER_MSEC)
+
+#define IVPU_MMU_GERROR_ERR_MASK ((REG_FLD(MTL_VPU_HOST_MMU_GERROR, CMDQ)) | \
+                  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, EVTQ_ABT)) | \
+                  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, PRIQ_ABT)) | \
+                  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_CMDQ_ABT)) | \
+                  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_EVTQ_ABT)) | \
+                  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_PRIQ_ABT)) | \
+                  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_ABT)) | \
+                  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, SFM)))
+
+static char *ivpu_mmu_evt_to_str(u32 cmd)
+{
+    switch (cmd) {
+    case IVPU_MMU_EVT_F_UUT:
+        return "Unsupported Upstream Transaction";
+    case IVPU_MMU_EVT_C_BAD_STREAMID:
+        return "Transaction StreamID out of range";
+    case IVPU_MMU_EVT_F_STE_FETCH:
+        return "Fetch of STE caused external abort";
+    case IVPU_MMU_EVT_C_BAD_STE:
+        return "Used STE invalid";
+    case IVPU_MMU_EVT_F_BAD_ATS_TREQ:
+        return "Address Request disallowed for a StreamID";
+    case IVPU_MMU_EVT_F_STREAM_DISABLED:
+        return "Transaction marks non-substream disabled";
+    case IVPU_MMU_EVT_F_TRANSL_FORBIDDEN:
+        return "MMU bypass is disallowed for this StreamID";
+    case IVPU_MMU_EVT_C_BAD_SUBSTREAMID:
+        return "Invalid StreamID";
+    case IVPU_MMU_EVT_F_CD_FETCH:
+        return "Fetch of CD caused external abort";
+    case IVPU_MMU_EVT_C_BAD_CD:
+        return "Fetched CD invalid";
+    case IVPU_MMU_EVT_F_WALK_EABT:
+        return " An external abort occurred fetching a TLB";
+    case IVPU_MMU_EVT_F_TRANSLATION:
+        return "Translation fault";
+    case IVPU_MMU_EVT_F_ADDR_SIZE:
+        return " Output address caused address size fault";
+    case IVPU_MMU_EVT_F_ACCESS:
+        return "Access flag fault";
+    case IVPU_MMU_EVT_F_PERMISSION:
+        return "Permission fault occurred on page access";
+    case IVPU_MMU_EVT_F_TLB_CONFLICT:
+        return "A TLB conflict";
+    case IVPU_MMU_EVT_F_CFG_CONFLICT:
+        return "A configuration cache conflict";
+    case IVPU_MMU_EVT_E_PAGE_REQUEST:
+        return "Page request hint from a client device";
+    case IVPU_MMU_EVT_F_VMS_FETCH:
+        return "Fetch of VMS caused external abort";
+    default:
+        return "Unknown CMDQ command";
+    }
+}
+
+static int ivpu_mmu_config_check(struct ivpu_device *vdev)
+{
+    u32 val_ref;
+    u32 val;
+
+    if (ivpu_is_simics(vdev))
+        val_ref = IVPU_MMU_IDR0_REF_SIMICS;
+    else
+        val_ref = IVPU_MMU_IDR0_REF;
+
+    val = REGV_RD32(MTL_VPU_HOST_MMU_IDR0);
+    if (val != val_ref)
+        ivpu_err(vdev, "IDR0 0x%x != IDR0_REF 0x%x\n", val, val_ref);
+
+    val = REGV_RD32(MTL_VPU_HOST_MMU_IDR1);
+    if (val != IVPU_MMU_IDR1_REF)
+        ivpu_warn(vdev, "IDR1 0x%x != IDR1_REF 0x%x\n", val, IVPU_MMU_IDR1_REF);
+
+    val = REGV_RD32(MTL_VPU_HOST_MMU_IDR3);
+    if (val != IVPU_MMU_IDR3_REF)
+        ivpu_warn(vdev, "IDR3 0x%x != IDR3_REF 0x%x\n", val, IVPU_MMU_IDR3_REF);
+
+    if (ivpu_is_simics(vdev))
+        val_ref = IVPU_MMU_IDR5_REF_SIMICS;
+    else if (ivpu_is_fpga(vdev))
+        val_ref = IVPU_MMU_IDR5_REF_FPGA;
+    else
+        val_ref = IVPU_MMU_IDR5_REF;
+
+    val = REGV_RD32(MTL_VPU_HOST_MMU_IDR5);
+    if (val != val_ref)
+        ivpu_warn(vdev, "IDR5 0x%x != IDR5_REF 0x%x\n", val, val_ref);
+
+    return 0;
+}
+
+static int ivpu_mmu_cdtab_alloc(struct ivpu_device *vdev)
+{
+    struct ivpu_mmu_info *mmu = vdev->mmu;
+    struct ivpu_mmu_cdtab *cdtab = &mmu->cdtab;
+    size_t size = IVPU_MMU_CDTAB_ENT_COUNT * IVPU_MMU_CDTAB_ENT_SIZE;
+
+    cdtab->base = dmam_alloc_coherent(vdev->drm.dev, size, &cdtab->dma, GFP_KERNEL);
+    if (!cdtab->base)
+        return -ENOMEM;
+
+    ivpu_dbg(MMU, "CDTAB alloc: dma=%pad size=%zu\n", &cdtab->dma, size);
+
+    return 0;
+}
+
+static int ivpu_mmu_strtab_alloc(struct ivpu_device *vdev)
+{
+    struct ivpu_mmu_info *mmu = vdev->mmu;
+    struct ivpu_mmu_strtab *strtab = &mmu->strtab;
+    size_t size = IVPU_MMU_STRTAB_ENT_COUNT * IVPU_MMU_STRTAB_ENT_SIZE;
+
+    strtab->base = dmam_alloc_coherent(vdev->drm.dev, size, &strtab->dma, GFP_KERNEL);
+    if (!strtab->base)
+        return -ENOMEM;
+
+    strtab->base_cfg = IVPU_MMU_STRTAB_CFG;
+    strtab->dma_q = IVPU_MMU_STRTAB_BASE_RA;
+    strtab->dma_q |= strtab->dma & IVPU_MMU_STRTAB_BASE_ADDR_MASK;
+
+    ivpu_dbg(MMU, "STRTAB alloc: dma=%pad dma_q=%pad size=%zu\n",
+         &strtab->dma, &strtab->dma_q, size);
+
+    return 0;
+}
+
+static int ivpu_mmu_cmdq_alloc(struct ivpu_device *vdev)
+{
+    struct ivpu_mmu_info *mmu = vdev->mmu;
+    struct ivpu_mmu_queue *q = &mmu->cmdq;
+
+    q->base = dmam_alloc_coherent(vdev->drm.dev, IVPU_MMU_CMDQ_SIZE, &q->dma, GFP_KERNEL);
+    if (!q->base)
+        return -ENOMEM;
+
+    q->dma_q = IVPU_MMU_Q_BASE_RWA;
+    q->dma_q |= q->dma & IVPU_MMU_Q_BASE_ADDR_MASK;
+    q->dma_q |= IVPU_MMU_Q_COUNT_LOG2;
+
+    ivpu_dbg(MMU, "CMDQ alloc: dma=%pad dma_q=%pad size=%u\n",
+         &q->dma, &q->dma_q, IVPU_MMU_CMDQ_SIZE);
+
+    return 0;
+}
+
+static int ivpu_mmu_evtq_alloc(struct ivpu_device *vdev)
+{
+    struct ivpu_mmu_info *mmu = vdev->mmu;
+    struct ivpu_mmu_queue *q = &mmu->evtq;
+
+    q->base = dmam_alloc_coherent(vdev->drm.dev, IVPU_MMU_EVTQ_SIZE, &q->dma, GFP_KERNEL);
+    if (!q->base)
+        return -ENOMEM;
+
+    q->dma_q = IVPU_MMU_Q_BASE_RWA;
+    q->dma_q |= q->dma & IVPU_MMU_Q_BASE_ADDR_MASK;
+    q->dma_q |= IVPU_MMU_Q_COUNT_LOG2;
+
+    ivpu_dbg(MMU, "EVTQ alloc: dma=%pad dma_q=%pad size=%u\n",
+         &q->dma, &q->dma_q, IVPU_MMU_EVTQ_SIZE);
+
+    return 0;
+}
+
+static int ivpu_mmu_structs_alloc(struct ivpu_device *vdev)
+{
+    int ret;
+
+    ret = ivpu_mmu_cdtab_alloc(vdev);
+    if (ret) {
+        ivpu_err(vdev, "Failed to allocate cdtab: %d\n", ret);
+        return ret;
+    }
+
+    ret = ivpu_mmu_strtab_alloc(vdev);
+    if (ret) {
+        ivpu_err(vdev, "Failed to allocate strtab: %d\n", ret);
+        return ret;
+    }
+
+    ret = ivpu_mmu_cmdq_alloc(vdev);
+    if (ret) {
+        ivpu_err(vdev, "Failed to allocate cmdq: %d\n", ret);
+        return ret;
+    }
+
+    ret = ivpu_mmu_evtq_alloc(vdev);
+    if (ret)
+        ivpu_err(vdev, "Failed to allocate evtq: %d\n", ret);
+
+    return ret;
+}
+
+static int ivpu_mmu_reg_write(struct ivpu_device *vdev, u32 reg, u32 val)
+{
+    u32 reg_ack = reg + 4; /* ACK register is 4B after base register */
+    u32 val_ack;
+    int ret;
+
+    REGV_WR32(reg, val);
+
Quoted 8 lines

drivers/gpu/drm/ivpu/ivpu_mmu.c line 386

Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_mmu.c new file +6 -0
+    ret = REGV_POLL(reg_ack, val_ack, (val == val_ack), IVPU_MMU_REG_TIMEOUT_US);
+    if (ret)
+        ivpu_err(vdev, "Failed to write register 0x%x\n", reg);
+
+    return ret;
+}

It really seems like you are reinventing regmap.  Any particular reason that mechanism doesn't work for you?

REGV_POLL() is actually just calling read_poll_timeout(), same as regmap, no reinvention here, just iopoll reuse. We don't need most of regmap features (locking, caching, etc) so I don't think is worth using it in our case.

Quoted 1 line

Also, given that this is a PCI device, I'm suprised there is no 0xFFFFFFFF handling.

We have some explicit handling of 0xFFFFFFFF (REG_IO_ERROR in the whole patchset) but it may be too little. I will review register reads and add handling where it seems to be needed.

Regards,
Jacek

#15 lore raw

Re: [PATCH v3 2/7] drm/ivpu: Add Intel VPU MMU support

Hi,

Quoted 124 lines · Jeffrey Hugo
Quoted 96 lines · Jacek Lawrynowicz

Hi,

Thanks for an in-depth review. My responses are inline.

Quoted 5 lines · Jeffrey Hugo
Quoted 2 lines · Jacek Lawrynowicz

VPU Memory Management Unit is based on ARM MMU-600.
It allows to create multiple virtual address spaces for the device and

"It allows the creation of"?

OK

Quoted 22 lines
Quoted 14 lines

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.

Quoted 7 lines

This patch is has to 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

So, this doesn't look valid.  This indicates that Karol wrote this patch, then it was handed off to Krystian, and then to you.  However, if that were the case, then the patch should be from Karol (which is then the author in git).  It is not.  It is from you.

What I suspect is happening is that the three of you all co-authroed this change.  In that case, there should probably be Co-developed-by tags in addition to SOB to make that clear.

Also, I missed this on the first patch I reviewed eariler, but it looks to have the same issue.

OK, I will change this in all the patches.

Quoted 44 lines
Quoted 44 lines

---
   drivers/gpu/drm/ivpu/Makefile           |   4 +- drivers/gpu/drm/ivpu/ivpu_drv.c         |  59 +- drivers/gpu/drm/ivpu/ivpu_drv.h         |   7 + drivers/gpu/drm/ivpu/ivpu_hw_mtl.c      |  10 + drivers/gpu/drm/ivpu/ivpu_mmu.c         | 883 ++++++++++++++++++++++++ drivers/gpu/drm/ivpu/ivpu_mmu.h         |  53 ++ drivers/gpu/drm/ivpu/ivpu_mmu_context.c | 419 +++++++++++ drivers/gpu/drm/ivpu/ivpu_mmu_context.h |  49 ++ include/uapi/drm/ivpu_drm.h             |   4 +

   9 files changed, 1485 insertions(+), 3 deletions(-)

   create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu_context.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu_context.h

drivers/gpu/drm/ivpu/Makefile +3 -1
index e59dc65abe6a..95bb04f26296 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_IVPU) += intel_vpu.o

drivers/gpu/drm/ivpu/ivpu_drv.c +4 -0
index a01c7244f6e5..cbeb9a801a31 100644
@@ -14,6 +14,8 @@
      #include "ivpu_drv.h"
    #include "ivpu_hw.h"
+#include "ivpu_mmu.h"
+#include "ivpu_mmu_context.h"
      #ifndef DRIVER_VERSION_STR
    #define DRIVER_VERSION_STR __stringify(DRM_IVPU_DRIVER_MAJOR) "." \
@@ -50,6 +52,11 @@ char *ivpu_platform_to_str(u32 platform)
      void ivpu_file_priv_get(struct ivpu_file_priv *file_priv, struct ivpu_file_priv **link)
    {
+    struct ivpu_device *vdev = file_priv->vdev;
+
Quoted 8 lines

drivers/gpu/drm/ivpu/ivpu_drv.c char *ivpu_platform_to_str(u32 platform) · line 62

Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +3 -0
+    ivpu_dbg(KREF, "file_priv get: ctx %u refcount %u\n",
+         file_priv->ctx.id, kref_read(&file_priv->ref));
+
        kref_get(&file_priv->ref);
        *link = file_priv;
    }

This hunk doesn't appear related to $SUBJECT.  It looks like it would fit better as part of patch 1.

Quoted 2 lines

file_priv->ctx is added in this patch and without context id this message does not make sense so this patch is the earliest the message can be printed.

I see

Quoted 10 lines
Quoted 8 lines

drivers/gpu/drm/ivpu/ivpu_drv.c void ivpu_file_priv_get(struct ivpu_file_priv *file_priv, struct ivpu_file_priv · line 67

Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +1 -0
@@ -57,6 +64,12 @@ void ivpu_file_priv_get(struct ivpu_file_priv *file_priv, struct ivpu_file_priv
    static void file_priv_release(struct kref *ref)
    {
        struct ivpu_file_priv *file_priv = container_of(ref, struct ivpu_file_priv, ref);
+    struct ivpu_device *vdev = file_priv->vdev;

Why do we need this?  It doesn't appear to be used at this time.  If it is needed later in the series, add it then.

vdev is implicitly used by ivpu_dbg(). This allows us to keep messages a bit longer and cleaner.

Violates coding-style -

12) Macros, Enums and RTL
-------------------------
.
.
Things to avoid when using macros:
.
.
2) macros that depend on having a local variable with a magic name:

Quoted 6 lines

.. code-block:: c

    #define FOO(val) bar(index, val)

might look like a good thing, but it's confusing as hell when one reads the code and it's prone to breakage from seemingly innocent changes.

Yeah, this was a mistake. I'll add explicit argument.

Quoted 36 lines
Quoted 36 lines
Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +5 -0
+
+    ivpu_dbg(FILE, "file_priv release: ctx %u\n", file_priv->ctx.id);
+
+    if (file_priv->ctx.id)
+        ivpu_mmu_user_context_fini(file_priv);
          kfree(file_priv);
    }
@@ -64,6 +77,10 @@ static void file_priv_release(struct kref *ref)
    void ivpu_file_priv_put(struct ivpu_file_priv **link)
    {
Quoted 8 lines
Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +4 -0
        struct ivpu_file_priv *file_priv = *link;
+    struct ivpu_device *vdev = file_priv->vdev;
+
+    ivpu_dbg(KREF, "file_priv put: ctx %u refcount %u\n",
+         file_priv->ctx.id, kref_read(&file_priv->ref));

vdev doesn't appear to be used, and the log message doesn't appear related to $SUBJECT

vdev is implicitly used by ivpu_dbg() and file_priv->ctx.id is added in this patch.

Quoted 14 lines

drivers/gpu/drm/ivpu/ivpu_drv.c static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_f · line 99

Quoted 12 lines

       *link = NULL; kref_put(&file_priv->ref, file_priv_release);

drivers/gpu/drm/ivpu/ivpu_drv.c +5 -1
@@ -75,7 +92,11 @@ static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_f
        struct ivpu_device *vdev = file_priv->vdev;
        struct pci_dev *pdev = to_pci_dev(vdev->drm.dev);
        struct drm_ivpu_param *args = data;
-    int ret = 0;
+    int ret;
+
+    ret = ivpu_mmu_user_context_init(file_priv);
+    if (ret)
+        return ret;

I fail to understand why we are initializing a user context in an ioctl.  This will happen N times, for N ioctl calls that a particular user does.  Feels like something that should occur in ivpu_open().  What am I missing?

Quoted 7 lines
Quoted 3 lines

The context is lazy initialized and it will be only initialized once during first buffer related ioctl. We don't want to allocate context ids and page tables for random user processes (Xorg, chrome, etc.). Currently user space has to open /dev/dri device in order to figure out what it is so we are optimizing for this case.

Hmm.  So, I haven't suggested this yet, but this driver looks like a good candidate for the newly proposed accel subsystem.  Since that subsystem has different devnodes, it will probably address this concern.

Regardless, is there an actual observed performance hit or other downside to this, or was this just something that was optimized for the sake of being optimal?

There were like 8MB of dma memory allocated on open() for the page tables but since then we moved to lazy alloc for the page tables and this is no longer an issue. I will remove lazy context alloc.

Quoted 67 lines
Quoted 42 lines
Quoted 18 lines
Quoted 18 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +0 -0
          switch (args->param) {

       case DRM_IVPU_PARAM_DEVICE_ID:

drivers/gpu/drm/ivpu/ivpu_drv.c +0 -0
@@ -99,6 +120,9 @@ static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_f

       case DRM_IVPU_PARAM_CONTEXT_PRIORITY:
           args->value = file_priv->priority;
           break;
+    case DRM_IVPU_PARAM_CONTEXT_ID:
+        args->value = file_priv->ctx.id;
+        break;
       default:
           ret = -EINVAL;
       }

drivers/gpu/drm/ivpu/ivpu_drv.c +0 -1
@@ -110,7 +134,11 @@ static int ivpu_set_param_ioctl(struct drm_device *dev, void *data, struct drm_f
    {
        struct ivpu_file_priv *file_priv = file->driver_priv;
        struct drm_ivpu_param *args = data;
-    int ret = 0;
Quoted 8 lines
Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +5 -0
+    int ret;
+
+    ret = ivpu_mmu_user_context_init(file_priv);
+    if (ret)
+        return ret;

Again, initializing a context in an ioctl isn't making sense to me.

See above.

Quoted 13 lines

drivers/gpu/drm/ivpu/ivpu_drv.c static int ivpu_open(struct drm_device *dev, struct drm_file *file) · line 173

Quoted 10 lines

       switch (args->param) {
       case DRM_IVPU_PARAM_CONTEXT_PRIORITY:

drivers/gpu/drm/ivpu/ivpu_drv.c +3 -0
@@ -139,9 +167,13 @@ static int ivpu_open(struct drm_device *dev, struct drm_file *file)
        file_priv->priority = DRM_IVPU_CONTEXT_PRIORITY_NORMAL;
          kref_init(&file_priv->ref);
+    mutex_init(&file_priv->lock);
          file->driver_priv = file_priv;
    +    ivpu_dbg(FILE, "file_priv alloc: process %s pid %d\n",
+         current->comm, task_pid_nr(current));
+

As it is, this doesn't seem related to $SUBJECT and seems to belong in patch 1.

Quoted 5 lines
Quoted 1 line

However, this seems dubious. Trace gives you the task name and pid for the process that triggered a trace, and you don't need to add anything to your code to be able to trace any symbol.

Sure but then it would be difficult to correlate this with the rest of dbg messages.
In order to properly use ftrace we have to implement tracing in the whole driver.
This is a first iteration of the driver that supposed to be as minimal as possible to make upstreaming easy :P

You could remove all the debug info if "minimal" is the goal  :P

I see improvements listed in the TODO, so I guess thats fair.

Quoted 15 lines
Quoted 14 lines
Quoted 8 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +1 -0
        return 0;
    }
    @@ -164,6 +196,7 @@ int ivpu_shutdown(struct ivpu_device *vdev)
        int ret;
          ivpu_hw_irq_disable(vdev);
+    ivpu_mmu_disable(vdev);

         ret = ivpu_hw_power_down(vdev);

I'm a bit confused.  It is good practice to balance your init and cleanup (fini in this series).  Everything that is done in init, is undone in fini in the reverse order.  This makes it easy to make sure things haven't been forgotten.

I don't understand what ivpu_shutdown() is suposed to be paired with.  I thought it was ivpu_hw_power_up(), but you have power_down() in shutdown().

I can't tell what mmu_disable() is suposed to pair with.  dev_init()?

Quoted 8 lines
Quoted 6 lines

Yeah I know this is a bit confusing in this patch. ivpu_shutdown() will be paired by ivpu_boot() in patch 5. I've added ivpu_shutdown() here so next patches in the series only add new code hoping it will be easier to review :/ Still it's gonna be confusing also later on. We have different boot flows for initial driver load and runtime resume. ivpu_dev_init() cannot power up the vpu in ivpu_boot() as it needs access to registers before boot. Also MMU has to be initialized early so internal allocations can be done in page tables.
Please take a look ivpu_dev_init() and ivpu_resume(), maybe you could suggest a better approach.

I went an looked at the final code result at the end of the series, and it doesn't look too bad.  I'm not seeing an easy alternative.  A few comments stating in code what you just explained about why things are how they are as little helper pointers to the reader might be good.

OK, I will add some comments.

Quoted 513 lines
Quoted 513 lines
Quoted 51 lines
Quoted 51 lines

       if (ret)

drivers/gpu/drm/ivpu/ivpu_drv.c +3 -0
@@ -272,6 +305,10 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
        if (!vdev->hw)
            return -ENOMEM;
    +    vdev->mmu = devm_kzalloc(vdev->drm.dev, sizeof(*vdev->mmu), GFP_KERNEL);
+    if (!vdev->mmu)
+        return -ENOMEM;
+
        vdev->hw->ops = &ivpu_hw_mtl_ops;
        vdev->platform = IVPU_PLATFORM_INVALID;
    @@ -303,8 +340,24 @@ static int ivpu_dev_init(struct ivpu_device *vdev)

           goto err_irq_fini;
       }
   +    ret = ivpu_mmu_global_context_init(vdev);
+    if (ret) {
+        ivpu_err(vdev, "Failed to initialize global MMU context: %d\n", ret); +        goto err_power_down;
+    }

drivers/gpu/drm/ivpu/ivpu_drv.c +7 -0
+
+    ret = ivpu_mmu_init(vdev);
+    if (ret) {
+        ivpu_err(vdev, "Failed to initialize MMU device: %d\n", ret);
+        goto err_mmu_gctx_fini;
+    }
+

       return 0;
   +err_mmu_gctx_fini:
+    ivpu_mmu_global_context_fini(vdev);
+err_power_down:
+    ivpu_hw_power_down(vdev);
   err_irq_fini:
       ivpu_irq_fini(vdev);
   err_pci_fini:

drivers/gpu/drm/ivpu/ivpu_drv.c +1 -0
@@ -316,6 +369,8 @@ static void ivpu_dev_fini(struct ivpu_device *vdev)
    {
        ivpu_shutdown(vdev);
    +    ivpu_mmu_fini(vdev);
+    ivpu_mmu_global_context_fini(vdev);
        ivpu_irq_fini(vdev);
        ivpu_pci_fini(vdev);

   diff --git a/drivers/gpu/drm/ivpu/ivpu_drv.h b/drivers/gpu/drm/ivpu/ivpu_drv.h index 43dfa78544c6..6eec3eb76c2f 100644

drivers/gpu/drm/ivpu/ivpu_drv.h +0 -0
@@ -14,6 +14,8 @@
    #include <linux/xarray.h>
    #include <uapi/drm/ivpu_drm.h>

   +#include "ivpu_mmu_context.h"
+
   #define DRIVER_NAME "intel_vpu"

Quoted 8 lines

drivers/gpu/drm/ivpu/ivpu_drv.h struct ivpu_wa_table { · line 74

Quoted 6 lines

   #define DRIVER_DESC "Driver for Intel Versatile Processing Unit (VPU)" #define DRIVER_DATE "20220913"

drivers/gpu/drm/ivpu/ivpu_drv.h +1 -0
@@ -70,6 +72,7 @@ struct ivpu_wa_table {
    };
      struct ivpu_hw_info;
+struct ivpu_mmu_info;

Why not just include ivpu_mmu.h?

To minimize dependencies between c files. We don't want all driver headers included in ivpu_drv.h.

Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/ivpu/ivpu_drv.h +0 -0
      struct ivpu_device {

       struct drm_device drm; /* Must be first */

drivers/gpu/drm/ivpu/ivpu_drv.h +1 -0
@@ -80,7 +83,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;
Quoted 8 lines
Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_drv.h +0 -0
        struct xa_limit context_xa_limit;
    @@ -95,6 +100,8 @@ struct ivpu_device {
    struct ivpu_file_priv {

       struct kref ref;
       struct ivpu_device *vdev;
+    struct mutex lock;

A comment indicating what this protects would be extremely useful.

OK

Quoted 34 lines
Quoted 34 lines

+    struct ivpu_mmu_context ctx;
       u32 priority;
   };
   diff --git a/drivers/gpu/drm/ivpu/ivpu_hw_mtl.c b/drivers/gpu/drm/ivpu/ivpu_hw_mtl.c index 17fc8e1104df..525b57c4029c 100644

drivers/gpu/drm/ivpu/ivpu_hw_mtl.c +9 -0
@@ -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 irqreturn_t 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))
+        ret &= ivpu_mmu_irq_evtq_handler(vdev);
+
+    if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_1_INT, status))
+        ivpu_dbg(IRQ, "MMU sync complete\n");
+
+    if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_2_INT, status))
+        ret &= ivpu_mmu_irq_gerr_handler(vdev);
+
        if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, CPU_INT_REDIRECT_0_INT, status))
            ret &= ivpu_hw_mtl_irq_wdt_mss_handler(vdev);

   diff --git a/drivers/gpu/drm/ivpu/ivpu_mmu.c b/drivers/gpu/drm/ivpu/ivpu_mmu.c new file mode 100644
index 000000000000..b9f225a83c45

drivers/gpu/drm/ivpu/ivpu_mmu.c new file +1 -0
@@ -0,0 +1,883 @@
+// SPDX-License-Identifier: GPL-2.0-only
Quoted 8 lines

drivers/gpu/drm/ivpu/ivpu_mmu.c line 7

Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_mmu.c new file +6 -0
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#include <linux/highmem.h>
+#include <linux/circ_buf.h>

Alphabetical order?

OK

Quoted 12 lines
Quoted 12 lines
drivers/gpu/drm/ivpu/ivpu_mmu.c new file +12 -0
+
+#include "ivpu_drv.h"
+#include "ivpu_hw_mtl_reg.h"
+#include "ivpu_hw_reg_io.h"
+#include "ivpu_mmu.h"
+#include "ivpu_mmu_context.h"
+
+#define IVPU_MMU_IDR0_REF        0x080f3e0f
+#define IVPU_MMU_IDR0_REF_SIMICS    0x080f3e1f
+#define IVPU_MMU_IDR1_REF        0x0e739d18
+#define IVPU_MMU_IDR3_REF        0x0000003c
+#define IVPU_MMU_IDR5_REF        0x00040070
Quoted 8 lines

drivers/gpu/drm/ivpu/ivpu_mmu.c line 25

Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_mmu.c new file +6 -0
+#define IVPU_MMU_IDR5_REF_SIMICS    0x00000075
+#define IVPU_MMU_IDR5_REF_FPGA        0x00800075
+
+#define IVPU_MMU_CDTAB_ENT_SIZE        64
+#define IVPU_MMU_CDTAB_ENT_COUNT_LOG2    8 /* 256 entries */
+#define IVPU_MMU_CDTAB_ENT_COUNT    ((u32)1 << IVPU_MMU_CDTAB_ENT_COUNT_LOG2)

Isn't this just BIT(IVPU_MMU_CDTAB_ENT_COUNT_LOG2)?

It is but shift here is more readable in my opinion.

Quoted 363 lines
Quoted 351 lines
drivers/gpu/drm/ivpu/ivpu_mmu.c new file +351 -0
+
+#define IVPU_MMU_STREAM_ID0        0
+#define IVPU_MMU_STREAM_ID3        3
+
+#define IVPU_MMU_STRTAB_ENT_SIZE    64
+#define IVPU_MMU_STRTAB_ENT_COUNT    4
+#define IVPU_MMU_STRTAB_CFG_LOG2SIZE    2
+#define IVPU_MMU_STRTAB_CFG        IVPU_MMU_STRTAB_CFG_LOG2SIZE
+
+#define IVPU_MMU_Q_COUNT_LOG2        4 /* 16 entries */
+#define IVPU_MMU_Q_COUNT        ((u32)1 << IVPU_MMU_Q_COUNT_LOG2)
+#define IVPU_MMU_Q_WRAP_BIT        (IVPU_MMU_Q_COUNT << 1)
+#define IVPU_MMU_Q_WRAP_MASK        (IVPU_MMU_Q_WRAP_BIT - 1)
+#define IVPU_MMU_Q_IDX_MASK        (IVPU_MMU_Q_COUNT - 1)
+#define IVPU_MMU_Q_IDX(val)        ((val) & IVPU_MMU_Q_IDX_MASK)
+
+#define IVPU_MMU_CMDQ_CMD_SIZE        16
+#define IVPU_MMU_CMDQ_SIZE        (IVPU_MMU_Q_COUNT * IVPU_MMU_CMDQ_CMD_SIZE)
+
+#define IVPU_MMU_EVTQ_CMD_SIZE        32
+#define IVPU_MMU_EVTQ_SIZE        (IVPU_MMU_Q_COUNT * IVPU_MMU_EVTQ_CMD_SIZE)
+
+#define IVPU_MMU_CMD_OPCODE        GENMASK(8, 0)
+
+#define IVPU_MMU_CMD_SYNC_0_CS        GENMASK(13, 12)
+#define IVPU_MMU_CMD_SYNC_0_MSH        GENMASK(23, 22)
+#define IVPU_MMU_CMD_SYNC_0_MSI_ATTR    GENMASK(27, 24)
+#define IVPU_MMU_CMD_SYNC_0_MSI_ATTR    GENMASK(27, 24)
+#define IVPU_MMU_CMD_SYNC_0_MSI_DATA    GENMASK(63, 32)
+
+#define IVPU_MMU_CMD_CFGI_0_SSEC    BIT(10)
+#define IVPU_MMU_CMD_CFGI_0_SSV        BIT(11)
+#define IVPU_MMU_CMD_CFGI_0_SSID    GENMASK(31, 12)
+#define IVPU_MMU_CMD_CFGI_0_SID        GENMASK(63, 32)
+#define IVPU_MMU_CMD_CFGI_1_RANGE    GENMASK(4, 0)
+
+#define IVPU_MMU_CMD_TLBI_0_ASID    GENMASK(63, 48)
+#define IVPU_MMU_CMD_TLBI_0_VMID    GENMASK(47, 32)
+
+#define CMD_PREFETCH_CFG        0x1
+#define CMD_CFGI_STE            0x3
+#define CMD_CFGI_ALL            0x4
+#define CMD_CFGI_CD            0x5
+#define CMD_CFGI_CD_ALL            0x6
+#define CMD_TLBI_NH_ASID        0x11
+#define CMD_TLBI_EL2_ALL        0x20
+#define CMD_TLBI_NSNH_ALL        0x30
+#define CMD_SYNC            0x46
+
+#define IVPU_MMU_EVT_F_UUT        0x01
+#define IVPU_MMU_EVT_C_BAD_STREAMID    0x02
+#define IVPU_MMU_EVT_F_STE_FETCH    0x03
+#define IVPU_MMU_EVT_C_BAD_STE        0x04
+#define IVPU_MMU_EVT_F_BAD_ATS_TREQ    0x05
+#define IVPU_MMU_EVT_F_STREAM_DISABLED    0x06
+#define IVPU_MMU_EVT_F_TRANSL_FORBIDDEN    0x07
+#define IVPU_MMU_EVT_C_BAD_SUBSTREAMID    0x08
+#define IVPU_MMU_EVT_F_CD_FETCH        0x09
+#define IVPU_MMU_EVT_C_BAD_CD        0x0a
+#define IVPU_MMU_EVT_F_WALK_EABT    0x0b
+#define IVPU_MMU_EVT_F_TRANSLATION    0x10
+#define IVPU_MMU_EVT_F_ADDR_SIZE    0x11
+#define IVPU_MMU_EVT_F_ACCESS        0x12
+#define IVPU_MMU_EVT_F_PERMISSION    0x13
+#define IVPU_MMU_EVT_F_TLB_CONFLICT    0x20
+#define IVPU_MMU_EVT_F_CFG_CONFLICT    0x21
+#define IVPU_MMU_EVT_E_PAGE_REQUEST    0x24
+#define IVPU_MMU_EVT_F_VMS_FETCH    0x25
+
+#define IVPU_MMU_EVTS_MAX        8
+
+#define IVPU_MMU_EVT_OP_MASK        GENMASK_ULL(7, 0)
+#define IVPU_MMU_EVT_SSID_MASK        GENMASK_ULL(31, 12)
+
+#define IVPU_MMU_Q_BASE_RWA        BIT(62)
+#define IVPU_MMU_Q_BASE_ADDR_MASK    GENMASK_ULL(51, 5)
+#define IVPU_MMU_STRTAB_BASE_RA        BIT(62)
+#define IVPU_MMU_STRTAB_BASE_ADDR_MASK    GENMASK_ULL(51, 6)
+
+#define IVPU_MMU_IRQ_EVTQ_EN        BIT(2)
+#define IVPU_MMU_IRQ_GERROR_EN        BIT(0)
+
+#define IVPU_MMU_CR0_ATSCHK        BIT(4)
+#define IVPU_MMU_CR0_CMDQEN        BIT(3)
+#define IVPU_MMU_CR0_EVTQEN        BIT(2)
+#define IVPU_MMU_CR0_PRIQEN        BIT(1)
+#define IVPU_MMU_CR0_SMMUEN        BIT(0)
+
+#define IVPU_MMU_CR1_TABLE_SH        GENMASK(11, 10)
+#define IVPU_MMU_CR1_TABLE_OC        GENMASK(9, 8)
+#define IVPU_MMU_CR1_TABLE_IC        GENMASK(7, 6)
+#define IVPU_MMU_CR1_QUEUE_SH        GENMASK(5, 4)
+#define IVPU_MMU_CR1_QUEUE_OC        GENMASK(3, 2)
+#define IVPU_MMU_CR1_QUEUE_IC        GENMASK(1, 0)
+#define IVPU_MMU_CACHE_NC        0
+#define IVPU_MMU_CACHE_WB        1
+#define IVPU_MMU_CACHE_WT        2
+#define IVPU_MMU_SH_NSH            0
+#define IVPU_MMU_SH_OSH            2
+#define IVPU_MMU_SH_ISH            3
+
+#define IVPU_MMU_CMDQ_OP        GENMASK_ULL(7, 0)
+
+#define IVPU_MMU_CD_0_TCR_T0SZ        GENMASK_ULL(5, 0)
+#define IVPU_MMU_CD_0_TCR_TG0        GENMASK_ULL(7, 6)
+#define IVPU_MMU_CD_0_TCR_IRGN0        GENMASK_ULL(9, 8)
+#define IVPU_MMU_CD_0_TCR_ORGN0        GENMASK_ULL(11, 10)
+#define IVPU_MMU_CD_0_TCR_SH0        GENMASK_ULL(13, 12)
+#define IVPU_MMU_CD_0_TCR_EPD0        BIT_ULL(14)
+#define IVPU_MMU_CD_0_TCR_EPD1        BIT_ULL(30)
+#define IVPU_MMU_CD_0_ENDI        BIT(15)
+#define IVPU_MMU_CD_0_V            BIT(31)
+#define IVPU_MMU_CD_0_TCR_IPS        GENMASK_ULL(34, 32)
+#define IVPU_MMU_CD_0_TCR_TBI0        BIT_ULL(38)
+#define IVPU_MMU_CD_0_AA64        BIT(41)
+#define IVPU_MMU_CD_0_S            BIT(44)
+#define IVPU_MMU_CD_0_R            BIT(45)
+#define IVPU_MMU_CD_0_A            BIT(46)
+#define IVPU_MMU_CD_0_ASET        BIT(47)
+#define IVPU_MMU_CD_0_ASID        GENMASK_ULL(63, 48)
+
+#define IVPU_MMU_CD_1_TTB0_MASK        GENMASK_ULL(51, 4)
+
+#define IVPU_MMU_STE_0_S1CDMAX        GENMASK_ULL(63, 59)
+#define IVPU_MMU_STE_0_S1FMT        GENMASK_ULL(5, 4)
+#define IVPU_MMU_STE_0_S1FMT_LINEAR    0
+#define IVPU_MMU_STE_DWORDS        8
+#define IVPU_MMU_STE_0_CFG_S1_TRANS    5
+#define IVPU_MMU_STE_0_CFG        GENMASK_ULL(3, 1)
+#define IVPU_MMU_STE_0_S1CTXPTR_MASK    GENMASK_ULL(51, 6)
+#define IVPU_MMU_STE_0_V            BIT(0)
+
+#define IVPU_MMU_STE_1_STRW_NSEL1    0ul
+#define IVPU_MMU_STE_1_CONT        GENMASK_ULL(16, 13)
+#define IVPU_MMU_STE_1_STRW        GENMASK_ULL(31, 30)
+#define IVPU_MMU_STE_1_PRIVCFG        GENMASK_ULL(49, 48)
+#define IVPU_MMU_STE_1_PRIVCFG_UNPRIV    2ul
+#define IVPU_MMU_STE_1_INSTCFG        GENMASK_ULL(51, 50)
+#define IVPU_MMU_STE_1_INSTCFG_DATA    2ul
+#define IVPU_MMU_STE_1_MEV        BIT(19)
+#define IVPU_MMU_STE_1_S1STALLD        BIT(27)
+#define IVPU_MMU_STE_1_S1C_CACHE_NC    0ul
+#define IVPU_MMU_STE_1_S1C_CACHE_WBRA    1ul
+#define IVPU_MMU_STE_1_S1C_CACHE_WT    2ul
+#define IVPU_MMU_STE_1_S1C_CACHE_WB    3ul
+#define IVPU_MMU_STE_1_S1CIR        GENMASK_ULL(3, 2)
+#define IVPU_MMU_STE_1_S1COR        GENMASK_ULL(5, 4)
+#define IVPU_MMU_STE_1_S1CSH        GENMASK_ULL(7, 6)
+#define IVPU_MMU_STE_1_S1DSS        GENMASK_ULL(1, 0)
+#define IVPU_MMU_STE_1_S1DSS_TERMINATE    0x0
+
+#define IVPU_MMU_REG_TIMEOUT_US        (10 * USEC_PER_MSEC)
+#define IVPU_MMU_QUEUE_TIMEOUT_US    (100 * USEC_PER_MSEC)
+
+#define IVPU_MMU_GERROR_ERR_MASK ((REG_FLD(MTL_VPU_HOST_MMU_GERROR, CMDQ)) | \
+                  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, EVTQ_ABT)) | \
+                  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, PRIQ_ABT)) | \
+                  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_CMDQ_ABT)) | \
+                  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_EVTQ_ABT)) | \
+                  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_PRIQ_ABT)) | \
+                  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_ABT)) | \
+                  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, SFM)))
+
+static char *ivpu_mmu_evt_to_str(u32 cmd)
+{
+    switch (cmd) {
+    case IVPU_MMU_EVT_F_UUT:
+        return "Unsupported Upstream Transaction";
+    case IVPU_MMU_EVT_C_BAD_STREAMID:
+        return "Transaction StreamID out of range";
+    case IVPU_MMU_EVT_F_STE_FETCH:
+        return "Fetch of STE caused external abort";
+    case IVPU_MMU_EVT_C_BAD_STE:
+        return "Used STE invalid";
+    case IVPU_MMU_EVT_F_BAD_ATS_TREQ:
+        return "Address Request disallowed for a StreamID";
+    case IVPU_MMU_EVT_F_STREAM_DISABLED:
+        return "Transaction marks non-substream disabled";
+    case IVPU_MMU_EVT_F_TRANSL_FORBIDDEN:
+        return "MMU bypass is disallowed for this StreamID";
+    case IVPU_MMU_EVT_C_BAD_SUBSTREAMID:
+        return "Invalid StreamID";
+    case IVPU_MMU_EVT_F_CD_FETCH:
+        return "Fetch of CD caused external abort";
+    case IVPU_MMU_EVT_C_BAD_CD:
+        return "Fetched CD invalid";
+    case IVPU_MMU_EVT_F_WALK_EABT:
+        return " An external abort occurred fetching a TLB";
+    case IVPU_MMU_EVT_F_TRANSLATION:
+        return "Translation fault";
+    case IVPU_MMU_EVT_F_ADDR_SIZE:
+        return " Output address caused address size fault";
+    case IVPU_MMU_EVT_F_ACCESS:
+        return "Access flag fault";
+    case IVPU_MMU_EVT_F_PERMISSION:
+        return "Permission fault occurred on page access";
+    case IVPU_MMU_EVT_F_TLB_CONFLICT:
+        return "A TLB conflict";
+    case IVPU_MMU_EVT_F_CFG_CONFLICT:
+        return "A configuration cache conflict";
+    case IVPU_MMU_EVT_E_PAGE_REQUEST:
+        return "Page request hint from a client device";
+    case IVPU_MMU_EVT_F_VMS_FETCH:
+        return "Fetch of VMS caused external abort";
+    default:
+        return "Unknown CMDQ command";
+    }
+}
+
+static int ivpu_mmu_config_check(struct ivpu_device *vdev)
+{
+    u32 val_ref;
+    u32 val;
+
+    if (ivpu_is_simics(vdev))
+        val_ref = IVPU_MMU_IDR0_REF_SIMICS;
+    else
+        val_ref = IVPU_MMU_IDR0_REF;
+
+    val = REGV_RD32(MTL_VPU_HOST_MMU_IDR0);
+    if (val != val_ref)
+        ivpu_err(vdev, "IDR0 0x%x != IDR0_REF 0x%x\n", val, val_ref);
+
+    val = REGV_RD32(MTL_VPU_HOST_MMU_IDR1);
+    if (val != IVPU_MMU_IDR1_REF)
+        ivpu_warn(vdev, "IDR1 0x%x != IDR1_REF 0x%x\n", val, IVPU_MMU_IDR1_REF);
+
+    val = REGV_RD32(MTL_VPU_HOST_MMU_IDR3);
+    if (val != IVPU_MMU_IDR3_REF)
+        ivpu_warn(vdev, "IDR3 0x%x != IDR3_REF 0x%x\n", val, IVPU_MMU_IDR3_REF);
+
+    if (ivpu_is_simics(vdev))
+        val_ref = IVPU_MMU_IDR5_REF_SIMICS;
+    else if (ivpu_is_fpga(vdev))
+        val_ref = IVPU_MMU_IDR5_REF_FPGA;
+    else
+        val_ref = IVPU_MMU_IDR5_REF;
+
+    val = REGV_RD32(MTL_VPU_HOST_MMU_IDR5);
+    if (val != val_ref)
+        ivpu_warn(vdev, "IDR5 0x%x != IDR5_REF 0x%x\n", val, val_ref);
+
+    return 0;
+}
+
+static int ivpu_mmu_cdtab_alloc(struct ivpu_device *vdev)
+{
+    struct ivpu_mmu_info *mmu = vdev->mmu;
+    struct ivpu_mmu_cdtab *cdtab = &mmu->cdtab;
+    size_t size = IVPU_MMU_CDTAB_ENT_COUNT * IVPU_MMU_CDTAB_ENT_SIZE;
+
+    cdtab->base = dmam_alloc_coherent(vdev->drm.dev, size, &cdtab->dma, GFP_KERNEL);
+    if (!cdtab->base)
+        return -ENOMEM;
+
+    ivpu_dbg(MMU, "CDTAB alloc: dma=%pad size=%zu\n", &cdtab->dma, size);
+
+    return 0;
+}
+
+static int ivpu_mmu_strtab_alloc(struct ivpu_device *vdev)
+{
+    struct ivpu_mmu_info *mmu = vdev->mmu;
+    struct ivpu_mmu_strtab *strtab = &mmu->strtab;
+    size_t size = IVPU_MMU_STRTAB_ENT_COUNT * IVPU_MMU_STRTAB_ENT_SIZE;
+
+    strtab->base = dmam_alloc_coherent(vdev->drm.dev, size, &strtab->dma, GFP_KERNEL);
+    if (!strtab->base)
+        return -ENOMEM;
+
+    strtab->base_cfg = IVPU_MMU_STRTAB_CFG;
+    strtab->dma_q = IVPU_MMU_STRTAB_BASE_RA;
+    strtab->dma_q |= strtab->dma & IVPU_MMU_STRTAB_BASE_ADDR_MASK;
+
+    ivpu_dbg(MMU, "STRTAB alloc: dma=%pad dma_q=%pad size=%zu\n",
+         &strtab->dma, &strtab->dma_q, size);
+
+    return 0;
+}
+
+static int ivpu_mmu_cmdq_alloc(struct ivpu_device *vdev)
+{
+    struct ivpu_mmu_info *mmu = vdev->mmu;
+    struct ivpu_mmu_queue *q = &mmu->cmdq;
+
+    q->base = dmam_alloc_coherent(vdev->drm.dev, IVPU_MMU_CMDQ_SIZE, &q->dma, GFP_KERNEL);
+    if (!q->base)
+        return -ENOMEM;
+
+    q->dma_q = IVPU_MMU_Q_BASE_RWA;
+    q->dma_q |= q->dma & IVPU_MMU_Q_BASE_ADDR_MASK;
+    q->dma_q |= IVPU_MMU_Q_COUNT_LOG2;
+
+    ivpu_dbg(MMU, "CMDQ alloc: dma=%pad dma_q=%pad size=%u\n",
+         &q->dma, &q->dma_q, IVPU_MMU_CMDQ_SIZE);
+
+    return 0;
+}
+
+static int ivpu_mmu_evtq_alloc(struct ivpu_device *vdev)
+{
+    struct ivpu_mmu_info *mmu = vdev->mmu;
+    struct ivpu_mmu_queue *q = &mmu->evtq;
+
+    q->base = dmam_alloc_coherent(vdev->drm.dev, IVPU_MMU_EVTQ_SIZE, &q->dma, GFP_KERNEL);
+    if (!q->base)
+        return -ENOMEM;
+
+    q->dma_q = IVPU_MMU_Q_BASE_RWA;
+    q->dma_q |= q->dma & IVPU_MMU_Q_BASE_ADDR_MASK;
+    q->dma_q |= IVPU_MMU_Q_COUNT_LOG2;
+
+    ivpu_dbg(MMU, "EVTQ alloc: dma=%pad dma_q=%pad size=%u\n",
+         &q->dma, &q->dma_q, IVPU_MMU_EVTQ_SIZE);
+
+    return 0;
+}
+
+static int ivpu_mmu_structs_alloc(struct ivpu_device *vdev)
+{
+    int ret;
+
+    ret = ivpu_mmu_cdtab_alloc(vdev);
+    if (ret) {
+        ivpu_err(vdev, "Failed to allocate cdtab: %d\n", ret);
+        return ret;
+    }
+
+    ret = ivpu_mmu_strtab_alloc(vdev);
+    if (ret) {
+        ivpu_err(vdev, "Failed to allocate strtab: %d\n", ret);
+        return ret;
+    }
+
+    ret = ivpu_mmu_cmdq_alloc(vdev);
+    if (ret) {
+        ivpu_err(vdev, "Failed to allocate cmdq: %d\n", ret);
+        return ret;
+    }
+
+    ret = ivpu_mmu_evtq_alloc(vdev);
+    if (ret)
+        ivpu_err(vdev, "Failed to allocate evtq: %d\n", ret);
+
+    return ret;
+}
+
+static int ivpu_mmu_reg_write(struct ivpu_device *vdev, u32 reg, u32 val)
+{
+    u32 reg_ack = reg + 4; /* ACK register is 4B after base register */
+    u32 val_ack;

drivers/gpu/drm/ivpu/ivpu_mmu.c line 386

Quoted 10 lines
drivers/gpu/drm/ivpu/ivpu_mmu.c new file +10 -0
+    int ret;
+
+    REGV_WR32(reg, val);
+
+    ret = REGV_POLL(reg_ack, val_ack, (val == val_ack), IVPU_MMU_REG_TIMEOUT_US);
+    if (ret)
+        ivpu_err(vdev, "Failed to write register 0x%x\n", reg);
+
+    return ret;
+}

It really seems like you are reinventing regmap.  Any particular reason that mechanism doesn't work for you?

Quoted 8 lines
Quoted 8 lines

REGV_POLL() is actually just calling read_poll_timeout(), same as regmap, no reinvention here, just iopoll reuse. We don't need most of regmap features (locking, caching, etc) so I don't think is worth using it in our case.

Quoted 1 line

Also, given that this is a PCI device, I'm suprised there is no 0xFFFFFFFF handling.

We have some explicit handling of 0xFFFFFFFF (REG_IO_ERROR in the whole patchset) but it may be too little. I will review register reads and add handling where it seems to be needed.

Regards,
Jacek

#16 lore raw

Re: [PATCH v3 2/7] drm/ivpu: Add Intel VPU MMU support

In reply to Jacek Lawrynowicz

Hi

Am 24.09.22 um 17:11 schrieb Jacek Lawrynowicz:

Quoted 197 lines

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

Address space is implemented as a struct 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 to 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/gpu/drm/ivpu/Makefile           |   4 +-
  drivers/gpu/drm/ivpu/ivpu_drv.c         |  59 +-
  drivers/gpu/drm/ivpu/ivpu_drv.h         |   7 +
  drivers/gpu/drm/ivpu/ivpu_hw_mtl.c      |  10 +
  drivers/gpu/drm/ivpu/ivpu_mmu.c         | 883 ++++++++++++++++++++++++
  drivers/gpu/drm/ivpu/ivpu_mmu.h         |  53 ++
  drivers/gpu/drm/ivpu/ivpu_mmu_context.c | 419 +++++++++++
  drivers/gpu/drm/ivpu/ivpu_mmu_context.h |  49 ++
  include/uapi/drm/ivpu_drm.h             |   4 +
  9 files changed, 1485 insertions(+), 3 deletions(-)

create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu_context.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu_context.h

drivers/gpu/drm/ivpu/Makefile +3 -1
index e59dc65abe6a..95bb04f26296 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_IVPU) += intel_vpu.o
drivers/gpu/drm/ivpu/ivpu_drv.c +55 -2
index a01c7244f6e5..cbeb9a801a31 100644
@@ -14,6 +14,8 @@
 
  #include "ivpu_drv.h"
  #include "ivpu_hw.h"
+#include "ivpu_mmu.h"
+#include "ivpu_mmu_context.h"
 
  #ifndef DRIVER_VERSION_STR
  #define DRIVER_VERSION_STR __stringify(DRM_IVPU_DRIVER_MAJOR) "." \
@@ -50,6 +52,11 @@ char *ivpu_platform_to_str(u32 platform)
 
  void ivpu_file_priv_get(struct ivpu_file_priv *file_priv, struct ivpu_file_priv **link)
  {
+	struct ivpu_device *vdev = file_priv->vdev;
+
+	ivpu_dbg(KREF, "file_priv get: ctx %u refcount %u\n",
+		 file_priv->ctx.id, kref_read(&file_priv->ref));
+
  	kref_get(&file_priv->ref);
  	*link = file_priv;
  }
@@ -57,6 +64,12 @@ void ivpu_file_priv_get(struct ivpu_file_priv *file_priv, struct ivpu_file_priv
  static void file_priv_release(struct kref *ref)
  {
  	struct ivpu_file_priv *file_priv = container_of(ref, struct ivpu_file_priv, ref);
+	struct ivpu_device *vdev = file_priv->vdev;
+
+	ivpu_dbg(FILE, "file_priv release: ctx %u\n", file_priv->ctx.id);
+
+	if (file_priv->ctx.id)
+		ivpu_mmu_user_context_fini(file_priv);
 
  	kfree(file_priv);
  }
@@ -64,6 +77,10 @@ static void file_priv_release(struct kref *ref)
  void ivpu_file_priv_put(struct ivpu_file_priv **link)
  {
  	struct ivpu_file_priv *file_priv = *link;
+	struct ivpu_device *vdev = file_priv->vdev;
+
+	ivpu_dbg(KREF, "file_priv put: ctx %u refcount %u\n",
+		 file_priv->ctx.id, kref_read(&file_priv->ref));
 
  	*link = NULL;
  	kref_put(&file_priv->ref, file_priv_release);
@@ -75,7 +92,11 @@ static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_f
  	struct ivpu_device *vdev = file_priv->vdev;
  	struct pci_dev *pdev = to_pci_dev(vdev->drm.dev);
  	struct drm_ivpu_param *args = data;
-	int ret = 0;
+	int ret;
+
+	ret = ivpu_mmu_user_context_init(file_priv);
+	if (ret)
+		return ret;
 
  	switch (args->param) {
  	case DRM_IVPU_PARAM_DEVICE_ID:
@@ -99,6 +120,9 @@ static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_f
  	case DRM_IVPU_PARAM_CONTEXT_PRIORITY:
  		args->value = file_priv->priority;
  		break;
+	case DRM_IVPU_PARAM_CONTEXT_ID:
+		args->value = file_priv->ctx.id;
+		break;
  	default:
  		ret = -EINVAL;
  	}
@@ -110,7 +134,11 @@ static int ivpu_set_param_ioctl(struct drm_device *dev, void *data, struct drm_f
  {
  	struct ivpu_file_priv *file_priv = file->driver_priv;
  	struct drm_ivpu_param *args = data;
-	int ret = 0;
+	int ret;
+
+	ret = ivpu_mmu_user_context_init(file_priv);
+	if (ret)
+		return ret;
 
  	switch (args->param) {
  	case DRM_IVPU_PARAM_CONTEXT_PRIORITY:
@@ -139,9 +167,13 @@ static int ivpu_open(struct drm_device *dev, struct drm_file *file)
  	file_priv->priority = DRM_IVPU_CONTEXT_PRIORITY_NORMAL;
 
  	kref_init(&file_priv->ref);
+	mutex_init(&file_priv->lock);
 
  	file->driver_priv = file_priv;
 
+	ivpu_dbg(FILE, "file_priv alloc: process %s pid %d\n",
+		 current->comm, task_pid_nr(current));
+
  	return 0;
  }
 
@@ -164,6 +196,7 @@ int ivpu_shutdown(struct ivpu_device *vdev)
  	int ret;
 
  	ivpu_hw_irq_disable(vdev);
+	ivpu_mmu_disable(vdev);
 
  	ret = ivpu_hw_power_down(vdev);
  	if (ret)
@@ -272,6 +305,10 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
  	if (!vdev->hw)
  		return -ENOMEM;
 
+	vdev->mmu = devm_kzalloc(vdev->drm.dev, sizeof(*vdev->mmu), GFP_KERNEL);
+	if (!vdev->mmu)
+		return -ENOMEM;
+
  	vdev->hw->ops = &ivpu_hw_mtl_ops;
  	vdev->platform = IVPU_PLATFORM_INVALID;
 
@@ -303,8 +340,24 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
  		goto err_irq_fini;
  	}
 
+	ret = ivpu_mmu_global_context_init(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to initialize global MMU context: %d\n", ret);
+		goto err_power_down;
+	}
+
+	ret = ivpu_mmu_init(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to initialize MMU device: %d\n", ret);
+		goto err_mmu_gctx_fini;
+	}
+
  	return 0;
 
+err_mmu_gctx_fini:
+	ivpu_mmu_global_context_fini(vdev);
+err_power_down:
+	ivpu_hw_power_down(vdev);
  err_irq_fini:
  	ivpu_irq_fini(vdev);
  err_pci_fini:

drivers/gpu/drm/ivpu/ivpu_drv.c static void ivpu_dev_fini(struct ivpu_device *vdev) · line 375

Quoted 7 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +2 -0
@@ -316,6 +369,8 @@ static void ivpu_dev_fini(struct ivpu_device *vdev)
  {
  	ivpu_shutdown(vdev);
 
+	ivpu_mmu_fini(vdev);
+	ivpu_mmu_global_context_fini(vdev);
  	ivpu_irq_fini(vdev);
  	ivpu_pci_fini(vdev);

I already mentioned that these _fini and cleanup routines are not much liked in DRM land. I suggest to go throught the code and turn everything into managed cleanup; here and everywhere else in the driver. The overhead is small and your driver probably doesn't run in a environment where it makes a difference.

Quoted 771 lines
drivers/gpu/drm/ivpu/ivpu_drv.h +7 -0
index 43dfa78544c6..6eec3eb76c2f 100644
@@ -14,6 +14,8 @@
  #include <linux/xarray.h>
  #include <uapi/drm/ivpu_drm.h>
 
+#include "ivpu_mmu_context.h"
+
  #define DRIVER_NAME "intel_vpu"
  #define DRIVER_DESC "Driver for Intel Versatile Processing Unit (VPU)"
  #define DRIVER_DATE "20220913"
@@ -70,6 +72,7 @@ struct ivpu_wa_table {
  };
 
  struct ivpu_hw_info;
+struct ivpu_mmu_info;
 
  struct ivpu_device {
  	struct drm_device drm; /* Must be first */
@@ -80,7 +83,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;
 
@@ -95,6 +100,8 @@ struct ivpu_device {
  struct ivpu_file_priv {
  	struct kref ref;
  	struct ivpu_device *vdev;
+	struct mutex lock;
+	struct ivpu_mmu_context ctx;
  	u32 priority;
  };
 
drivers/gpu/drm/ivpu/ivpu_hw_mtl.c +10 -0
index 17fc8e1104df..525b57c4029c 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 irqreturn_t 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))
+		ret &= ivpu_mmu_irq_evtq_handler(vdev);
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_1_INT, status))
+		ivpu_dbg(IRQ, "MMU sync complete\n");
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_2_INT, status))
+		ret &= ivpu_mmu_irq_gerr_handler(vdev);
+
  	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, CPU_INT_REDIRECT_0_INT, status))
  		ret &= ivpu_hw_mtl_irq_wdt_mss_handler(vdev);
 
drivers/gpu/drm/ivpu/ivpu_mmu.c new file +696 -0
new file mode 100644
index 000000000000..b9f225a83c45
@@ -0,0 +1,883 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#include <linux/highmem.h>
+#include <linux/circ_buf.h>
+
+#include "ivpu_drv.h"
+#include "ivpu_hw_mtl_reg.h"
+#include "ivpu_hw_reg_io.h"
+#include "ivpu_mmu.h"
+#include "ivpu_mmu_context.h"
+
+#define IVPU_MMU_IDR0_REF		0x080f3e0f
+#define IVPU_MMU_IDR0_REF_SIMICS	0x080f3e1f
+#define IVPU_MMU_IDR1_REF		0x0e739d18
+#define IVPU_MMU_IDR3_REF		0x0000003c
+#define IVPU_MMU_IDR5_REF		0x00040070
+#define IVPU_MMU_IDR5_REF_SIMICS	0x00000075
+#define IVPU_MMU_IDR5_REF_FPGA		0x00800075
+
+#define IVPU_MMU_CDTAB_ENT_SIZE		64
+#define IVPU_MMU_CDTAB_ENT_COUNT_LOG2	8 /* 256 entries */
+#define IVPU_MMU_CDTAB_ENT_COUNT	((u32)1 << IVPU_MMU_CDTAB_ENT_COUNT_LOG2)
+
+#define IVPU_MMU_STREAM_ID0		0
+#define IVPU_MMU_STREAM_ID3		3
+
+#define IVPU_MMU_STRTAB_ENT_SIZE	64
+#define IVPU_MMU_STRTAB_ENT_COUNT	4
+#define IVPU_MMU_STRTAB_CFG_LOG2SIZE	2
+#define IVPU_MMU_STRTAB_CFG		IVPU_MMU_STRTAB_CFG_LOG2SIZE
+
+#define IVPU_MMU_Q_COUNT_LOG2		4 /* 16 entries */
+#define IVPU_MMU_Q_COUNT		((u32)1 << IVPU_MMU_Q_COUNT_LOG2)
+#define IVPU_MMU_Q_WRAP_BIT		(IVPU_MMU_Q_COUNT << 1)
+#define IVPU_MMU_Q_WRAP_MASK		(IVPU_MMU_Q_WRAP_BIT - 1)
+#define IVPU_MMU_Q_IDX_MASK		(IVPU_MMU_Q_COUNT - 1)
+#define IVPU_MMU_Q_IDX(val)		((val) & IVPU_MMU_Q_IDX_MASK)
+
+#define IVPU_MMU_CMDQ_CMD_SIZE		16
+#define IVPU_MMU_CMDQ_SIZE		(IVPU_MMU_Q_COUNT * IVPU_MMU_CMDQ_CMD_SIZE)
+
+#define IVPU_MMU_EVTQ_CMD_SIZE		32
+#define IVPU_MMU_EVTQ_SIZE		(IVPU_MMU_Q_COUNT * IVPU_MMU_EVTQ_CMD_SIZE)
+
+#define IVPU_MMU_CMD_OPCODE		GENMASK(8, 0)
+
+#define IVPU_MMU_CMD_SYNC_0_CS		GENMASK(13, 12)
+#define IVPU_MMU_CMD_SYNC_0_MSH		GENMASK(23, 22)
+#define IVPU_MMU_CMD_SYNC_0_MSI_ATTR	GENMASK(27, 24)
+#define IVPU_MMU_CMD_SYNC_0_MSI_ATTR	GENMASK(27, 24)
+#define IVPU_MMU_CMD_SYNC_0_MSI_DATA	GENMASK(63, 32)
+
+#define IVPU_MMU_CMD_CFGI_0_SSEC	BIT(10)
+#define IVPU_MMU_CMD_CFGI_0_SSV		BIT(11)
+#define IVPU_MMU_CMD_CFGI_0_SSID	GENMASK(31, 12)
+#define IVPU_MMU_CMD_CFGI_0_SID		GENMASK(63, 32)
+#define IVPU_MMU_CMD_CFGI_1_RANGE	GENMASK(4, 0)
+
+#define IVPU_MMU_CMD_TLBI_0_ASID	GENMASK(63, 48)
+#define IVPU_MMU_CMD_TLBI_0_VMID	GENMASK(47, 32)
+
+#define CMD_PREFETCH_CFG		0x1
+#define CMD_CFGI_STE			0x3
+#define CMD_CFGI_ALL			0x4
+#define CMD_CFGI_CD			0x5
+#define CMD_CFGI_CD_ALL			0x6
+#define CMD_TLBI_NH_ASID		0x11
+#define CMD_TLBI_EL2_ALL		0x20
+#define CMD_TLBI_NSNH_ALL		0x30
+#define CMD_SYNC			0x46
+
+#define IVPU_MMU_EVT_F_UUT		0x01
+#define IVPU_MMU_EVT_C_BAD_STREAMID	0x02
+#define IVPU_MMU_EVT_F_STE_FETCH	0x03
+#define IVPU_MMU_EVT_C_BAD_STE		0x04
+#define IVPU_MMU_EVT_F_BAD_ATS_TREQ	0x05
+#define IVPU_MMU_EVT_F_STREAM_DISABLED	0x06
+#define IVPU_MMU_EVT_F_TRANSL_FORBIDDEN	0x07
+#define IVPU_MMU_EVT_C_BAD_SUBSTREAMID	0x08
+#define IVPU_MMU_EVT_F_CD_FETCH		0x09
+#define IVPU_MMU_EVT_C_BAD_CD		0x0a
+#define IVPU_MMU_EVT_F_WALK_EABT	0x0b
+#define IVPU_MMU_EVT_F_TRANSLATION	0x10
+#define IVPU_MMU_EVT_F_ADDR_SIZE	0x11
+#define IVPU_MMU_EVT_F_ACCESS		0x12
+#define IVPU_MMU_EVT_F_PERMISSION	0x13
+#define IVPU_MMU_EVT_F_TLB_CONFLICT	0x20
+#define IVPU_MMU_EVT_F_CFG_CONFLICT	0x21
+#define IVPU_MMU_EVT_E_PAGE_REQUEST	0x24
+#define IVPU_MMU_EVT_F_VMS_FETCH	0x25
+
+#define IVPU_MMU_EVTS_MAX		8
+
+#define IVPU_MMU_EVT_OP_MASK		GENMASK_ULL(7, 0)
+#define IVPU_MMU_EVT_SSID_MASK		GENMASK_ULL(31, 12)
+
+#define IVPU_MMU_Q_BASE_RWA		BIT(62)
+#define IVPU_MMU_Q_BASE_ADDR_MASK	GENMASK_ULL(51, 5)
+#define IVPU_MMU_STRTAB_BASE_RA		BIT(62)
+#define IVPU_MMU_STRTAB_BASE_ADDR_MASK	GENMASK_ULL(51, 6)
+
+#define IVPU_MMU_IRQ_EVTQ_EN		BIT(2)
+#define IVPU_MMU_IRQ_GERROR_EN		BIT(0)
+
+#define IVPU_MMU_CR0_ATSCHK		BIT(4)
+#define IVPU_MMU_CR0_CMDQEN		BIT(3)
+#define IVPU_MMU_CR0_EVTQEN		BIT(2)
+#define IVPU_MMU_CR0_PRIQEN		BIT(1)
+#define IVPU_MMU_CR0_SMMUEN		BIT(0)
+
+#define IVPU_MMU_CR1_TABLE_SH		GENMASK(11, 10)
+#define IVPU_MMU_CR1_TABLE_OC		GENMASK(9, 8)
+#define IVPU_MMU_CR1_TABLE_IC		GENMASK(7, 6)
+#define IVPU_MMU_CR1_QUEUE_SH		GENMASK(5, 4)
+#define IVPU_MMU_CR1_QUEUE_OC		GENMASK(3, 2)
+#define IVPU_MMU_CR1_QUEUE_IC		GENMASK(1, 0)
+#define IVPU_MMU_CACHE_NC		0
+#define IVPU_MMU_CACHE_WB		1
+#define IVPU_MMU_CACHE_WT		2
+#define IVPU_MMU_SH_NSH			0
+#define IVPU_MMU_SH_OSH			2
+#define IVPU_MMU_SH_ISH			3
+
+#define IVPU_MMU_CMDQ_OP		GENMASK_ULL(7, 0)
+
+#define IVPU_MMU_CD_0_TCR_T0SZ		GENMASK_ULL(5, 0)
+#define IVPU_MMU_CD_0_TCR_TG0		GENMASK_ULL(7, 6)
+#define IVPU_MMU_CD_0_TCR_IRGN0		GENMASK_ULL(9, 8)
+#define IVPU_MMU_CD_0_TCR_ORGN0		GENMASK_ULL(11, 10)
+#define IVPU_MMU_CD_0_TCR_SH0		GENMASK_ULL(13, 12)
+#define IVPU_MMU_CD_0_TCR_EPD0		BIT_ULL(14)
+#define IVPU_MMU_CD_0_TCR_EPD1		BIT_ULL(30)
+#define IVPU_MMU_CD_0_ENDI		BIT(15)
+#define IVPU_MMU_CD_0_V			BIT(31)
+#define IVPU_MMU_CD_0_TCR_IPS		GENMASK_ULL(34, 32)
+#define IVPU_MMU_CD_0_TCR_TBI0		BIT_ULL(38)
+#define IVPU_MMU_CD_0_AA64		BIT(41)
+#define IVPU_MMU_CD_0_S			BIT(44)
+#define IVPU_MMU_CD_0_R			BIT(45)
+#define IVPU_MMU_CD_0_A			BIT(46)
+#define IVPU_MMU_CD_0_ASET		BIT(47)
+#define IVPU_MMU_CD_0_ASID		GENMASK_ULL(63, 48)
+
+#define IVPU_MMU_CD_1_TTB0_MASK		GENMASK_ULL(51, 4)
+
+#define IVPU_MMU_STE_0_S1CDMAX		GENMASK_ULL(63, 59)
+#define IVPU_MMU_STE_0_S1FMT		GENMASK_ULL(5, 4)
+#define IVPU_MMU_STE_0_S1FMT_LINEAR	0
+#define IVPU_MMU_STE_DWORDS		8
+#define IVPU_MMU_STE_0_CFG_S1_TRANS	5
+#define IVPU_MMU_STE_0_CFG		GENMASK_ULL(3, 1)
+#define IVPU_MMU_STE_0_S1CTXPTR_MASK	GENMASK_ULL(51, 6)
+#define IVPU_MMU_STE_0_V			BIT(0)
+
+#define IVPU_MMU_STE_1_STRW_NSEL1	0ul
+#define IVPU_MMU_STE_1_CONT		GENMASK_ULL(16, 13)
+#define IVPU_MMU_STE_1_STRW		GENMASK_ULL(31, 30)
+#define IVPU_MMU_STE_1_PRIVCFG		GENMASK_ULL(49, 48)
+#define IVPU_MMU_STE_1_PRIVCFG_UNPRIV	2ul
+#define IVPU_MMU_STE_1_INSTCFG		GENMASK_ULL(51, 50)
+#define IVPU_MMU_STE_1_INSTCFG_DATA	2ul
+#define IVPU_MMU_STE_1_MEV		BIT(19)
+#define IVPU_MMU_STE_1_S1STALLD		BIT(27)
+#define IVPU_MMU_STE_1_S1C_CACHE_NC	0ul
+#define IVPU_MMU_STE_1_S1C_CACHE_WBRA	1ul
+#define IVPU_MMU_STE_1_S1C_CACHE_WT	2ul
+#define IVPU_MMU_STE_1_S1C_CACHE_WB	3ul
+#define IVPU_MMU_STE_1_S1CIR		GENMASK_ULL(3, 2)
+#define IVPU_MMU_STE_1_S1COR		GENMASK_ULL(5, 4)
+#define IVPU_MMU_STE_1_S1CSH		GENMASK_ULL(7, 6)
+#define IVPU_MMU_STE_1_S1DSS		GENMASK_ULL(1, 0)
+#define IVPU_MMU_STE_1_S1DSS_TERMINATE	0x0
+
+#define IVPU_MMU_REG_TIMEOUT_US		(10 * USEC_PER_MSEC)
+#define IVPU_MMU_QUEUE_TIMEOUT_US	(100 * USEC_PER_MSEC)
+
+#define IVPU_MMU_GERROR_ERR_MASK ((REG_FLD(MTL_VPU_HOST_MMU_GERROR, CMDQ)) | \
+				  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, EVTQ_ABT)) | \
+				  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, PRIQ_ABT)) | \
+				  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_CMDQ_ABT)) | \
+				  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_EVTQ_ABT)) | \
+				  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_PRIQ_ABT)) | \
+				  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_ABT)) | \
+				  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, SFM)))
+
+static char *ivpu_mmu_evt_to_str(u32 cmd)
+{
+	switch (cmd) {
+	case IVPU_MMU_EVT_F_UUT:
+		return "Unsupported Upstream Transaction";
+	case IVPU_MMU_EVT_C_BAD_STREAMID:
+		return "Transaction StreamID out of range";
+	case IVPU_MMU_EVT_F_STE_FETCH:
+		return "Fetch of STE caused external abort";
+	case IVPU_MMU_EVT_C_BAD_STE:
+		return "Used STE invalid";
+	case IVPU_MMU_EVT_F_BAD_ATS_TREQ:
+		return "Address Request disallowed for a StreamID";
+	case IVPU_MMU_EVT_F_STREAM_DISABLED:
+		return "Transaction marks non-substream disabled";
+	case IVPU_MMU_EVT_F_TRANSL_FORBIDDEN:
+		return "MMU bypass is disallowed for this StreamID";
+	case IVPU_MMU_EVT_C_BAD_SUBSTREAMID:
+		return "Invalid StreamID";
+	case IVPU_MMU_EVT_F_CD_FETCH:
+		return "Fetch of CD caused external abort";
+	case IVPU_MMU_EVT_C_BAD_CD:
+		return "Fetched CD invalid";
+	case IVPU_MMU_EVT_F_WALK_EABT:
+		return " An external abort occurred fetching a TLB";
+	case IVPU_MMU_EVT_F_TRANSLATION:
+		return "Translation fault";
+	case IVPU_MMU_EVT_F_ADDR_SIZE:
+		return " Output address caused address size fault";
+	case IVPU_MMU_EVT_F_ACCESS:
+		return "Access flag fault";
+	case IVPU_MMU_EVT_F_PERMISSION:
+		return "Permission fault occurred on page access";
+	case IVPU_MMU_EVT_F_TLB_CONFLICT:
+		return "A TLB conflict";
+	case IVPU_MMU_EVT_F_CFG_CONFLICT:
+		return "A configuration cache conflict";
+	case IVPU_MMU_EVT_E_PAGE_REQUEST:
+		return "Page request hint from a client device";
+	case IVPU_MMU_EVT_F_VMS_FETCH:
+		return "Fetch of VMS caused external abort";
+	default:
+		return "Unknown CMDQ command";
+	}
+}
+
+static int ivpu_mmu_config_check(struct ivpu_device *vdev)
+{
+	u32 val_ref;
+	u32 val;
+
+	if (ivpu_is_simics(vdev))
+		val_ref = IVPU_MMU_IDR0_REF_SIMICS;
+	else
+		val_ref = IVPU_MMU_IDR0_REF;
+
+	val = REGV_RD32(MTL_VPU_HOST_MMU_IDR0);
+	if (val != val_ref)
+		ivpu_err(vdev, "IDR0 0x%x != IDR0_REF 0x%x\n", val, val_ref);
+
+	val = REGV_RD32(MTL_VPU_HOST_MMU_IDR1);
+	if (val != IVPU_MMU_IDR1_REF)
+		ivpu_warn(vdev, "IDR1 0x%x != IDR1_REF 0x%x\n", val, IVPU_MMU_IDR1_REF);
+
+	val = REGV_RD32(MTL_VPU_HOST_MMU_IDR3);
+	if (val != IVPU_MMU_IDR3_REF)
+		ivpu_warn(vdev, "IDR3 0x%x != IDR3_REF 0x%x\n", val, IVPU_MMU_IDR3_REF);
+
+	if (ivpu_is_simics(vdev))
+		val_ref = IVPU_MMU_IDR5_REF_SIMICS;
+	else if (ivpu_is_fpga(vdev))
+		val_ref = IVPU_MMU_IDR5_REF_FPGA;
+	else
+		val_ref = IVPU_MMU_IDR5_REF;
+
+	val = REGV_RD32(MTL_VPU_HOST_MMU_IDR5);
+	if (val != val_ref)
+		ivpu_warn(vdev, "IDR5 0x%x != IDR5_REF 0x%x\n", val, val_ref);
+
+	return 0;
+}
+
+static int ivpu_mmu_cdtab_alloc(struct ivpu_device *vdev)
+{
+	struct ivpu_mmu_info *mmu = vdev->mmu;
+	struct ivpu_mmu_cdtab *cdtab = &mmu->cdtab;
+	size_t size = IVPU_MMU_CDTAB_ENT_COUNT * IVPU_MMU_CDTAB_ENT_SIZE;
+
+	cdtab->base = dmam_alloc_coherent(vdev->drm.dev, size, &cdtab->dma, GFP_KERNEL);
+	if (!cdtab->base)
+		return -ENOMEM;
+
+	ivpu_dbg(MMU, "CDTAB alloc: dma=%pad size=%zu\n", &cdtab->dma, size);
+
+	return 0;
+}
+
+static int ivpu_mmu_strtab_alloc(struct ivpu_device *vdev)
+{
+	struct ivpu_mmu_info *mmu = vdev->mmu;
+	struct ivpu_mmu_strtab *strtab = &mmu->strtab;
+	size_t size = IVPU_MMU_STRTAB_ENT_COUNT * IVPU_MMU_STRTAB_ENT_SIZE;
+
+	strtab->base = dmam_alloc_coherent(vdev->drm.dev, size, &strtab->dma, GFP_KERNEL);
+	if (!strtab->base)
+		return -ENOMEM;
+
+	strtab->base_cfg = IVPU_MMU_STRTAB_CFG;
+	strtab->dma_q = IVPU_MMU_STRTAB_BASE_RA;
+	strtab->dma_q |= strtab->dma & IVPU_MMU_STRTAB_BASE_ADDR_MASK;
+
+	ivpu_dbg(MMU, "STRTAB alloc: dma=%pad dma_q=%pad size=%zu\n",
+		 &strtab->dma, &strtab->dma_q, size);
+
+	return 0;
+}
+
+static int ivpu_mmu_cmdq_alloc(struct ivpu_device *vdev)
+{
+	struct ivpu_mmu_info *mmu = vdev->mmu;
+	struct ivpu_mmu_queue *q = &mmu->cmdq;
+
+	q->base = dmam_alloc_coherent(vdev->drm.dev, IVPU_MMU_CMDQ_SIZE, &q->dma, GFP_KERNEL);
+	if (!q->base)
+		return -ENOMEM;
+
+	q->dma_q = IVPU_MMU_Q_BASE_RWA;
+	q->dma_q |= q->dma & IVPU_MMU_Q_BASE_ADDR_MASK;
+	q->dma_q |= IVPU_MMU_Q_COUNT_LOG2;
+
+	ivpu_dbg(MMU, "CMDQ alloc: dma=%pad dma_q=%pad size=%u\n",
+		 &q->dma, &q->dma_q, IVPU_MMU_CMDQ_SIZE);
+
+	return 0;
+}
+
+static int ivpu_mmu_evtq_alloc(struct ivpu_device *vdev)
+{
+	struct ivpu_mmu_info *mmu = vdev->mmu;
+	struct ivpu_mmu_queue *q = &mmu->evtq;
+
+	q->base = dmam_alloc_coherent(vdev->drm.dev, IVPU_MMU_EVTQ_SIZE, &q->dma, GFP_KERNEL);
+	if (!q->base)
+		return -ENOMEM;
+
+	q->dma_q = IVPU_MMU_Q_BASE_RWA;
+	q->dma_q |= q->dma & IVPU_MMU_Q_BASE_ADDR_MASK;
+	q->dma_q |= IVPU_MMU_Q_COUNT_LOG2;
+
+	ivpu_dbg(MMU, "EVTQ alloc: dma=%pad dma_q=%pad size=%u\n",
+		 &q->dma, &q->dma_q, IVPU_MMU_EVTQ_SIZE);
+
+	return 0;
+}
+
+static int ivpu_mmu_structs_alloc(struct ivpu_device *vdev)
+{
+	int ret;
+
+	ret = ivpu_mmu_cdtab_alloc(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to allocate cdtab: %d\n", ret);
+		return ret;
+	}
+
+	ret = ivpu_mmu_strtab_alloc(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to allocate strtab: %d\n", ret);
+		return ret;
+	}
+
+	ret = ivpu_mmu_cmdq_alloc(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to allocate cmdq: %d\n", ret);
+		return ret;
+	}
+
+	ret = ivpu_mmu_evtq_alloc(vdev);
+	if (ret)
+		ivpu_err(vdev, "Failed to allocate evtq: %d\n", ret);
+
+	return ret;
+}
+
+static int ivpu_mmu_reg_write(struct ivpu_device *vdev, u32 reg, u32 val)
+{
+	u32 reg_ack = reg + 4; /* ACK register is 4B after base register */
+	u32 val_ack;
+	int ret;
+
+	REGV_WR32(reg, val);
+
+	ret = REGV_POLL(reg_ack, val_ack, (val == val_ack), IVPU_MMU_REG_TIMEOUT_US);
+	if (ret)
+		ivpu_err(vdev, "Failed to write register 0x%x\n", reg);
+
+	return ret;
+}
+
+static int ivpu_mmu_irqs_setup(struct ivpu_device *vdev)
+{
+	u32 irq_ctrl = IVPU_MMU_IRQ_EVTQ_EN | IVPU_MMU_IRQ_GERROR_EN;
+	int ret;
+
+	ret = ivpu_mmu_reg_write(vdev, MTL_VPU_HOST_MMU_IRQ_CTRL, 0);
+	if (ret)
+		return ret;
+
+	return ivpu_mmu_reg_write(vdev, MTL_VPU_HOST_MMU_IRQ_CTRL, irq_ctrl);
+}
+
+static int ivpu_mmu_cmdq_wait_for_cons(struct ivpu_device *vdev)
+{
+	struct ivpu_mmu_queue *cmdq = &vdev->mmu->cmdq;
+
+	return REGV_POLL(MTL_VPU_HOST_MMU_CMDQ_CONS, cmdq->cons, (cmdq->prod == cmdq->cons),
+			 IVPU_MMU_QUEUE_TIMEOUT_US);
+}
+
+static int ivpu_mmu_cmdq_cmd_write(struct ivpu_device *vdev, const char *name, u64 data0, u64 data1)
+{
+	struct ivpu_mmu_queue *q = &vdev->mmu->cmdq;
+	u64 *queue_buffer = q->base;
+	int idx = IVPU_MMU_Q_IDX(q->prod) * (IVPU_MMU_CMDQ_CMD_SIZE / sizeof(*queue_buffer));
+
+	if (!CIRC_SPACE(IVPU_MMU_Q_IDX(q->prod), IVPU_MMU_Q_IDX(q->cons), IVPU_MMU_Q_COUNT)) {
+		ivpu_err(vdev, "Failed to write MMU CMD %s\n", name);
+		return -EBUSY;
+	}
+
+	queue_buffer[idx] = data0;
+	queue_buffer[idx + 1] = data1;
+	q->prod = (q->prod + 1) & IVPU_MMU_Q_WRAP_MASK;
+
+	ivpu_dbg(MMU, "CMD write: %s data: 0x%llx 0x%llx\n", name, data0, data1);
+
+	return 0;
+}
+
+static int ivpu_mmu_cmdq_sync(struct ivpu_device *vdev)
+{
+	struct ivpu_mmu_queue *q = &vdev->mmu->cmdq;
+	u64 val;
+	int ret;
+
+	val = FIELD_PREP(IVPU_MMU_CMD_OPCODE, CMD_SYNC) |
+	      FIELD_PREP(IVPU_MMU_CMD_SYNC_0_CS, 0x2) |
+	      FIELD_PREP(IVPU_MMU_CMD_SYNC_0_MSH, 0x3) |
+	      FIELD_PREP(IVPU_MMU_CMD_SYNC_0_MSI_ATTR, 0xf);
+
+	ret = ivpu_mmu_cmdq_cmd_write(vdev, "SYNC", val, 0);
+	if (ret)
+		return ret;
+
+	clflush_cache_range(q->base, IVPU_MMU_CMDQ_SIZE);
+	REGV_WR32(MTL_VPU_HOST_MMU_CMDQ_PROD, q->prod);
+
+	ret = ivpu_mmu_cmdq_wait_for_cons(vdev);
+	if (ret)
+		ivpu_err(vdev, "Timed out waiting for consumer: %d\n", ret);
+
+	return ret;
+}
+
+static int ivpu_mmu_cmdq_write_cfgi_all(struct ivpu_device *vdev)
+{
+	u64 data0 = FIELD_PREP(IVPU_MMU_CMD_OPCODE, CMD_CFGI_ALL);
+	u64 data1 = FIELD_PREP(IVPU_MMU_CMD_CFGI_1_RANGE, 0x1f);
+
+	return ivpu_mmu_cmdq_cmd_write(vdev, "CFGI_ALL", data0, data1);
+}
+
+static int ivpu_mmu_cmdq_write_tlbi_nh_asid(struct ivpu_device *vdev, u16 ssid)
+{
+	u64 val = FIELD_PREP(IVPU_MMU_CMD_OPCODE, CMD_TLBI_NH_ASID) |
+		  FIELD_PREP(IVPU_MMU_CMD_TLBI_0_ASID, ssid);
+
+	return ivpu_mmu_cmdq_cmd_write(vdev, "TLBI_NH_ASID", val, 0);
+}
+
+static int ivpu_mmu_cmdq_write_tlbi_nsnh_all(struct ivpu_device *vdev)
+{
+	u64 val = FIELD_PREP(IVPU_MMU_CMD_OPCODE, CMD_TLBI_NSNH_ALL);
+
+	return ivpu_mmu_cmdq_cmd_write(vdev, "TLBI_NSNH_ALL", val, 0);
+}
+
+static int ivpu_mmu_reset(struct ivpu_device *vdev)
+{
+	struct ivpu_mmu_info *mmu = vdev->mmu;
+	u32 val;
+	int ret;
+
+	memset(mmu->cmdq.base, 0, IVPU_MMU_CMDQ_SIZE);
+	clflush_cache_range(mmu->cmdq.base, IVPU_MMU_CMDQ_SIZE);
+	mmu->cmdq.prod = 0;
+	mmu->cmdq.cons = 0;
+
+	memset(mmu->evtq.base, 0, IVPU_MMU_EVTQ_SIZE);
+	clflush_cache_range(mmu->evtq.base, IVPU_MMU_EVTQ_SIZE);
+	mmu->evtq.prod = 0;
+	mmu->evtq.cons = 0;
+
+	ret = ivpu_mmu_reg_write(vdev, MTL_VPU_HOST_MMU_CR0, 0);
+	if (ret)
+		return ret;
+
+	val = FIELD_PREP(IVPU_MMU_CR1_TABLE_SH, IVPU_MMU_SH_ISH) |
+	      FIELD_PREP(IVPU_MMU_CR1_TABLE_OC, IVPU_MMU_CACHE_WB) |
+	      FIELD_PREP(IVPU_MMU_CR1_TABLE_IC, IVPU_MMU_CACHE_WB) |
+	      FIELD_PREP(IVPU_MMU_CR1_QUEUE_SH, IVPU_MMU_SH_ISH) |
+	      FIELD_PREP(IVPU_MMU_CR1_QUEUE_OC, IVPU_MMU_CACHE_WB) |
+	      FIELD_PREP(IVPU_MMU_CR1_QUEUE_IC, IVPU_MMU_CACHE_WB);
+	REGV_WR32(MTL_VPU_HOST_MMU_CR1, val);
+
+	REGV_WR64(MTL_VPU_HOST_MMU_STRTAB_BASE, mmu->strtab.dma_q);
+	REGV_WR32(MTL_VPU_HOST_MMU_STRTAB_BASE_CFG, mmu->strtab.base_cfg);
+
+	REGV_WR64(MTL_VPU_HOST_MMU_CMDQ_BASE, mmu->cmdq.dma_q);
+	REGV_WR32(MTL_VPU_HOST_MMU_CMDQ_PROD, 0);
+	REGV_WR32(MTL_VPU_HOST_MMU_CMDQ_CONS, 0);
+
+	val = IVPU_MMU_CR0_CMDQEN;
+	ret = ivpu_mmu_reg_write(vdev, MTL_VPU_HOST_MMU_CR0, val);
+	if (ret)
+		return ret;
+
+	ret = ivpu_mmu_cmdq_write_cfgi_all(vdev);
+	if (ret)
+		return ret;
+
+	ret = ivpu_mmu_cmdq_write_tlbi_nsnh_all(vdev);
+	if (ret)
+		return ret;
+
+	ret = ivpu_mmu_cmdq_sync(vdev);
+	if (ret)
+		return ret;
+
+	REGV_WR64(MTL_VPU_HOST_MMU_EVTQ_BASE, mmu->evtq.dma_q);
+	REGV_WR32(MTL_VPU_HOST_MMU_EVTQ_PROD_SEC, 0);
+	REGV_WR32(MTL_VPU_HOST_MMU_EVTQ_CONS_SEC, 0);
+
+	val |= IVPU_MMU_CR0_EVTQEN;
+	ret = ivpu_mmu_reg_write(vdev, MTL_VPU_HOST_MMU_CR0, val);
+	if (ret)
+		return ret;
+
+	val |= IVPU_MMU_CR0_ATSCHK;
+	ret = ivpu_mmu_reg_write(vdev, MTL_VPU_HOST_MMU_CR0, val);
+	if (ret)
+		return ret;
+
+	ret = ivpu_mmu_irqs_setup(vdev);
+	if (ret)
+		return ret;
+
+	val |= IVPU_MMU_CR0_SMMUEN;
+	return ivpu_mmu_reg_write(vdev, MTL_VPU_HOST_MMU_CR0, val);
+}
+
+static void ivpu_mmu_strtab_link_cd(struct ivpu_device *vdev, u32 sid)
+{
+	struct ivpu_mmu_info *mmu = vdev->mmu;
+	struct ivpu_mmu_strtab *strtab = &mmu->strtab;
+	struct ivpu_mmu_cdtab *cdtab = &mmu->cdtab;
+	u64 *entry = strtab->base + (sid * IVPU_MMU_STRTAB_ENT_SIZE);
+	u64 str[2];
+
+	str[0] = FIELD_PREP(IVPU_MMU_STE_0_CFG, IVPU_MMU_STE_0_CFG_S1_TRANS) |
+		 FIELD_PREP(IVPU_MMU_STE_0_S1CDMAX, IVPU_MMU_CDTAB_ENT_COUNT_LOG2) |
+		 FIELD_PREP(IVPU_MMU_STE_0_S1FMT, IVPU_MMU_STE_0_S1FMT_LINEAR) |
+		 IVPU_MMU_STE_0_V |
+		 (cdtab->dma & IVPU_MMU_STE_0_S1CTXPTR_MASK);
+
+	str[1] = FIELD_PREP(IVPU_MMU_STE_1_S1DSS, IVPU_MMU_STE_1_S1DSS_TERMINATE) |
+		 FIELD_PREP(IVPU_MMU_STE_1_S1CIR, IVPU_MMU_STE_1_S1C_CACHE_NC) |
+		 FIELD_PREP(IVPU_MMU_STE_1_S1COR, IVPU_MMU_STE_1_S1C_CACHE_NC) |
+		 FIELD_PREP(IVPU_MMU_STE_1_S1CSH, IVPU_MMU_SH_NSH) |
+		 FIELD_PREP(IVPU_MMU_STE_1_PRIVCFG, IVPU_MMU_STE_1_PRIVCFG_UNPRIV) |
+		 FIELD_PREP(IVPU_MMU_STE_1_INSTCFG, IVPU_MMU_STE_1_INSTCFG_DATA) |
+		 FIELD_PREP(IVPU_MMU_STE_1_STRW, IVPU_MMU_STE_1_STRW_NSEL1) |
+		 FIELD_PREP(IVPU_MMU_STE_1_CONT, IVPU_MMU_STRTAB_CFG_LOG2SIZE) |
+		 IVPU_MMU_STE_1_MEV |
+		 IVPU_MMU_STE_1_S1STALLD;
+
+	WRITE_ONCE(entry[1], str[1]);
+	WRITE_ONCE(entry[0], str[0]);
+
+	clflush_cache_range(entry, IVPU_MMU_STRTAB_ENT_SIZE);
+
+	ivpu_dbg(MMU, "STRTAB write entry (SSID=%u): 0x%llx, 0x%llx\n",
+		 sid, str[0], str[1]);
+}
+
+static int ivpu_mmu_strtab_init(struct ivpu_device *vdev)
+{
+	ivpu_mmu_strtab_link_cd(vdev, IVPU_MMU_STREAM_ID0);
+	ivpu_mmu_strtab_link_cd(vdev, IVPU_MMU_STREAM_ID3);
+
+	return 0;
+}
+
+int ivpu_mmu_invalidate_tlb(struct ivpu_device *vdev, u16 ssid)
+{
+	struct ivpu_mmu_info *mmu = vdev->mmu;
+	int ret;
+
+	if (mutex_lock_interruptible(&mmu->lock))
+		return -EINTR;
+
+	if (!mmu->on) {
+		ret = 0;
+		goto unlock;
+	}
+
+	ret = ivpu_mmu_cmdq_write_tlbi_nh_asid(vdev, ssid);
+	if (ret)
+		goto unlock;
+
+	ret = ivpu_mmu_cmdq_sync(vdev);
+unlock:
+	mutex_unlock(&mmu->lock);
+	return ret;
+}
+
+static int ivpu_mmu_cd_add(struct ivpu_device *vdev, u32 ssid, u64 cd_dma)
+{
+	struct ivpu_mmu_info *mmu = vdev->mmu;
+	struct ivpu_mmu_cdtab *cdtab = &mmu->cdtab;
+	u64 *entry;
+	u64 cd[4];
+	int ret;
+
+	if (ssid > IVPU_MMU_CDTAB_ENT_COUNT)
+		return -EINVAL;
+
+	if (mutex_lock_interruptible(&mmu->lock))
+		return -EINTR;
+
+	entry = cdtab->base + (ssid * IVPU_MMU_CDTAB_ENT_SIZE);
+
+	if (cd_dma != 0) {
+		cd[0] = FIELD_PREP(IVPU_MMU_CD_0_TCR_T0SZ, 26) |
+			FIELD_PREP(IVPU_MMU_CD_0_TCR_TG0, 0) |
+			FIELD_PREP(IVPU_MMU_CD_0_TCR_IRGN0, 0) |
+			FIELD_PREP(IVPU_MMU_CD_0_TCR_ORGN0, 0) |
+			FIELD_PREP(IVPU_MMU_CD_0_TCR_SH0, 0) |
+			FIELD_PREP(IVPU_MMU_CD_0_TCR_IPS, 3) |
+			FIELD_PREP(IVPU_MMU_CD_0_ASID, ssid) |
+			IVPU_MMU_CD_0_TCR_EPD1 |
+			IVPU_MMU_CD_0_AA64 |
+			IVPU_MMU_CD_0_R |
+			IVPU_MMU_CD_0_A |
+			IVPU_MMU_CD_0_ASET |
+			IVPU_MMU_CD_0_V;
+		cd[1] = cd_dma & IVPU_MMU_CD_1_TTB0_MASK;
+		cd[2] = 0;
+		cd[3] = 0x0000000000007444;
+	} else {
+		memset(cd, 0, sizeof(cd));
+	}
+
+	WRITE_ONCE(entry[1], cd[1]);
+	WRITE_ONCE(entry[2], cd[2]);
+	WRITE_ONCE(entry[3], cd[3]);
+	WRITE_ONCE(entry[0], cd[0]);
+
+	clflush_cache_range(entry, IVPU_MMU_CDTAB_ENT_SIZE);
+
+	ivpu_dbg(MMU, "CDTAB %s entry (SSID=%u, dma=%pad): 0x%llx, 0x%llx, 0x%llx, 0x%llx\n",
+		 cd_dma ? "write" : "clear", ssid, &cd_dma, cd[0], cd[1], cd[2], cd[3]);
+
+	if (!mmu->on) {
+		ret = 0;
+		goto unlock;
+	}
+
+	ret = ivpu_mmu_cmdq_write_cfgi_all(vdev);
+	if (ret)
+		goto unlock;
+
+	ret = ivpu_mmu_cmdq_sync(vdev);
+unlock:
+	mutex_unlock(&mmu->lock);
+	return ret;
+}
+
+static int ivpu_mmu_cd_add_gbl(struct ivpu_device *vdev)
+{
+	int ret;
+
+	ret = ivpu_mmu_cd_add(vdev, 0, vdev->gctx.pgtable.pgd_dma);
+	if (ret)
+		ivpu_err(vdev, "Failed to add global CD entry: %d\n", ret);
+
+	return ret;
+}
+
+static int ivpu_mmu_cd_add_user(struct ivpu_device *vdev, u32 ssid, dma_addr_t cd_dma)
+{
+	int ret;
+
+	if (ssid == 0) {
+		ivpu_err(vdev, "Invalid SSID: %u\n", ssid);
+		return -EINVAL;
+	}
+

drivers/gpu/drm/ivpu/ivpu_mmu.c line 706

Quoted 10 lines
drivers/gpu/drm/ivpu/ivpu_mmu.c new file +10 -0
+	ret = ivpu_mmu_cd_add(vdev, ssid, cd_dma);
+	if (ret)
+		ivpu_err(vdev, "Failed to add CD entry SSID=%u: %d\n", ssid, ret);
+
+	return ret;
+}
+
+void ivpu_mmu_fini(struct ivpu_device *vdev)
+{
+	mutex_destroy(&vdev->mmu->lock);

We have drmm_mutex_init()

Best regards
Thomas

Quoted 738 lines
drivers/gpu/drm/ivpu/ivpu_mmu.c new file +177 -0
+}
+
+int ivpu_mmu_init(struct ivpu_device *vdev)
+{
+	struct ivpu_mmu_info *mmu = vdev->mmu;
+	int ret;
+
+	ivpu_dbg(MMU, "Init..\n");
+
+	mutex_init(&mmu->lock);
+
+	ret = ivpu_mmu_config_check(vdev);
+	if (ret)
+		goto err;
+
+	ret = ivpu_mmu_structs_alloc(vdev);
+	if (ret)
+		goto err;
+
+	ret = ivpu_mmu_strtab_init(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to initialize strtab: %d\n", ret);
+		goto err;
+	}
+
+	ret = ivpu_mmu_cd_add_gbl(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to initialize strtab: %d\n", ret);
+		goto err;
+	}
+
+	ret = ivpu_mmu_enable(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to resume MMU: %d\n", ret);
+		goto err;
+	}
+
+	ivpu_dbg(MMU, "Init done\n");
+
+	return 0;
+
+err:
+	ivpu_mmu_fini(vdev);
+	return ret;
+}
+
+int ivpu_mmu_enable(struct ivpu_device *vdev)
+{
+	struct ivpu_mmu_info *mmu = vdev->mmu;
+	int ret;
+
+	mutex_lock(&mmu->lock);
+
+	mmu->on = true;
+
+	ret = ivpu_mmu_reset(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to reset MMU: %d\n", ret);
+		goto err;
+	}
+
+	ret = ivpu_mmu_cmdq_write_cfgi_all(vdev);
+	if (ret)
+		goto err;
+
+	ret = ivpu_mmu_cmdq_write_tlbi_nsnh_all(vdev);
+	if (ret)
+		goto err;
+
+	ret = ivpu_mmu_cmdq_sync(vdev);
+	if (ret)
+		goto err;
+
+	mutex_unlock(&mmu->lock);
+
+	return 0;
+err:
+	mmu->on = false;
+	mutex_unlock(&mmu->lock);
+	return ret;
+}
+
+void ivpu_mmu_disable(struct ivpu_device *vdev)
+{
+	struct ivpu_mmu_info *mmu = vdev->mmu;
+
+	mutex_lock(&mmu->lock);
+	mmu->on = false;
+	mutex_unlock(&mmu->lock);
+}
+
+irqreturn_t ivpu_mmu_irq_evtq_handler(struct ivpu_device *vdev)
+{
+	struct ivpu_mmu_queue *evtq = &vdev->mmu->evtq;
+	u64 in_addr, fetch_addr;
+	u32 *evt, op, ssid, sid, counter = 0;
+
+	ivpu_dbg(IRQ, "MMU event queue\n");
+
+	do {
+		evt = (evtq->base + (IVPU_MMU_Q_IDX(evtq->cons) * IVPU_MMU_EVTQ_CMD_SIZE));
+		clflush_cache_range(evt, IVPU_MMU_EVTQ_CMD_SIZE);
+
+		op = FIELD_GET(IVPU_MMU_EVT_OP_MASK, evt[0]);
+		ssid = FIELD_GET(IVPU_MMU_EVT_SSID_MASK, evt[0]);
+		sid = evt[1];
+		in_addr = ((u64)evt[5]) << 32 | evt[4];
+		fetch_addr = ((u64)evt[7]) << 32 | evt[6];
+
+		ivpu_err(vdev, "MMU EVTQ: 0x%x (%s) SSID: %d SID: %d, e[2] %08x, e[3] %08x, in addr: 0x%llx, fetch addr: 0x%llx\n",
+			 op, ivpu_mmu_evt_to_str(op), ssid, sid, evt[2], evt[3],
+			 in_addr, fetch_addr);
+		evtq->cons = (evtq->cons + 1) & IVPU_MMU_Q_WRAP_MASK;
+
+		REGV_WR32(MTL_VPU_HOST_MMU_EVTQ_CONS_SEC, evtq->cons);
+
+		evtq->prod = REGV_RD32(MTL_VPU_HOST_MMU_EVTQ_PROD_SEC);
+
+		if (counter++ >= IVPU_MMU_EVTS_MAX)
+			break;
+
+	} while (evtq->prod != evtq->cons);
+
+	return IRQ_HANDLED;
+}
+
+irqreturn_t ivpu_mmu_irq_gerr_handler(struct ivpu_device *vdev)
+{
+	u32 gerror_val, gerrorn_val, active;
+
+	ivpu_dbg(IRQ, "MMU error\n");
+
+	gerror_val = REGV_RD32(MTL_VPU_HOST_MMU_GERROR);
+	gerrorn_val = REGV_RD32(MTL_VPU_HOST_MMU_GERRORN);
+
+	active = gerror_val ^ gerrorn_val;
+	if (!(active & IVPU_MMU_GERROR_ERR_MASK))
+		return IRQ_NONE;
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_MMU_GERROR, SFM, active))
+		ivpu_err_ratelimited(vdev, "MMU has entered service failure mode\n");
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_ABT, active))
+		ivpu_warn_ratelimited(vdev, "MMU MSI ABT write aborted\n");
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_PRIQ_ABT, active))
+		ivpu_warn_ratelimited(vdev, "MMU PRIQ MSI ABT write aborted\n");
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_EVTQ_ABT, active))
+		ivpu_warn_ratelimited(vdev, "MMU EVTQ MSI ABT write aborted\n");
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_CMDQ_ABT, active))
+		ivpu_warn_ratelimited(vdev, "MMU CMDQ MSI ABT write aborted\n");
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_MMU_GERROR, PRIQ_ABT, active))
+		ivpu_err_ratelimited(vdev, "MMU PRIQ write aborted\n");
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_MMU_GERROR, EVTQ_ABT, active))
+		ivpu_err_ratelimited(vdev, "MMU EVTQ write aborted\n");
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_MMU_GERROR, CMDQ, active))
+		ivpu_err_ratelimited(vdev, "MMU CMDQ write aborted\n");
+
+	REGV_WR32(MTL_VPU_HOST_MMU_GERRORN, gerror_val);
+
+	return IRQ_HANDLED;
+}
+
+int ivpu_mmu_set_pgtable(struct ivpu_device *vdev, int ssid, struct ivpu_mmu_pgtable *pgtable)
+{
+	return ivpu_mmu_cd_add_user(vdev, ssid, pgtable->pgd_dma);
+}
+
+void ivpu_mmu_clear_pgtable(struct ivpu_device *vdev, int ssid)
+{
+	ivpu_mmu_cd_add_user(vdev, ssid, 0); /* 0 will clear CD entry */
+}
drivers/gpu/drm/ivpu/ivpu_mmu.h new file +53 -0
new file mode 100644
index 000000000000..af671c312e63
@@ -0,0 +1,53 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#ifndef __IVPU_MMU_H__
+#define __IVPU_MMU_H__
+
+#include <linux/irqreturn.h>
+
+struct ivpu_device;
+
+struct ivpu_mmu_cdtab {
+	void *base;
+	dma_addr_t dma;
+};
+
+struct ivpu_mmu_strtab {
+	void *base;
+	dma_addr_t dma;
+	u64 dma_q;
+	u32 base_cfg;
+};
+
+struct ivpu_mmu_queue {
+	void *base;
+	dma_addr_t dma;
+	u64 dma_q;
+	u32 prod;
+	u32 cons;
+};
+
+struct ivpu_mmu_info {
+	struct mutex lock; /* Protects cdtab, strtab, cmdq, on */
+	struct ivpu_mmu_cdtab cdtab;
+	struct ivpu_mmu_strtab strtab;
+	struct ivpu_mmu_queue cmdq;
+	struct ivpu_mmu_queue evtq;
+	bool on;
+};
+
+int ivpu_mmu_init(struct ivpu_device *vdev);
+void ivpu_mmu_fini(struct ivpu_device *vdev);
+void ivpu_mmu_disable(struct ivpu_device *vdev);
+int ivpu_mmu_enable(struct ivpu_device *vdev);
+int ivpu_mmu_set_pgtable(struct ivpu_device *vdev, int ssid, struct ivpu_mmu_pgtable *pgtable);
+void ivpu_mmu_clear_pgtable(struct ivpu_device *vdev, int ssid);
+int ivpu_mmu_invalidate_tlb(struct ivpu_device *vdev, u16 ssid);
+
+irqreturn_t ivpu_mmu_irq_evtq_handler(struct ivpu_device *vdev);
+irqreturn_t ivpu_mmu_irq_gerr_handler(struct ivpu_device *vdev);
+
+#endif /* __IVPU_MMU_H__ */
drivers/gpu/drm/ivpu/ivpu_mmu_context.c new file +419 -0
new file mode 100644
index 000000000000..3849ff200450
@@ -0,0 +1,419 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#include <linux/bitfield.h>
+#include <linux/highmem.h>
+
+#include "ivpu_drv.h"
+#include "ivpu_hw.h"
+#include "ivpu_mmu.h"
+#include "ivpu_mmu_context.h"
+
+#define IVPU_MMU_PGD_INDEX_MASK          GENMASK(38, 30)
+#define IVPU_MMU_PMD_INDEX_MASK          GENMASK(29, 21)
+#define IVPU_MMU_PTE_INDEX_MASK          GENMASK(20, 12)
+#define IVPU_MMU_ENTRY_FLAGS_MASK        GENMASK(11, 0)
+#define IVPU_MMU_ENTRY_FLAG_NG           BIT(11)
+#define IVPU_MMU_ENTRY_FLAG_AF           BIT(10)
+#define IVPU_MMU_ENTRY_FLAG_USER         BIT(6)
+#define IVPU_MMU_ENTRY_FLAG_LLC_COHERENT BIT(2)
+#define IVPU_MMU_ENTRY_FLAG_TYPE_PAGE    BIT(1)
+#define IVPU_MMU_ENTRY_FLAG_VALID        BIT(0)
+
+#define IVPU_MMU_PAGE_SIZE    SZ_4K
+#define IVPU_MMU_PTE_MAP_SIZE (IVPU_MMU_PGTABLE_ENTRIES * IVPU_MMU_PAGE_SIZE)
+#define IVPU_MMU_PMD_MAP_SIZE (IVPU_MMU_PGTABLE_ENTRIES * IVPU_MMU_PTE_MAP_SIZE)
+#define IVPU_MMU_PGTABLE_SIZE (IVPU_MMU_PGTABLE_ENTRIES * sizeof(u64))
+
+#define IVPU_MMU_DUMMY_ADDRESS 0xdeadb000
+#define IVPU_MMU_ENTRY_VALID   (IVPU_MMU_ENTRY_FLAG_TYPE_PAGE | IVPU_MMU_ENTRY_FLAG_VALID)
+#define IVPU_MMU_ENTRY_INVALID (IVPU_MMU_DUMMY_ADDRESS & ~IVPU_MMU_ENTRY_FLAGS_MASK)
+#define IVPU_MMU_ENTRY_MAPPED  (IVPU_MMU_ENTRY_FLAG_AF | IVPU_MMU_ENTRY_FLAG_USER | \
+				IVPU_MMU_ENTRY_FLAG_NG | IVPU_MMU_ENTRY_VALID)
+
+static int ivpu_mmu_pgtable_init(struct ivpu_device *vdev, struct ivpu_mmu_pgtable *pgtable)
+{
+	dma_addr_t pgd_dma;
+	u64 *pgd;
+
+	pgd = dma_alloc_wc(vdev->drm.dev, IVPU_MMU_PGTABLE_SIZE, &pgd_dma, GFP_KERNEL);
+	if (!pgd)
+		return -ENOMEM;
+
+	pgtable->pgd = pgd;
+	pgtable->pgd_dma = pgd_dma;
+
+	return 0;
+}
+
+static void ivpu_mmu_pgtable_free(struct ivpu_device *vdev, struct ivpu_mmu_pgtable *pgtable)
+{
+	int pgd_index, pmd_index;
+
+	for (pgd_index = 0; pgd_index < IVPU_MMU_PGTABLE_ENTRIES; ++pgd_index) {
+		u64 **pmd_entries = pgtable->pgd_cpu_entries[pgd_index];
+		u64 *pmd = pgtable->pgd_entries[pgd_index];
+
+		if (!pmd_entries)
+			continue;
+
+		for (pmd_index = 0; pmd_index < IVPU_MMU_PGTABLE_ENTRIES; ++pmd_index) {
+			if (pmd_entries[pmd_index])
+				dma_free_wc(vdev->drm.dev, IVPU_MMU_PGTABLE_SIZE,
+					    pmd_entries[pmd_index],
+					    pmd[pmd_index] & ~IVPU_MMU_ENTRY_FLAGS_MASK);
+		}
+
+		kfree(pmd_entries);
+		dma_free_wc(vdev->drm.dev, IVPU_MMU_PGTABLE_SIZE, pgtable->pgd_entries[pgd_index],
+			    pgtable->pgd[pgd_index] & ~IVPU_MMU_ENTRY_FLAGS_MASK);
+	}
+
+	dma_free_wc(vdev->drm.dev, IVPU_MMU_PGTABLE_SIZE, pgtable->pgd,
+		    pgtable->pgd_dma & ~IVPU_MMU_ENTRY_FLAGS_MASK);
+}
+
+static u64*
+ivpu_mmu_ensure_pmd(struct ivpu_device *vdev, struct ivpu_mmu_pgtable *pgtable, u64 pgd_index)
+{
+	u64 **pmd_entries;
+	dma_addr_t pmd_dma;
+	u64 *pmd;
+
+	if (pgtable->pgd_entries[pgd_index])
+		return pgtable->pgd_entries[pgd_index];
+
+	pmd = dma_alloc_wc(vdev->drm.dev, IVPU_MMU_PGTABLE_SIZE, &pmd_dma, GFP_KERNEL);
+	if (!pmd)
+		return NULL;
+
+	pmd_entries = kzalloc(IVPU_MMU_PGTABLE_SIZE, GFP_KERNEL);
+	if (!pmd_entries)
+		goto err_free_pgd;
+
+	pgtable->pgd_entries[pgd_index] = pmd;
+	pgtable->pgd_cpu_entries[pgd_index] = pmd_entries;
+	pgtable->pgd[pgd_index] = pmd_dma | IVPU_MMU_ENTRY_VALID;
+
+	return pmd;
+
+err_free_pgd:
+	dma_free_wc(vdev->drm.dev, IVPU_MMU_PGTABLE_SIZE, pmd, pmd_dma);
+	return NULL;
+}
+
+static u64*
+ivpu_mmu_ensure_pte(struct ivpu_device *vdev, struct ivpu_mmu_pgtable *pgtable,
+		    int pgd_index, int pmd_index)
+{
+	dma_addr_t pte_dma;
+	u64 *pte;
+
+	if (pgtable->pgd_cpu_entries[pgd_index][pmd_index])
+		return pgtable->pgd_cpu_entries[pgd_index][pmd_index];
+
+	pte = dma_alloc_wc(vdev->drm.dev, IVPU_MMU_PGTABLE_SIZE, &pte_dma, GFP_KERNEL);
+	if (!pte)
+		return NULL;
+
+	pgtable->pgd_cpu_entries[pgd_index][pmd_index] = pte;
+	pgtable->pgd_entries[pgd_index][pmd_index] = pte_dma | IVPU_MMU_ENTRY_VALID;
+
+	return pte;
+}
+
+static int
+ivpu_mmu_context_map_page(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,
+			  u64 vpu_addr, dma_addr_t dma_addr, int prot)
+{
+	u64 *pte;
+	int pgd_index = FIELD_GET(IVPU_MMU_PGD_INDEX_MASK, vpu_addr);
+	int pmd_index = FIELD_GET(IVPU_MMU_PMD_INDEX_MASK, vpu_addr);
+	int pte_index = FIELD_GET(IVPU_MMU_PTE_INDEX_MASK, vpu_addr);
+
+	/* Allocate PMD - second level page table if needed */
+	if (!ivpu_mmu_ensure_pmd(vdev, &ctx->pgtable, pgd_index))
+		return -ENOMEM;
+
+	/* Allocate PTE - third level page table if needed */
+	pte = ivpu_mmu_ensure_pte(vdev, &ctx->pgtable, pgd_index, pmd_index);
+	if (!pte)
+		return -ENOMEM;
+
+	/* Update PTE - third level page table with DMA address */
+	pte[pte_index] = dma_addr | prot;
+
+	return 0;
+}
+
+static void ivpu_mmu_context_unmap_page(struct ivpu_mmu_context *ctx, u64 vpu_addr)
+{
+	int pgd_index = FIELD_GET(IVPU_MMU_PGD_INDEX_MASK, vpu_addr);
+	int pmd_index = FIELD_GET(IVPU_MMU_PMD_INDEX_MASK, vpu_addr);
+	int pte_index = FIELD_GET(IVPU_MMU_PTE_INDEX_MASK, vpu_addr);
+
+	/* Update PTE with dummy physical address and clear flags */
+	ctx->pgtable.pgd_cpu_entries[pgd_index][pmd_index][pte_index] = IVPU_MMU_ENTRY_INVALID;
+}
+
+static void
+ivpu_mmu_context_flush_page_tables(struct ivpu_mmu_context *ctx, u64 vpu_addr, size_t size)
+{
+	u64 end_addr = vpu_addr + size;
+	u64 *pgd = ctx->pgtable.pgd;
+
+	/* Align to PMD entry (2 MB) */
+	vpu_addr &= ~(IVPU_MMU_PTE_MAP_SIZE - 1);
+
+	while (vpu_addr < end_addr) {
+		int pgd_index = FIELD_GET(IVPU_MMU_PGD_INDEX_MASK, vpu_addr);
+		u64 pmd_end = (pgd_index + 1) * (u64)IVPU_MMU_PMD_MAP_SIZE;
+		u64 *pmd = ctx->pgtable.pgd_entries[pgd_index];
+
+		while (vpu_addr < end_addr && vpu_addr < pmd_end) {
+			int pmd_index = FIELD_GET(IVPU_MMU_PMD_INDEX_MASK, vpu_addr);
+			u64 *pte = ctx->pgtable.pgd_cpu_entries[pgd_index][pmd_index];
+
+			clflush_cache_range(pte, IVPU_MMU_PGTABLE_SIZE);
+			vpu_addr += IVPU_MMU_PTE_MAP_SIZE;
+		}
+		clflush_cache_range(pmd, IVPU_MMU_PGTABLE_SIZE);
+	}
+	clflush_cache_range(pgd, IVPU_MMU_PGTABLE_SIZE);
+}
+
+static int
+ivpu_mmu_context_map_pages(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,
+			   u64 vpu_addr, dma_addr_t dma_addr, size_t size, int prot)
+{
+	while (size) {
+		int ret = ivpu_mmu_context_map_page(vdev, ctx, vpu_addr, dma_addr, prot);
+
+		if (ret)
+			return ret;
+
+		vpu_addr += IVPU_MMU_PAGE_SIZE;
+		dma_addr += IVPU_MMU_PAGE_SIZE;
+		size -= IVPU_MMU_PAGE_SIZE;
+	}
+
+	return 0;
+}
+
+static void ivpu_mmu_context_unmap_pages(struct ivpu_mmu_context *ctx, u64 vpu_addr, size_t size)
+{
+	while (size) {
+		ivpu_mmu_context_unmap_page(ctx, vpu_addr);
+		vpu_addr += IVPU_MMU_PAGE_SIZE;
+		size -= IVPU_MMU_PAGE_SIZE;
+	}
+}
+
+int
+ivpu_mmu_context_map_sgt(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,
+			 u64 vpu_addr, struct sg_table *sgt,  bool llc_coherent)
+{
+	struct scatterlist *sg;
+	int prot;
+	int ret;
+	u64 i;
+
+	if (!IS_ALIGNED(vpu_addr, IVPU_MMU_PAGE_SIZE))
+		return -EINVAL;
+	/*
+	 * VPU is only 32 bit, but DMA engine is 38 bit
+	 * Ranges < 2 GB are reserved for VPU internal registers
+	 * Limit range to 8 GB
+	 */
+	if (vpu_addr < SZ_2G || vpu_addr > SZ_8G)
+		return -EINVAL;
+
+	prot = IVPU_MMU_ENTRY_MAPPED;
+	if (llc_coherent)
+		prot |= IVPU_MMU_ENTRY_FLAG_LLC_COHERENT;
+
+	mutex_lock(&ctx->lock);
+
+	for_each_sgtable_dma_sg(sgt, sg, i) {
+		u64 dma_addr = sg_dma_address(sg) - sg->offset;
+		size_t size = sg_dma_len(sg) + sg->offset;
+
+		ret = ivpu_mmu_context_map_pages(vdev, ctx, vpu_addr, dma_addr, size, prot);
+		if (ret) {
+			ivpu_err(vdev, "Failed to map context pages\n");
+			mutex_unlock(&ctx->lock);
+			return ret;
+		}
+		ivpu_mmu_context_flush_page_tables(ctx, vpu_addr, size);
+		vpu_addr += size;
+	}
+
+	mutex_unlock(&ctx->lock);
+
+	ret = ivpu_mmu_invalidate_tlb(vdev, ctx->id);
+	if (ret)
+		ivpu_err(vdev, "Failed to invalidate TLB for ctx %u: %d\n", ctx->id, ret);
+	return ret;
+}
+
+void
+ivpu_mmu_context_unmap_sgt(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,
+			   u64 vpu_addr, struct sg_table *sgt)
+{
+	struct scatterlist *sg;
+	int ret;
+	u64 i;
+
+	if (!IS_ALIGNED(vpu_addr, IVPU_MMU_PAGE_SIZE))
+		ivpu_warn(vdev, "Unaligned vpu_addr: 0x%llx\n", vpu_addr);
+
+	mutex_lock(&ctx->lock);
+
+	for_each_sgtable_dma_sg(sgt, sg, i) {
+		size_t size = sg_dma_len(sg) + sg->offset;
+
+		ivpu_mmu_context_unmap_pages(ctx, vpu_addr, size);
+		ivpu_mmu_context_flush_page_tables(ctx, vpu_addr, size);
+		vpu_addr += size;
+	}
+
+	mutex_unlock(&ctx->lock);
+
+	ret = ivpu_mmu_invalidate_tlb(vdev, ctx->id);
+	if (ret)
+		ivpu_warn(vdev, "Failed to invalidate TLB for ctx %u: %d\n", ctx->id, ret);
+}
+
+int
+ivpu_mmu_context_insert_node_locked(struct ivpu_mmu_context *ctx,
+				    const struct ivpu_addr_range *range,
+				    u64 size, struct drm_mm_node *node)
+{
+	lockdep_assert_held(&ctx->lock);
+
+	return drm_mm_insert_node_in_range(&ctx->mm, node, size, IVPU_MMU_PAGE_SIZE,
+					  0, range->start, range->end, DRM_MM_INSERT_BEST);
+}
+
+void
+ivpu_mmu_context_remove_node_locked(struct ivpu_mmu_context *ctx, struct drm_mm_node *node)
+{
+	lockdep_assert_held(&ctx->lock);
+
+	drm_mm_remove_node(node);
+}
+
+static int
+ivpu_mmu_context_init(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx, u32 context_id)
+{
+	u64 start, end;
+	int ret;
+
+	mutex_init(&ctx->lock);
+	INIT_LIST_HEAD(&ctx->bo_list);
+
+	ret = ivpu_mmu_pgtable_init(vdev, &ctx->pgtable);
+	if (ret)
+		return ret;
+
+	if (!context_id) {
+		start = vdev->hw->ranges.global_low.start;
+		end = vdev->hw->ranges.global_high.end;
+	} else {
+		start = vdev->hw->ranges.user_low.start;
+		end = vdev->hw->ranges.user_high.end;
+	}
+
+	drm_mm_init(&ctx->mm, start, end - start);
+	ctx->id = context_id;
+
+	return 0;
+}
+
+static void ivpu_mmu_context_fini(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx)
+{
+	WARN_ON(!ctx->pgtable.pgd);
+
+	mutex_destroy(&ctx->lock);
+	ivpu_mmu_pgtable_free(vdev, &ctx->pgtable);
+	drm_mm_takedown(&ctx->mm);
+}
+
+int ivpu_mmu_global_context_init(struct ivpu_device *vdev)
+{
+	return ivpu_mmu_context_init(vdev, &vdev->gctx, IVPU_GLOBAL_CONTEXT_MMU_SSID);
+}
+
+void ivpu_mmu_global_context_fini(struct ivpu_device *vdev)
+{
+	return ivpu_mmu_context_fini(vdev, &vdev->gctx);
+}
+
+int ivpu_mmu_user_context_init(struct ivpu_file_priv *file_priv)
+{
+	struct ivpu_device *vdev = file_priv->vdev;
+	u32 context_id;
+	void *old;
+	int ret;
+
+	mutex_lock(&file_priv->lock);
+
+	if (file_priv->ctx.id)
+		goto unlock;
+
+	ret = xa_alloc(&vdev->context_xa, &context_id, NULL, vdev->context_xa_limit, GFP_KERNEL);
+	if (ret) {
+		ivpu_err(vdev, "Failed to allocate context_id\n");
+		goto err_unlock;
+	}
+
+	ret = ivpu_mmu_context_init(vdev, &file_priv->ctx, context_id);
+	if (ret) {
+		ivpu_err(vdev, "Failed to initialize context\n");
+		goto err_erase_context_id;
+	}
+
+	ret = ivpu_mmu_set_pgtable(vdev, context_id, &file_priv->ctx.pgtable);
+	if (ret) {
+		ivpu_err(vdev, "Failed to set page table\n");
+		goto err_context_fini;
+	}
+
+	old = xa_store(&vdev->context_xa, context_id, file_priv, GFP_KERNEL);
+	if (xa_is_err(old)) {
+		ret = xa_err(old);
+		ivpu_err(vdev, "Failed to store context %u: %d\n", context_id, ret);
+		goto err_clear_pgtable;
+	}
+
+	ivpu_dbg(FILE, "file_priv context init: id %u process %s pid %d\n",
+		 context_id, current->comm, task_pid_nr(current));
+
+unlock:
+	mutex_unlock(&file_priv->lock);
+	return 0;
+
+err_clear_pgtable:
+	ivpu_mmu_clear_pgtable(vdev, context_id);
+err_context_fini:
+	ivpu_mmu_context_fini(vdev, &file_priv->ctx);
+err_erase_context_id:
+	xa_erase(&vdev->context_xa, context_id);
+err_unlock:
+	mutex_unlock(&file_priv->lock);
+	return ret;
+}
+
+void ivpu_mmu_user_context_fini(struct ivpu_file_priv *file_priv)
+{
+	struct ivpu_device *vdev = file_priv->vdev;
+
+	WARN_ON(!file_priv->ctx.id);
+
+	xa_store(&vdev->context_xa, file_priv->ctx.id,  NULL, GFP_KERNEL);
+	ivpu_mmu_clear_pgtable(vdev, file_priv->ctx.id);
+	ivpu_mmu_context_fini(vdev, &file_priv->ctx);
+	xa_erase(&vdev->context_xa, file_priv->ctx.id);
+}
drivers/gpu/drm/ivpu/ivpu_mmu_context.h new file +49 -0
new file mode 100644
index 000000000000..af163dfb0e04
@@ -0,0 +1,49 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#ifndef __IVPU_MMU_CONTEXT_H__
+#define __IVPU_MMU_CONTEXT_H__
+
+#include <drm/drm_mm.h>
+
+struct ivpu_device;
+struct ivpu_file_priv;
+struct ivpu_addr_range;
+
+#define IVPU_MMU_PGTABLE_ENTRIES	512
+
+struct ivpu_mmu_pgtable {
+	u64             **pgd_cpu_entries[IVPU_MMU_PGTABLE_ENTRIES];
+	u64		*pgd_entries[IVPU_MMU_PGTABLE_ENTRIES];
+	u64		*pgd;
+	dma_addr_t	pgd_dma;
+};
+
+struct ivpu_mmu_context {
+	struct mutex lock; /* protects: mm, pgtable, bo_list */
+	struct drm_mm mm;
+	struct ivpu_mmu_pgtable pgtable;
+	struct list_head bo_list;
+	u32 id;
+};
+
+int ivpu_mmu_global_context_init(struct ivpu_device *vdev);
+void ivpu_mmu_global_context_fini(struct ivpu_device *vdev);
+
+int ivpu_mmu_user_context_init(struct ivpu_file_priv *file_priv);
+void ivpu_mmu_user_context_fini(struct ivpu_file_priv *file_priv);
+
+int ivpu_mmu_context_insert_node_locked(struct ivpu_mmu_context *ctx,
+					const struct ivpu_addr_range *range,
+				       u64 size, struct drm_mm_node *node);
+void ivpu_mmu_context_remove_node_locked(struct ivpu_mmu_context *ctx,
+					 struct drm_mm_node *node);
+
+int ivpu_mmu_context_map_sgt(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,
+			     u64 vpu_addr, struct sg_table *sgt, bool llc_coherent);
+void ivpu_mmu_context_unmap_sgt(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,
+				u64 vpu_addr, struct sg_table *sgt);
+
+#endif /* __IVPU_MMU_CONTEXT_H__ */
include/uapi/drm/ivpu_drm.h +4 -0
index 7a7dbe2f521b..cb48d6118412 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 · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Maxfeldstr. 5, 90409 Nürnberg, Germany
(HRB 36809, AG Nürnberg)
Geschäftsführer: Ivo Totev
#17 lore raw

Re: [PATCH v3 2/7] drm/ivpu: Add Intel VPU MMU support

Hi,

Quoted 181 lines · Thomas Zimmermann

Hi

Am 24.09.22 um 17:11 schrieb Jacek Lawrynowicz:

Quoted 178 lines

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

Address space is implemented as a struct 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 to 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/gpu/drm/ivpu/Makefile           |   4 +- drivers/gpu/drm/ivpu/ivpu_drv.c         |  59 +- drivers/gpu/drm/ivpu/ivpu_drv.h         |   7 + drivers/gpu/drm/ivpu/ivpu_hw_mtl.c      |  10 + drivers/gpu/drm/ivpu/ivpu_mmu.c         | 883 ++++++++++++++++++++++++ drivers/gpu/drm/ivpu/ivpu_mmu.h         |  53 ++ drivers/gpu/drm/ivpu/ivpu_mmu_context.c | 419 +++++++++++ drivers/gpu/drm/ivpu/ivpu_mmu_context.h |  49 ++ include/uapi/drm/ivpu_drm.h             |   4 +

  9 files changed, 1485 insertions(+), 3 deletions(-)

  create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu_context.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu_context.h

drivers/gpu/drm/ivpu/Makefile +3 -1
index e59dc65abe6a..95bb04f26296 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_IVPU) += intel_vpu.o

drivers/gpu/drm/ivpu/ivpu_drv.c +22 -1
index a01c7244f6e5..cbeb9a801a31 100644
@@ -14,6 +14,8 @@
     #include "ivpu_drv.h"
   #include "ivpu_hw.h"
+#include "ivpu_mmu.h"
+#include "ivpu_mmu_context.h"
     #ifndef DRIVER_VERSION_STR
   #define DRIVER_VERSION_STR __stringify(DRM_IVPU_DRIVER_MAJOR) "." \
@@ -50,6 +52,11 @@ char *ivpu_platform_to_str(u32 platform)
     void ivpu_file_priv_get(struct ivpu_file_priv *file_priv, struct ivpu_file_priv **link)
   {
+    struct ivpu_device *vdev = file_priv->vdev;
+
+    ivpu_dbg(KREF, "file_priv get: ctx %u refcount %u\n",
+         file_priv->ctx.id, kref_read(&file_priv->ref));
+
       kref_get(&file_priv->ref);
       *link = file_priv;
   }
@@ -57,6 +64,12 @@ void ivpu_file_priv_get(struct ivpu_file_priv *file_priv, struct ivpu_file_priv
   static void file_priv_release(struct kref *ref)
   {
       struct ivpu_file_priv *file_priv = container_of(ref, struct ivpu_file_priv, ref);
+    struct ivpu_device *vdev = file_priv->vdev;
+
+    ivpu_dbg(FILE, "file_priv release: ctx %u\n", file_priv->ctx.id);
+
+    if (file_priv->ctx.id)
+        ivpu_mmu_user_context_fini(file_priv);
         kfree(file_priv);
   }
@@ -64,6 +77,10 @@ static void file_priv_release(struct kref *ref)
   void ivpu_file_priv_put(struct ivpu_file_priv **link)
   {
       struct ivpu_file_priv *file_priv = *link;
+    struct ivpu_device *vdev = file_priv->vdev;
+
+    ivpu_dbg(KREF, "file_priv put: ctx %u refcount %u\n",
+         file_priv->ctx.id, kref_read(&file_priv->ref));
         *link = NULL;
       kref_put(&file_priv->ref, file_priv_release);
@@ -75,7 +92,11 @@ static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_f
       struct ivpu_device *vdev = file_priv->vdev;
       struct pci_dev *pdev = to_pci_dev(vdev->drm.dev);
       struct drm_ivpu_param *args = data;
-    int ret = 0;
+    int ret;
+
+    ret = ivpu_mmu_user_context_init(file_priv);
+    if (ret)
+        return ret;
         switch (args->param) {

      case DRM_IVPU_PARAM_DEVICE_ID:

drivers/gpu/drm/ivpu/ivpu_drv.c +0 -0
@@ -99,6 +120,9 @@ static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_f

      case DRM_IVPU_PARAM_CONTEXT_PRIORITY:
          args->value = file_priv->priority;
          break;
+    case DRM_IVPU_PARAM_CONTEXT_ID:
+        args->value = file_priv->ctx.id;
+        break;
      default:
          ret = -EINVAL;
      }

drivers/gpu/drm/ivpu/ivpu_drv.c +5 -1
@@ -110,7 +134,11 @@ static int ivpu_set_param_ioctl(struct drm_device *dev, void *data, struct drm_f
   {
       struct ivpu_file_priv *file_priv = file->driver_priv;
       struct drm_ivpu_param *args = data;
-    int ret = 0;
+    int ret;
+
+    ret = ivpu_mmu_user_context_init(file_priv);
+    if (ret)
+        return ret;
         switch (args->param) {

      case DRM_IVPU_PARAM_CONTEXT_PRIORITY:

drivers/gpu/drm/ivpu/ivpu_drv.c +4 -0
@@ -139,9 +167,13 @@ static int ivpu_open(struct drm_device *dev, struct drm_file *file)
       file_priv->priority = DRM_IVPU_CONTEXT_PRIORITY_NORMAL;
         kref_init(&file_priv->ref);
+    mutex_init(&file_priv->lock);
         file->driver_priv = file_priv;
   +    ivpu_dbg(FILE, "file_priv alloc: process %s pid %d\n",
+         current->comm, task_pid_nr(current));
+
       return 0;
   }
   @@ -164,6 +196,7 @@ int ivpu_shutdown(struct ivpu_device *vdev)
       int ret;
         ivpu_hw_irq_disable(vdev);
+    ivpu_mmu_disable(vdev);

        ret = ivpu_hw_power_down(vdev);
      if (ret)

drivers/gpu/drm/ivpu/ivpu_drv.c +3 -0
@@ -272,6 +305,10 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
       if (!vdev->hw)
           return -ENOMEM;
   +    vdev->mmu = devm_kzalloc(vdev->drm.dev, sizeof(*vdev->mmu), GFP_KERNEL);
+    if (!vdev->mmu)
+        return -ENOMEM;
+
       vdev->hw->ops = &ivpu_hw_mtl_ops;
       vdev->platform = IVPU_PLATFORM_INVALID;
   @@ -303,8 +340,24 @@ static int ivpu_dev_init(struct ivpu_device *vdev)

          goto err_irq_fini;
      }
  +    ret = ivpu_mmu_global_context_init(vdev);
+    if (ret) {
+        ivpu_err(vdev, "Failed to initialize global MMU context: %d\n", ret); +        goto err_power_down;
+    }

drivers/gpu/drm/ivpu/ivpu_drv.c +7 -0
+
+    ret = ivpu_mmu_init(vdev);
+    if (ret) {
+        ivpu_err(vdev, "Failed to initialize MMU device: %d\n", ret);
+        goto err_mmu_gctx_fini;
+    }
+

      return 0;
  +err_mmu_gctx_fini:
+    ivpu_mmu_global_context_fini(vdev);
+err_power_down:
+    ivpu_hw_power_down(vdev);
  err_irq_fini:
      ivpu_irq_fini(vdev);
  err_pci_fini:

drivers/gpu/drm/ivpu/ivpu_drv.c +0 -0
@@ -316,6 +369,8 @@ static void ivpu_dev_fini(struct ivpu_device *vdev)
Quoted 8 lines

drivers/gpu/drm/ivpu/ivpu_drv.c static void ivpu_dev_fini(struct ivpu_device *vdev) · line 375

Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_drv.c +1 -0
   {
       ivpu_shutdown(vdev);
   +    ivpu_mmu_fini(vdev);
+    ivpu_mmu_global_context_fini(vdev);
       ivpu_irq_fini(vdev);
       ivpu_pci_fini(vdev);

I already mentioned that these _fini and cleanup routines are not much liked in DRM land. I suggest to go throught the code and turn everything into managed cleanup; here and everywhere else in the driver. The overhead is small and your driver probably doesn't run in a environment where it makes a difference.

OK, I'll try to convert as much as possible.

Quoted 763 lines
Quoted 763 lines

  diff --git a/drivers/gpu/drm/ivpu/ivpu_drv.h b/drivers/gpu/drm/ivpu/ivpu_drv.h index 43dfa78544c6..6eec3eb76c2f 100644

drivers/gpu/drm/ivpu/ivpu_drv.h +0 -0
@@ -14,6 +14,8 @@
   #include <linux/xarray.h>
   #include <uapi/drm/ivpu_drm.h>

  +#include "ivpu_mmu_context.h"
+
  #define DRIVER_NAME "intel_vpu"
  #define DRIVER_DESC "Driver for Intel Versatile Processing Unit (VPU)" #define DRIVER_DATE "20220913"

drivers/gpu/drm/ivpu/ivpu_drv.h +1 -0
@@ -70,6 +72,7 @@ struct ivpu_wa_table {
   };
     struct ivpu_hw_info;
+struct ivpu_mmu_info;
     struct ivpu_device {

      struct drm_device drm; /* Must be first */

drivers/gpu/drm/ivpu/ivpu_drv.h +1 -0
@@ -80,7 +83,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;
   @@ -95,6 +100,8 @@ struct ivpu_device {
   struct ivpu_file_priv {

      struct kref ref;
      struct ivpu_device *vdev;
+    struct mutex lock;
+    struct ivpu_mmu_context ctx;
      u32 priority;
  };
  diff --git a/drivers/gpu/drm/ivpu/ivpu_hw_mtl.c b/drivers/gpu/drm/ivpu/ivpu_hw_mtl.c index 17fc8e1104df..525b57c4029c 100644

drivers/gpu/drm/ivpu/ivpu_hw_mtl.c +9 -0
@@ -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 irqreturn_t 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))
+        ret &= ivpu_mmu_irq_evtq_handler(vdev);
+
+    if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_1_INT, status))
+        ivpu_dbg(IRQ, "MMU sync complete\n");
+
+    if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_2_INT, status))
+        ret &= ivpu_mmu_irq_gerr_handler(vdev);
+
       if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, CPU_INT_REDIRECT_0_INT, status))
           ret &= ivpu_hw_mtl_irq_wdt_mss_handler(vdev);

  diff --git a/drivers/gpu/drm/ivpu/ivpu_mmu.c b/drivers/gpu/drm/ivpu/ivpu_mmu.c new file mode 100644
index 000000000000..b9f225a83c45

drivers/gpu/drm/ivpu/ivpu_mmu.c new file +700 -0
@@ -0,0 +1,883 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#include <linux/highmem.h>
+#include <linux/circ_buf.h>
+
+#include "ivpu_drv.h"
+#include "ivpu_hw_mtl_reg.h"
+#include "ivpu_hw_reg_io.h"
+#include "ivpu_mmu.h"
+#include "ivpu_mmu_context.h"
+
+#define IVPU_MMU_IDR0_REF        0x080f3e0f
+#define IVPU_MMU_IDR0_REF_SIMICS    0x080f3e1f
+#define IVPU_MMU_IDR1_REF        0x0e739d18
+#define IVPU_MMU_IDR3_REF        0x0000003c
+#define IVPU_MMU_IDR5_REF        0x00040070
+#define IVPU_MMU_IDR5_REF_SIMICS    0x00000075
+#define IVPU_MMU_IDR5_REF_FPGA        0x00800075
+
+#define IVPU_MMU_CDTAB_ENT_SIZE        64
+#define IVPU_MMU_CDTAB_ENT_COUNT_LOG2    8 /* 256 entries */
+#define IVPU_MMU_CDTAB_ENT_COUNT    ((u32)1 << IVPU_MMU_CDTAB_ENT_COUNT_LOG2)
+
+#define IVPU_MMU_STREAM_ID0        0
+#define IVPU_MMU_STREAM_ID3        3
+
+#define IVPU_MMU_STRTAB_ENT_SIZE    64
+#define IVPU_MMU_STRTAB_ENT_COUNT    4
+#define IVPU_MMU_STRTAB_CFG_LOG2SIZE    2
+#define IVPU_MMU_STRTAB_CFG        IVPU_MMU_STRTAB_CFG_LOG2SIZE
+
+#define IVPU_MMU_Q_COUNT_LOG2        4 /* 16 entries */
+#define IVPU_MMU_Q_COUNT        ((u32)1 << IVPU_MMU_Q_COUNT_LOG2)
+#define IVPU_MMU_Q_WRAP_BIT        (IVPU_MMU_Q_COUNT << 1)
+#define IVPU_MMU_Q_WRAP_MASK        (IVPU_MMU_Q_WRAP_BIT - 1)
+#define IVPU_MMU_Q_IDX_MASK        (IVPU_MMU_Q_COUNT - 1)
+#define IVPU_MMU_Q_IDX(val)        ((val) & IVPU_MMU_Q_IDX_MASK)
+
+#define IVPU_MMU_CMDQ_CMD_SIZE        16
+#define IVPU_MMU_CMDQ_SIZE        (IVPU_MMU_Q_COUNT * IVPU_MMU_CMDQ_CMD_SIZE)
+
+#define IVPU_MMU_EVTQ_CMD_SIZE        32
+#define IVPU_MMU_EVTQ_SIZE        (IVPU_MMU_Q_COUNT * IVPU_MMU_EVTQ_CMD_SIZE)
+
+#define IVPU_MMU_CMD_OPCODE        GENMASK(8, 0)
+
+#define IVPU_MMU_CMD_SYNC_0_CS        GENMASK(13, 12)
+#define IVPU_MMU_CMD_SYNC_0_MSH        GENMASK(23, 22)
+#define IVPU_MMU_CMD_SYNC_0_MSI_ATTR    GENMASK(27, 24)
+#define IVPU_MMU_CMD_SYNC_0_MSI_ATTR    GENMASK(27, 24)
+#define IVPU_MMU_CMD_SYNC_0_MSI_DATA    GENMASK(63, 32)
+
+#define IVPU_MMU_CMD_CFGI_0_SSEC    BIT(10)
+#define IVPU_MMU_CMD_CFGI_0_SSV        BIT(11)
+#define IVPU_MMU_CMD_CFGI_0_SSID    GENMASK(31, 12)
+#define IVPU_MMU_CMD_CFGI_0_SID        GENMASK(63, 32)
+#define IVPU_MMU_CMD_CFGI_1_RANGE    GENMASK(4, 0)
+
+#define IVPU_MMU_CMD_TLBI_0_ASID    GENMASK(63, 48)
+#define IVPU_MMU_CMD_TLBI_0_VMID    GENMASK(47, 32)
+
+#define CMD_PREFETCH_CFG        0x1
+#define CMD_CFGI_STE            0x3
+#define CMD_CFGI_ALL            0x4
+#define CMD_CFGI_CD            0x5
+#define CMD_CFGI_CD_ALL            0x6
+#define CMD_TLBI_NH_ASID        0x11
+#define CMD_TLBI_EL2_ALL        0x20
+#define CMD_TLBI_NSNH_ALL        0x30
+#define CMD_SYNC            0x46
+
+#define IVPU_MMU_EVT_F_UUT        0x01
+#define IVPU_MMU_EVT_C_BAD_STREAMID    0x02
+#define IVPU_MMU_EVT_F_STE_FETCH    0x03
+#define IVPU_MMU_EVT_C_BAD_STE        0x04
+#define IVPU_MMU_EVT_F_BAD_ATS_TREQ    0x05
+#define IVPU_MMU_EVT_F_STREAM_DISABLED    0x06
+#define IVPU_MMU_EVT_F_TRANSL_FORBIDDEN    0x07
+#define IVPU_MMU_EVT_C_BAD_SUBSTREAMID    0x08
+#define IVPU_MMU_EVT_F_CD_FETCH        0x09
+#define IVPU_MMU_EVT_C_BAD_CD        0x0a
+#define IVPU_MMU_EVT_F_WALK_EABT    0x0b
+#define IVPU_MMU_EVT_F_TRANSLATION    0x10
+#define IVPU_MMU_EVT_F_ADDR_SIZE    0x11
+#define IVPU_MMU_EVT_F_ACCESS        0x12
+#define IVPU_MMU_EVT_F_PERMISSION    0x13
+#define IVPU_MMU_EVT_F_TLB_CONFLICT    0x20
+#define IVPU_MMU_EVT_F_CFG_CONFLICT    0x21
+#define IVPU_MMU_EVT_E_PAGE_REQUEST    0x24
+#define IVPU_MMU_EVT_F_VMS_FETCH    0x25
+
+#define IVPU_MMU_EVTS_MAX        8
+
+#define IVPU_MMU_EVT_OP_MASK        GENMASK_ULL(7, 0)
+#define IVPU_MMU_EVT_SSID_MASK        GENMASK_ULL(31, 12)
+
+#define IVPU_MMU_Q_BASE_RWA        BIT(62)
+#define IVPU_MMU_Q_BASE_ADDR_MASK    GENMASK_ULL(51, 5)
+#define IVPU_MMU_STRTAB_BASE_RA        BIT(62)
+#define IVPU_MMU_STRTAB_BASE_ADDR_MASK    GENMASK_ULL(51, 6)
+
+#define IVPU_MMU_IRQ_EVTQ_EN        BIT(2)
+#define IVPU_MMU_IRQ_GERROR_EN        BIT(0)
+
+#define IVPU_MMU_CR0_ATSCHK        BIT(4)
+#define IVPU_MMU_CR0_CMDQEN        BIT(3)
+#define IVPU_MMU_CR0_EVTQEN        BIT(2)
+#define IVPU_MMU_CR0_PRIQEN        BIT(1)
+#define IVPU_MMU_CR0_SMMUEN        BIT(0)
+
+#define IVPU_MMU_CR1_TABLE_SH        GENMASK(11, 10)
+#define IVPU_MMU_CR1_TABLE_OC        GENMASK(9, 8)
+#define IVPU_MMU_CR1_TABLE_IC        GENMASK(7, 6)
+#define IVPU_MMU_CR1_QUEUE_SH        GENMASK(5, 4)
+#define IVPU_MMU_CR1_QUEUE_OC        GENMASK(3, 2)
+#define IVPU_MMU_CR1_QUEUE_IC        GENMASK(1, 0)
+#define IVPU_MMU_CACHE_NC        0
+#define IVPU_MMU_CACHE_WB        1
+#define IVPU_MMU_CACHE_WT        2
+#define IVPU_MMU_SH_NSH            0
+#define IVPU_MMU_SH_OSH            2
+#define IVPU_MMU_SH_ISH            3
+
+#define IVPU_MMU_CMDQ_OP        GENMASK_ULL(7, 0)
+
+#define IVPU_MMU_CD_0_TCR_T0SZ        GENMASK_ULL(5, 0)
+#define IVPU_MMU_CD_0_TCR_TG0        GENMASK_ULL(7, 6)
+#define IVPU_MMU_CD_0_TCR_IRGN0        GENMASK_ULL(9, 8)
+#define IVPU_MMU_CD_0_TCR_ORGN0        GENMASK_ULL(11, 10)
+#define IVPU_MMU_CD_0_TCR_SH0        GENMASK_ULL(13, 12)
+#define IVPU_MMU_CD_0_TCR_EPD0        BIT_ULL(14)
+#define IVPU_MMU_CD_0_TCR_EPD1        BIT_ULL(30)
+#define IVPU_MMU_CD_0_ENDI        BIT(15)
+#define IVPU_MMU_CD_0_V            BIT(31)
+#define IVPU_MMU_CD_0_TCR_IPS        GENMASK_ULL(34, 32)
+#define IVPU_MMU_CD_0_TCR_TBI0        BIT_ULL(38)
+#define IVPU_MMU_CD_0_AA64        BIT(41)
+#define IVPU_MMU_CD_0_S            BIT(44)
+#define IVPU_MMU_CD_0_R            BIT(45)
+#define IVPU_MMU_CD_0_A            BIT(46)
+#define IVPU_MMU_CD_0_ASET        BIT(47)
+#define IVPU_MMU_CD_0_ASID        GENMASK_ULL(63, 48)
+
+#define IVPU_MMU_CD_1_TTB0_MASK        GENMASK_ULL(51, 4)
+
+#define IVPU_MMU_STE_0_S1CDMAX        GENMASK_ULL(63, 59)
+#define IVPU_MMU_STE_0_S1FMT        GENMASK_ULL(5, 4)
+#define IVPU_MMU_STE_0_S1FMT_LINEAR    0
+#define IVPU_MMU_STE_DWORDS        8
+#define IVPU_MMU_STE_0_CFG_S1_TRANS    5
+#define IVPU_MMU_STE_0_CFG        GENMASK_ULL(3, 1)
+#define IVPU_MMU_STE_0_S1CTXPTR_MASK    GENMASK_ULL(51, 6)
+#define IVPU_MMU_STE_0_V            BIT(0)
+
+#define IVPU_MMU_STE_1_STRW_NSEL1    0ul
+#define IVPU_MMU_STE_1_CONT        GENMASK_ULL(16, 13)
+#define IVPU_MMU_STE_1_STRW        GENMASK_ULL(31, 30)
+#define IVPU_MMU_STE_1_PRIVCFG        GENMASK_ULL(49, 48)
+#define IVPU_MMU_STE_1_PRIVCFG_UNPRIV    2ul
+#define IVPU_MMU_STE_1_INSTCFG        GENMASK_ULL(51, 50)
+#define IVPU_MMU_STE_1_INSTCFG_DATA    2ul
+#define IVPU_MMU_STE_1_MEV        BIT(19)
+#define IVPU_MMU_STE_1_S1STALLD        BIT(27)
+#define IVPU_MMU_STE_1_S1C_CACHE_NC    0ul
+#define IVPU_MMU_STE_1_S1C_CACHE_WBRA    1ul
+#define IVPU_MMU_STE_1_S1C_CACHE_WT    2ul
+#define IVPU_MMU_STE_1_S1C_CACHE_WB    3ul
+#define IVPU_MMU_STE_1_S1CIR        GENMASK_ULL(3, 2)
+#define IVPU_MMU_STE_1_S1COR        GENMASK_ULL(5, 4)
+#define IVPU_MMU_STE_1_S1CSH        GENMASK_ULL(7, 6)
+#define IVPU_MMU_STE_1_S1DSS        GENMASK_ULL(1, 0)
+#define IVPU_MMU_STE_1_S1DSS_TERMINATE    0x0
+
+#define IVPU_MMU_REG_TIMEOUT_US        (10 * USEC_PER_MSEC)
+#define IVPU_MMU_QUEUE_TIMEOUT_US    (100 * USEC_PER_MSEC)
+
+#define IVPU_MMU_GERROR_ERR_MASK ((REG_FLD(MTL_VPU_HOST_MMU_GERROR, CMDQ)) | \
+                  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, EVTQ_ABT)) | \
+                  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, PRIQ_ABT)) | \
+                  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_CMDQ_ABT)) | \
+                  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_EVTQ_ABT)) | \
+                  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_PRIQ_ABT)) | \
+                  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_ABT)) | \
+                  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, SFM)))
+
+static char *ivpu_mmu_evt_to_str(u32 cmd)
+{
+    switch (cmd) {
+    case IVPU_MMU_EVT_F_UUT:
+        return "Unsupported Upstream Transaction";
+    case IVPU_MMU_EVT_C_BAD_STREAMID:
+        return "Transaction StreamID out of range";
+    case IVPU_MMU_EVT_F_STE_FETCH:
+        return "Fetch of STE caused external abort";
+    case IVPU_MMU_EVT_C_BAD_STE:
+        return "Used STE invalid";
+    case IVPU_MMU_EVT_F_BAD_ATS_TREQ:
+        return "Address Request disallowed for a StreamID";
+    case IVPU_MMU_EVT_F_STREAM_DISABLED:
+        return "Transaction marks non-substream disabled";
+    case IVPU_MMU_EVT_F_TRANSL_FORBIDDEN:
+        return "MMU bypass is disallowed for this StreamID";
+    case IVPU_MMU_EVT_C_BAD_SUBSTREAMID:
+        return "Invalid StreamID";
+    case IVPU_MMU_EVT_F_CD_FETCH:
+        return "Fetch of CD caused external abort";
+    case IVPU_MMU_EVT_C_BAD_CD:
+        return "Fetched CD invalid";
+    case IVPU_MMU_EVT_F_WALK_EABT:
+        return " An external abort occurred fetching a TLB";
+    case IVPU_MMU_EVT_F_TRANSLATION:
+        return "Translation fault";
+    case IVPU_MMU_EVT_F_ADDR_SIZE:
+        return " Output address caused address size fault";
+    case IVPU_MMU_EVT_F_ACCESS:
+        return "Access flag fault";
+    case IVPU_MMU_EVT_F_PERMISSION:
+        return "Permission fault occurred on page access";
+    case IVPU_MMU_EVT_F_TLB_CONFLICT:
+        return "A TLB conflict";
+    case IVPU_MMU_EVT_F_CFG_CONFLICT:
+        return "A configuration cache conflict";
+    case IVPU_MMU_EVT_E_PAGE_REQUEST:
+        return "Page request hint from a client device";
+    case IVPU_MMU_EVT_F_VMS_FETCH:
+        return "Fetch of VMS caused external abort";
+    default:
+        return "Unknown CMDQ command";
+    }
+}
+
+static int ivpu_mmu_config_check(struct ivpu_device *vdev)
+{
+    u32 val_ref;
+    u32 val;
+
+    if (ivpu_is_simics(vdev))
+        val_ref = IVPU_MMU_IDR0_REF_SIMICS;
+    else
+        val_ref = IVPU_MMU_IDR0_REF;
+
+    val = REGV_RD32(MTL_VPU_HOST_MMU_IDR0);
+    if (val != val_ref)
+        ivpu_err(vdev, "IDR0 0x%x != IDR0_REF 0x%x\n", val, val_ref);
+
+    val = REGV_RD32(MTL_VPU_HOST_MMU_IDR1);
+    if (val != IVPU_MMU_IDR1_REF)
+        ivpu_warn(vdev, "IDR1 0x%x != IDR1_REF 0x%x\n", val, IVPU_MMU_IDR1_REF);
+
+    val = REGV_RD32(MTL_VPU_HOST_MMU_IDR3);
+    if (val != IVPU_MMU_IDR3_REF)
+        ivpu_warn(vdev, "IDR3 0x%x != IDR3_REF 0x%x\n", val, IVPU_MMU_IDR3_REF);
+
+    if (ivpu_is_simics(vdev))
+        val_ref = IVPU_MMU_IDR5_REF_SIMICS;
+    else if (ivpu_is_fpga(vdev))
+        val_ref = IVPU_MMU_IDR5_REF_FPGA;
+    else
+        val_ref = IVPU_MMU_IDR5_REF;
+
+    val = REGV_RD32(MTL_VPU_HOST_MMU_IDR5);
+    if (val != val_ref)
+        ivpu_warn(vdev, "IDR5 0x%x != IDR5_REF 0x%x\n", val, val_ref);
+
+    return 0;
+}
+
+static int ivpu_mmu_cdtab_alloc(struct ivpu_device *vdev)
+{
+    struct ivpu_mmu_info *mmu = vdev->mmu;
+    struct ivpu_mmu_cdtab *cdtab = &mmu->cdtab;
+    size_t size = IVPU_MMU_CDTAB_ENT_COUNT * IVPU_MMU_CDTAB_ENT_SIZE;
+
+    cdtab->base = dmam_alloc_coherent(vdev->drm.dev, size, &cdtab->dma, GFP_KERNEL);
+    if (!cdtab->base)
+        return -ENOMEM;
+
+    ivpu_dbg(MMU, "CDTAB alloc: dma=%pad size=%zu\n", &cdtab->dma, size);
+
+    return 0;
+}
+
+static int ivpu_mmu_strtab_alloc(struct ivpu_device *vdev)
+{
+    struct ivpu_mmu_info *mmu = vdev->mmu;
+    struct ivpu_mmu_strtab *strtab = &mmu->strtab;
+    size_t size = IVPU_MMU_STRTAB_ENT_COUNT * IVPU_MMU_STRTAB_ENT_SIZE;
+
+    strtab->base = dmam_alloc_coherent(vdev->drm.dev, size, &strtab->dma, GFP_KERNEL);
+    if (!strtab->base)
+        return -ENOMEM;
+
+    strtab->base_cfg = IVPU_MMU_STRTAB_CFG;
+    strtab->dma_q = IVPU_MMU_STRTAB_BASE_RA;
+    strtab->dma_q |= strtab->dma & IVPU_MMU_STRTAB_BASE_ADDR_MASK;
+
+    ivpu_dbg(MMU, "STRTAB alloc: dma=%pad dma_q=%pad size=%zu\n",
+         &strtab->dma, &strtab->dma_q, size);
+
+    return 0;
+}
+
+static int ivpu_mmu_cmdq_alloc(struct ivpu_device *vdev)
+{
+    struct ivpu_mmu_info *mmu = vdev->mmu;
+    struct ivpu_mmu_queue *q = &mmu->cmdq;
+
+    q->base = dmam_alloc_coherent(vdev->drm.dev, IVPU_MMU_CMDQ_SIZE, &q->dma, GFP_KERNEL);
+    if (!q->base)
+        return -ENOMEM;
+
+    q->dma_q = IVPU_MMU_Q_BASE_RWA;
+    q->dma_q |= q->dma & IVPU_MMU_Q_BASE_ADDR_MASK;
+    q->dma_q |= IVPU_MMU_Q_COUNT_LOG2;
+
+    ivpu_dbg(MMU, "CMDQ alloc: dma=%pad dma_q=%pad size=%u\n",
+         &q->dma, &q->dma_q, IVPU_MMU_CMDQ_SIZE);
+
+    return 0;
+}
+
+static int ivpu_mmu_evtq_alloc(struct ivpu_device *vdev)
+{
+    struct ivpu_mmu_info *mmu = vdev->mmu;
+    struct ivpu_mmu_queue *q = &mmu->evtq;
+
+    q->base = dmam_alloc_coherent(vdev->drm.dev, IVPU_MMU_EVTQ_SIZE, &q->dma, GFP_KERNEL);
+    if (!q->base)
+        return -ENOMEM;
+
+    q->dma_q = IVPU_MMU_Q_BASE_RWA;
+    q->dma_q |= q->dma & IVPU_MMU_Q_BASE_ADDR_MASK;
+    q->dma_q |= IVPU_MMU_Q_COUNT_LOG2;
+
+    ivpu_dbg(MMU, "EVTQ alloc: dma=%pad dma_q=%pad size=%u\n",
+         &q->dma, &q->dma_q, IVPU_MMU_EVTQ_SIZE);
+
+    return 0;
+}
+
+static int ivpu_mmu_structs_alloc(struct ivpu_device *vdev)
+{
+    int ret;
+
+    ret = ivpu_mmu_cdtab_alloc(vdev);
+    if (ret) {
+        ivpu_err(vdev, "Failed to allocate cdtab: %d\n", ret);
+        return ret;
+    }
+
+    ret = ivpu_mmu_strtab_alloc(vdev);
+    if (ret) {
+        ivpu_err(vdev, "Failed to allocate strtab: %d\n", ret);
+        return ret;
+    }
+
+    ret = ivpu_mmu_cmdq_alloc(vdev);
+    if (ret) {
+        ivpu_err(vdev, "Failed to allocate cmdq: %d\n", ret);
+        return ret;
+    }
+
+    ret = ivpu_mmu_evtq_alloc(vdev);
+    if (ret)
+        ivpu_err(vdev, "Failed to allocate evtq: %d\n", ret);
+
+    return ret;
+}
+
+static int ivpu_mmu_reg_write(struct ivpu_device *vdev, u32 reg, u32 val)
+{
+    u32 reg_ack = reg + 4; /* ACK register is 4B after base register */
+    u32 val_ack;
+    int ret;
+
+    REGV_WR32(reg, val);
+
+    ret = REGV_POLL(reg_ack, val_ack, (val == val_ack), IVPU_MMU_REG_TIMEOUT_US);
+    if (ret)
+        ivpu_err(vdev, "Failed to write register 0x%x\n", reg);
+
+    return ret;
+}
+
+static int ivpu_mmu_irqs_setup(struct ivpu_device *vdev)
+{
+    u32 irq_ctrl = IVPU_MMU_IRQ_EVTQ_EN | IVPU_MMU_IRQ_GERROR_EN;
+    int ret;
+
+    ret = ivpu_mmu_reg_write(vdev, MTL_VPU_HOST_MMU_IRQ_CTRL, 0);
+    if (ret)
+        return ret;
+
+    return ivpu_mmu_reg_write(vdev, MTL_VPU_HOST_MMU_IRQ_CTRL, irq_ctrl);
+}
+
+static int ivpu_mmu_cmdq_wait_for_cons(struct ivpu_device *vdev)
+{
+    struct ivpu_mmu_queue *cmdq = &vdev->mmu->cmdq;
+
+    return REGV_POLL(MTL_VPU_HOST_MMU_CMDQ_CONS, cmdq->cons, (cmdq->prod == cmdq->cons),
+             IVPU_MMU_QUEUE_TIMEOUT_US);
+}
+
+static int ivpu_mmu_cmdq_cmd_write(struct ivpu_device *vdev, const char *name, u64 data0, u64 data1)
+{
+    struct ivpu_mmu_queue *q = &vdev->mmu->cmdq;
+    u64 *queue_buffer = q->base;
+    int idx = IVPU_MMU_Q_IDX(q->prod) * (IVPU_MMU_CMDQ_CMD_SIZE / sizeof(*queue_buffer));
+
+    if (!CIRC_SPACE(IVPU_MMU_Q_IDX(q->prod), IVPU_MMU_Q_IDX(q->cons), IVPU_MMU_Q_COUNT)) {
+        ivpu_err(vdev, "Failed to write MMU CMD %s\n", name);
+        return -EBUSY;
+    }
+
+    queue_buffer[idx] = data0;
+    queue_buffer[idx + 1] = data1;
+    q->prod = (q->prod + 1) & IVPU_MMU_Q_WRAP_MASK;
+
+    ivpu_dbg(MMU, "CMD write: %s data: 0x%llx 0x%llx\n", name, data0, data1);
+
+    return 0;
+}
+
+static int ivpu_mmu_cmdq_sync(struct ivpu_device *vdev)
+{
+    struct ivpu_mmu_queue *q = &vdev->mmu->cmdq;
+    u64 val;
+    int ret;
+
+    val = FIELD_PREP(IVPU_MMU_CMD_OPCODE, CMD_SYNC) |
+          FIELD_PREP(IVPU_MMU_CMD_SYNC_0_CS, 0x2) |
+          FIELD_PREP(IVPU_MMU_CMD_SYNC_0_MSH, 0x3) |
+          FIELD_PREP(IVPU_MMU_CMD_SYNC_0_MSI_ATTR, 0xf);
+
+    ret = ivpu_mmu_cmdq_cmd_write(vdev, "SYNC", val, 0);
+    if (ret)
+        return ret;
+
+    clflush_cache_range(q->base, IVPU_MMU_CMDQ_SIZE);
+    REGV_WR32(MTL_VPU_HOST_MMU_CMDQ_PROD, q->prod);
+
+    ret = ivpu_mmu_cmdq_wait_for_cons(vdev);
+    if (ret)
+        ivpu_err(vdev, "Timed out waiting for consumer: %d\n", ret);
+
+    return ret;
+}
+
+static int ivpu_mmu_cmdq_write_cfgi_all(struct ivpu_device *vdev)
+{
+    u64 data0 = FIELD_PREP(IVPU_MMU_CMD_OPCODE, CMD_CFGI_ALL);
+    u64 data1 = FIELD_PREP(IVPU_MMU_CMD_CFGI_1_RANGE, 0x1f);
+
+    return ivpu_mmu_cmdq_cmd_write(vdev, "CFGI_ALL", data0, data1);
+}
+
+static int ivpu_mmu_cmdq_write_tlbi_nh_asid(struct ivpu_device *vdev, u16 ssid)
+{
+    u64 val = FIELD_PREP(IVPU_MMU_CMD_OPCODE, CMD_TLBI_NH_ASID) |
+          FIELD_PREP(IVPU_MMU_CMD_TLBI_0_ASID, ssid);
+
+    return ivpu_mmu_cmdq_cmd_write(vdev, "TLBI_NH_ASID", val, 0);
+}
+
+static int ivpu_mmu_cmdq_write_tlbi_nsnh_all(struct ivpu_device *vdev)
+{
+    u64 val = FIELD_PREP(IVPU_MMU_CMD_OPCODE, CMD_TLBI_NSNH_ALL);
+
+    return ivpu_mmu_cmdq_cmd_write(vdev, "TLBI_NSNH_ALL", val, 0);
+}
+
+static int ivpu_mmu_reset(struct ivpu_device *vdev)
+{
+    struct ivpu_mmu_info *mmu = vdev->mmu;
+    u32 val;
+    int ret;
+
+    memset(mmu->cmdq.base, 0, IVPU_MMU_CMDQ_SIZE);
+    clflush_cache_range(mmu->cmdq.base, IVPU_MMU_CMDQ_SIZE);
+    mmu->cmdq.prod = 0;
+    mmu->cmdq.cons = 0;
+
+    memset(mmu->evtq.base, 0, IVPU_MMU_EVTQ_SIZE);
+    clflush_cache_range(mmu->evtq.base, IVPU_MMU_EVTQ_SIZE);
+    mmu->evtq.prod = 0;
+    mmu->evtq.cons = 0;
+
+    ret = ivpu_mmu_reg_write(vdev, MTL_VPU_HOST_MMU_CR0, 0);
+    if (ret)
+        return ret;
+
+    val = FIELD_PREP(IVPU_MMU_CR1_TABLE_SH, IVPU_MMU_SH_ISH) |
+          FIELD_PREP(IVPU_MMU_CR1_TABLE_OC, IVPU_MMU_CACHE_WB) |
+          FIELD_PREP(IVPU_MMU_CR1_TABLE_IC, IVPU_MMU_CACHE_WB) |
+          FIELD_PREP(IVPU_MMU_CR1_QUEUE_SH, IVPU_MMU_SH_ISH) |
+          FIELD_PREP(IVPU_MMU_CR1_QUEUE_OC, IVPU_MMU_CACHE_WB) |
+          FIELD_PREP(IVPU_MMU_CR1_QUEUE_IC, IVPU_MMU_CACHE_WB);
+    REGV_WR32(MTL_VPU_HOST_MMU_CR1, val);
+
+    REGV_WR64(MTL_VPU_HOST_MMU_STRTAB_BASE, mmu->strtab.dma_q);
+    REGV_WR32(MTL_VPU_HOST_MMU_STRTAB_BASE_CFG, mmu->strtab.base_cfg);
+
+    REGV_WR64(MTL_VPU_HOST_MMU_CMDQ_BASE, mmu->cmdq.dma_q);
+    REGV_WR32(MTL_VPU_HOST_MMU_CMDQ_PROD, 0);
+    REGV_WR32(MTL_VPU_HOST_MMU_CMDQ_CONS, 0);
+
+    val = IVPU_MMU_CR0_CMDQEN;
+    ret = ivpu_mmu_reg_write(vdev, MTL_VPU_HOST_MMU_CR0, val);
+    if (ret)
+        return ret;
+
+    ret = ivpu_mmu_cmdq_write_cfgi_all(vdev);
+    if (ret)
+        return ret;
+
+    ret = ivpu_mmu_cmdq_write_tlbi_nsnh_all(vdev);
+    if (ret)
+        return ret;
+
+    ret = ivpu_mmu_cmdq_sync(vdev);
+    if (ret)
+        return ret;
+
+    REGV_WR64(MTL_VPU_HOST_MMU_EVTQ_BASE, mmu->evtq.dma_q);
+    REGV_WR32(MTL_VPU_HOST_MMU_EVTQ_PROD_SEC, 0);
+    REGV_WR32(MTL_VPU_HOST_MMU_EVTQ_CONS_SEC, 0);
+
+    val |= IVPU_MMU_CR0_EVTQEN;
+    ret = ivpu_mmu_reg_write(vdev, MTL_VPU_HOST_MMU_CR0, val);
+    if (ret)
+        return ret;
+
+    val |= IVPU_MMU_CR0_ATSCHK;
+    ret = ivpu_mmu_reg_write(vdev, MTL_VPU_HOST_MMU_CR0, val);
+    if (ret)
+        return ret;
+
+    ret = ivpu_mmu_irqs_setup(vdev);
+    if (ret)
+        return ret;
+
+    val |= IVPU_MMU_CR0_SMMUEN;
+    return ivpu_mmu_reg_write(vdev, MTL_VPU_HOST_MMU_CR0, val);
+}
+
+static void ivpu_mmu_strtab_link_cd(struct ivpu_device *vdev, u32 sid)
+{
+    struct ivpu_mmu_info *mmu = vdev->mmu;
+    struct ivpu_mmu_strtab *strtab = &mmu->strtab;
+    struct ivpu_mmu_cdtab *cdtab = &mmu->cdtab;
+    u64 *entry = strtab->base + (sid * IVPU_MMU_STRTAB_ENT_SIZE);
+    u64 str[2];
+
+    str[0] = FIELD_PREP(IVPU_MMU_STE_0_CFG, IVPU_MMU_STE_0_CFG_S1_TRANS) |
+         FIELD_PREP(IVPU_MMU_STE_0_S1CDMAX, IVPU_MMU_CDTAB_ENT_COUNT_LOG2) |
+         FIELD_PREP(IVPU_MMU_STE_0_S1FMT, IVPU_MMU_STE_0_S1FMT_LINEAR) |
+         IVPU_MMU_STE_0_V |
+         (cdtab->dma & IVPU_MMU_STE_0_S1CTXPTR_MASK);
+
+    str[1] = FIELD_PREP(IVPU_MMU_STE_1_S1DSS, IVPU_MMU_STE_1_S1DSS_TERMINATE) |
+         FIELD_PREP(IVPU_MMU_STE_1_S1CIR, IVPU_MMU_STE_1_S1C_CACHE_NC) |
+         FIELD_PREP(IVPU_MMU_STE_1_S1COR, IVPU_MMU_STE_1_S1C_CACHE_NC) |
+         FIELD_PREP(IVPU_MMU_STE_1_S1CSH, IVPU_MMU_SH_NSH) |
+         FIELD_PREP(IVPU_MMU_STE_1_PRIVCFG, IVPU_MMU_STE_1_PRIVCFG_UNPRIV) |
+         FIELD_PREP(IVPU_MMU_STE_1_INSTCFG, IVPU_MMU_STE_1_INSTCFG_DATA) |
+         FIELD_PREP(IVPU_MMU_STE_1_STRW, IVPU_MMU_STE_1_STRW_NSEL1) |
+         FIELD_PREP(IVPU_MMU_STE_1_CONT, IVPU_MMU_STRTAB_CFG_LOG2SIZE) |
+         IVPU_MMU_STE_1_MEV |
+         IVPU_MMU_STE_1_S1STALLD;
+
+    WRITE_ONCE(entry[1], str[1]);
+    WRITE_ONCE(entry[0], str[0]);
+
+    clflush_cache_range(entry, IVPU_MMU_STRTAB_ENT_SIZE);
+
+    ivpu_dbg(MMU, "STRTAB write entry (SSID=%u): 0x%llx, 0x%llx\n",
+         sid, str[0], str[1]);
+}
+
+static int ivpu_mmu_strtab_init(struct ivpu_device *vdev)
+{
+    ivpu_mmu_strtab_link_cd(vdev, IVPU_MMU_STREAM_ID0);
+    ivpu_mmu_strtab_link_cd(vdev, IVPU_MMU_STREAM_ID3);
+
+    return 0;
+}
+
+int ivpu_mmu_invalidate_tlb(struct ivpu_device *vdev, u16 ssid)
+{
+    struct ivpu_mmu_info *mmu = vdev->mmu;
+    int ret;
+
+    if (mutex_lock_interruptible(&mmu->lock))
+        return -EINTR;
+
+    if (!mmu->on) {
+        ret = 0;
+        goto unlock;
+    }
+
+    ret = ivpu_mmu_cmdq_write_tlbi_nh_asid(vdev, ssid);
+    if (ret)
+        goto unlock;
+
+    ret = ivpu_mmu_cmdq_sync(vdev);
+unlock:
+    mutex_unlock(&mmu->lock);
+    return ret;
+}
+
+static int ivpu_mmu_cd_add(struct ivpu_device *vdev, u32 ssid, u64 cd_dma)
+{
+    struct ivpu_mmu_info *mmu = vdev->mmu;
+    struct ivpu_mmu_cdtab *cdtab = &mmu->cdtab;
+    u64 *entry;
+    u64 cd[4];
+    int ret;
+
+    if (ssid > IVPU_MMU_CDTAB_ENT_COUNT)
+        return -EINVAL;
+
+    if (mutex_lock_interruptible(&mmu->lock))
+        return -EINTR;
+
+    entry = cdtab->base + (ssid * IVPU_MMU_CDTAB_ENT_SIZE);
+
+    if (cd_dma != 0) {
+        cd[0] = FIELD_PREP(IVPU_MMU_CD_0_TCR_T0SZ, 26) |
+            FIELD_PREP(IVPU_MMU_CD_0_TCR_TG0, 0) |
+            FIELD_PREP(IVPU_MMU_CD_0_TCR_IRGN0, 0) |
+            FIELD_PREP(IVPU_MMU_CD_0_TCR_ORGN0, 0) |
+            FIELD_PREP(IVPU_MMU_CD_0_TCR_SH0, 0) |
+            FIELD_PREP(IVPU_MMU_CD_0_TCR_IPS, 3) |
+            FIELD_PREP(IVPU_MMU_CD_0_ASID, ssid) |
+            IVPU_MMU_CD_0_TCR_EPD1 |
+            IVPU_MMU_CD_0_AA64 |
+            IVPU_MMU_CD_0_R |
+            IVPU_MMU_CD_0_A |
+            IVPU_MMU_CD_0_ASET |
+            IVPU_MMU_CD_0_V;
+        cd[1] = cd_dma & IVPU_MMU_CD_1_TTB0_MASK;
+        cd[2] = 0;
+        cd[3] = 0x0000000000007444;
+    } else {
+        memset(cd, 0, sizeof(cd));
+    }
+
+    WRITE_ONCE(entry[1], cd[1]);
+    WRITE_ONCE(entry[2], cd[2]);
+    WRITE_ONCE(entry[3], cd[3]);
+    WRITE_ONCE(entry[0], cd[0]);
+
+    clflush_cache_range(entry, IVPU_MMU_CDTAB_ENT_SIZE);
+
+    ivpu_dbg(MMU, "CDTAB %s entry (SSID=%u, dma=%pad): 0x%llx, 0x%llx, 0x%llx, 0x%llx\n",
+         cd_dma ? "write" : "clear", ssid, &cd_dma, cd[0], cd[1], cd[2], cd[3]);
+
+    if (!mmu->on) {
+        ret = 0;
+        goto unlock;
+    }
+
+    ret = ivpu_mmu_cmdq_write_cfgi_all(vdev);
+    if (ret)
+        goto unlock;
+
+    ret = ivpu_mmu_cmdq_sync(vdev);
+unlock:
+    mutex_unlock(&mmu->lock);
+    return ret;
+}
+
+static int ivpu_mmu_cd_add_gbl(struct ivpu_device *vdev)
+{
+    int ret;
+
+    ret = ivpu_mmu_cd_add(vdev, 0, vdev->gctx.pgtable.pgd_dma);
+    if (ret)
+        ivpu_err(vdev, "Failed to add global CD entry: %d\n", ret);
+
+    return ret;
+}
+
+static int ivpu_mmu_cd_add_user(struct ivpu_device *vdev, u32 ssid, dma_addr_t cd_dma)
+{
+    int ret;
+
+    if (ssid == 0) {
+        ivpu_err(vdev, "Invalid SSID: %u\n", ssid);
+        return -EINVAL;
+    }
+
+    ret = ivpu_mmu_cd_add(vdev, ssid, cd_dma);
+    if (ret)
+        ivpu_err(vdev, "Failed to add CD entry SSID=%u: %d\n", ssid, ret);
+
Quoted 8 lines

drivers/gpu/drm/ivpu/ivpu_mmu.c line 706

Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_mmu.c new file +6 -0
+    return ret;
+}
+
+void ivpu_mmu_fini(struct ivpu_device *vdev)
+{
+    mutex_destroy(&vdev->mmu->lock);

We have drmm_mutex_init()

OK, I'll try to use it.

Regards,
Jacek

#18 lore raw

Re: [PATCH v3 2/7] drm/ivpu: Add Intel VPU MMU support

In reply to Jacek Lawrynowicz

Hi

Am 24.09.22 um 17:11 schrieb Jacek Lawrynowicz:

Quoted 237 lines

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

Address space is implemented as a struct 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 to 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/gpu/drm/ivpu/Makefile           |   4 +-
  drivers/gpu/drm/ivpu/ivpu_drv.c         |  59 +-
  drivers/gpu/drm/ivpu/ivpu_drv.h         |   7 +
  drivers/gpu/drm/ivpu/ivpu_hw_mtl.c      |  10 +
  drivers/gpu/drm/ivpu/ivpu_mmu.c         | 883 ++++++++++++++++++++++++
  drivers/gpu/drm/ivpu/ivpu_mmu.h         |  53 ++
  drivers/gpu/drm/ivpu/ivpu_mmu_context.c | 419 +++++++++++
  drivers/gpu/drm/ivpu/ivpu_mmu_context.h |  49 ++
  include/uapi/drm/ivpu_drm.h             |   4 +
  9 files changed, 1485 insertions(+), 3 deletions(-)

create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu_context.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu_context.h

drivers/gpu/drm/ivpu/Makefile +3 -1
index e59dc65abe6a..95bb04f26296 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_IVPU) += intel_vpu.o
drivers/gpu/drm/ivpu/ivpu_drv.c +57 -2
index a01c7244f6e5..cbeb9a801a31 100644
@@ -14,6 +14,8 @@
 
  #include "ivpu_drv.h"
  #include "ivpu_hw.h"
+#include "ivpu_mmu.h"
+#include "ivpu_mmu_context.h"
 
  #ifndef DRIVER_VERSION_STR
  #define DRIVER_VERSION_STR __stringify(DRM_IVPU_DRIVER_MAJOR) "." \
@@ -50,6 +52,11 @@ char *ivpu_platform_to_str(u32 platform)
 
  void ivpu_file_priv_get(struct ivpu_file_priv *file_priv, struct ivpu_file_priv **link)
  {
+	struct ivpu_device *vdev = file_priv->vdev;
+
+	ivpu_dbg(KREF, "file_priv get: ctx %u refcount %u\n",
+		 file_priv->ctx.id, kref_read(&file_priv->ref));
+
  	kref_get(&file_priv->ref);
  	*link = file_priv;
  }
@@ -57,6 +64,12 @@ void ivpu_file_priv_get(struct ivpu_file_priv *file_priv, struct ivpu_file_priv
  static void file_priv_release(struct kref *ref)
  {
  	struct ivpu_file_priv *file_priv = container_of(ref, struct ivpu_file_priv, ref);
+	struct ivpu_device *vdev = file_priv->vdev;
+
+	ivpu_dbg(FILE, "file_priv release: ctx %u\n", file_priv->ctx.id);
+
+	if (file_priv->ctx.id)
+		ivpu_mmu_user_context_fini(file_priv);
 
  	kfree(file_priv);
  }
@@ -64,6 +77,10 @@ static void file_priv_release(struct kref *ref)
  void ivpu_file_priv_put(struct ivpu_file_priv **link)
  {
  	struct ivpu_file_priv *file_priv = *link;
+	struct ivpu_device *vdev = file_priv->vdev;
+
+	ivpu_dbg(KREF, "file_priv put: ctx %u refcount %u\n",
+		 file_priv->ctx.id, kref_read(&file_priv->ref));
 
  	*link = NULL;
  	kref_put(&file_priv->ref, file_priv_release);
@@ -75,7 +92,11 @@ static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_f
  	struct ivpu_device *vdev = file_priv->vdev;
  	struct pci_dev *pdev = to_pci_dev(vdev->drm.dev);
  	struct drm_ivpu_param *args = data;
-	int ret = 0;
+	int ret;
+
+	ret = ivpu_mmu_user_context_init(file_priv);
+	if (ret)
+		return ret;
 
  	switch (args->param) {
  	case DRM_IVPU_PARAM_DEVICE_ID:
@@ -99,6 +120,9 @@ static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_f
  	case DRM_IVPU_PARAM_CONTEXT_PRIORITY:
  		args->value = file_priv->priority;
  		break;
+	case DRM_IVPU_PARAM_CONTEXT_ID:
+		args->value = file_priv->ctx.id;
+		break;
  	default:
  		ret = -EINVAL;
  	}
@@ -110,7 +134,11 @@ static int ivpu_set_param_ioctl(struct drm_device *dev, void *data, struct drm_f
  {
  	struct ivpu_file_priv *file_priv = file->driver_priv;
  	struct drm_ivpu_param *args = data;
-	int ret = 0;
+	int ret;
+
+	ret = ivpu_mmu_user_context_init(file_priv);
+	if (ret)
+		return ret;
 
  	switch (args->param) {
  	case DRM_IVPU_PARAM_CONTEXT_PRIORITY:
@@ -139,9 +167,13 @@ static int ivpu_open(struct drm_device *dev, struct drm_file *file)
  	file_priv->priority = DRM_IVPU_CONTEXT_PRIORITY_NORMAL;
 
  	kref_init(&file_priv->ref);
+	mutex_init(&file_priv->lock);
 
  	file->driver_priv = file_priv;
 
+	ivpu_dbg(FILE, "file_priv alloc: process %s pid %d\n",
+		 current->comm, task_pid_nr(current));
+
  	return 0;
  }
 
@@ -164,6 +196,7 @@ int ivpu_shutdown(struct ivpu_device *vdev)
  	int ret;
 
  	ivpu_hw_irq_disable(vdev);
+	ivpu_mmu_disable(vdev);
 
  	ret = ivpu_hw_power_down(vdev);
  	if (ret)
@@ -272,6 +305,10 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
  	if (!vdev->hw)
  		return -ENOMEM;
 
+	vdev->mmu = devm_kzalloc(vdev->drm.dev, sizeof(*vdev->mmu), GFP_KERNEL);
+	if (!vdev->mmu)
+		return -ENOMEM;
+
  	vdev->hw->ops = &ivpu_hw_mtl_ops;
  	vdev->platform = IVPU_PLATFORM_INVALID;
 
@@ -303,8 +340,24 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
  		goto err_irq_fini;
  	}
 
+	ret = ivpu_mmu_global_context_init(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to initialize global MMU context: %d\n", ret);
+		goto err_power_down;
+	}
+
+	ret = ivpu_mmu_init(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to initialize MMU device: %d\n", ret);
+		goto err_mmu_gctx_fini;
+	}
+
  	return 0;
 
+err_mmu_gctx_fini:
+	ivpu_mmu_global_context_fini(vdev);
+err_power_down:
+	ivpu_hw_power_down(vdev);
  err_irq_fini:
  	ivpu_irq_fini(vdev);
  err_pci_fini:
@@ -316,6 +369,8 @@ static void ivpu_dev_fini(struct ivpu_device *vdev)
  {
  	ivpu_shutdown(vdev);
 
+	ivpu_mmu_fini(vdev);
+	ivpu_mmu_global_context_fini(vdev);
  	ivpu_irq_fini(vdev);
  	ivpu_pci_fini(vdev);
 
drivers/gpu/drm/ivpu/ivpu_drv.h +5 -0
index 43dfa78544c6..6eec3eb76c2f 100644
@@ -14,6 +14,8 @@
  #include <linux/xarray.h>
  #include <uapi/drm/ivpu_drm.h>
 
+#include "ivpu_mmu_context.h"
+
  #define DRIVER_NAME "intel_vpu"
  #define DRIVER_DESC "Driver for Intel Versatile Processing Unit (VPU)"
  #define DRIVER_DATE "20220913"
@@ -70,6 +72,7 @@ struct ivpu_wa_table {
  };
 
  struct ivpu_hw_info;
+struct ivpu_mmu_info;
 
  struct ivpu_device {
  	struct drm_device drm; /* Must be first */
@@ -80,7 +83,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;
 

drivers/gpu/drm/ivpu/ivpu_drv.h struct ivpu_device { · line 103

Quoted 4 lines
drivers/gpu/drm/ivpu/ivpu_drv.h +1 -0
@@ -95,6 +100,8 @@ struct ivpu_device {
  struct ivpu_file_priv {
  	struct kref ref;
  	struct ivpu_device *vdev;
+	struct mutex lock;

There was another lock in the mmu struct, but what's this lock for?

Bst regards
Thomas

Quoted 1,482 lines
drivers/gpu/drm/ivpu/ivpu_drv.h +1 -0
+	struct ivpu_mmu_context ctx;
  	u32 priority;
  };
 
drivers/gpu/drm/ivpu/ivpu_hw_mtl.c +10 -0
index 17fc8e1104df..525b57c4029c 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 irqreturn_t 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))
+		ret &= ivpu_mmu_irq_evtq_handler(vdev);
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_1_INT, status))
+		ivpu_dbg(IRQ, "MMU sync complete\n");
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_2_INT, status))
+		ret &= ivpu_mmu_irq_gerr_handler(vdev);
+
  	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, CPU_INT_REDIRECT_0_INT, status))
  		ret &= ivpu_hw_mtl_irq_wdt_mss_handler(vdev);
 
drivers/gpu/drm/ivpu/ivpu_mmu.c new file +883 -0
new file mode 100644
index 000000000000..b9f225a83c45
@@ -0,0 +1,883 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#include <linux/highmem.h>
+#include <linux/circ_buf.h>
+
+#include "ivpu_drv.h"
+#include "ivpu_hw_mtl_reg.h"
+#include "ivpu_hw_reg_io.h"
+#include "ivpu_mmu.h"
+#include "ivpu_mmu_context.h"
+
+#define IVPU_MMU_IDR0_REF		0x080f3e0f
+#define IVPU_MMU_IDR0_REF_SIMICS	0x080f3e1f
+#define IVPU_MMU_IDR1_REF		0x0e739d18
+#define IVPU_MMU_IDR3_REF		0x0000003c
+#define IVPU_MMU_IDR5_REF		0x00040070
+#define IVPU_MMU_IDR5_REF_SIMICS	0x00000075
+#define IVPU_MMU_IDR5_REF_FPGA		0x00800075
+
+#define IVPU_MMU_CDTAB_ENT_SIZE		64
+#define IVPU_MMU_CDTAB_ENT_COUNT_LOG2	8 /* 256 entries */
+#define IVPU_MMU_CDTAB_ENT_COUNT	((u32)1 << IVPU_MMU_CDTAB_ENT_COUNT_LOG2)
+
+#define IVPU_MMU_STREAM_ID0		0
+#define IVPU_MMU_STREAM_ID3		3
+
+#define IVPU_MMU_STRTAB_ENT_SIZE	64
+#define IVPU_MMU_STRTAB_ENT_COUNT	4
+#define IVPU_MMU_STRTAB_CFG_LOG2SIZE	2
+#define IVPU_MMU_STRTAB_CFG		IVPU_MMU_STRTAB_CFG_LOG2SIZE
+
+#define IVPU_MMU_Q_COUNT_LOG2		4 /* 16 entries */
+#define IVPU_MMU_Q_COUNT		((u32)1 << IVPU_MMU_Q_COUNT_LOG2)
+#define IVPU_MMU_Q_WRAP_BIT		(IVPU_MMU_Q_COUNT << 1)
+#define IVPU_MMU_Q_WRAP_MASK		(IVPU_MMU_Q_WRAP_BIT - 1)
+#define IVPU_MMU_Q_IDX_MASK		(IVPU_MMU_Q_COUNT - 1)
+#define IVPU_MMU_Q_IDX(val)		((val) & IVPU_MMU_Q_IDX_MASK)
+
+#define IVPU_MMU_CMDQ_CMD_SIZE		16
+#define IVPU_MMU_CMDQ_SIZE		(IVPU_MMU_Q_COUNT * IVPU_MMU_CMDQ_CMD_SIZE)
+
+#define IVPU_MMU_EVTQ_CMD_SIZE		32
+#define IVPU_MMU_EVTQ_SIZE		(IVPU_MMU_Q_COUNT * IVPU_MMU_EVTQ_CMD_SIZE)
+
+#define IVPU_MMU_CMD_OPCODE		GENMASK(8, 0)
+
+#define IVPU_MMU_CMD_SYNC_0_CS		GENMASK(13, 12)
+#define IVPU_MMU_CMD_SYNC_0_MSH		GENMASK(23, 22)
+#define IVPU_MMU_CMD_SYNC_0_MSI_ATTR	GENMASK(27, 24)
+#define IVPU_MMU_CMD_SYNC_0_MSI_ATTR	GENMASK(27, 24)
+#define IVPU_MMU_CMD_SYNC_0_MSI_DATA	GENMASK(63, 32)
+
+#define IVPU_MMU_CMD_CFGI_0_SSEC	BIT(10)
+#define IVPU_MMU_CMD_CFGI_0_SSV		BIT(11)
+#define IVPU_MMU_CMD_CFGI_0_SSID	GENMASK(31, 12)
+#define IVPU_MMU_CMD_CFGI_0_SID		GENMASK(63, 32)
+#define IVPU_MMU_CMD_CFGI_1_RANGE	GENMASK(4, 0)
+
+#define IVPU_MMU_CMD_TLBI_0_ASID	GENMASK(63, 48)
+#define IVPU_MMU_CMD_TLBI_0_VMID	GENMASK(47, 32)
+
+#define CMD_PREFETCH_CFG		0x1
+#define CMD_CFGI_STE			0x3
+#define CMD_CFGI_ALL			0x4
+#define CMD_CFGI_CD			0x5
+#define CMD_CFGI_CD_ALL			0x6
+#define CMD_TLBI_NH_ASID		0x11
+#define CMD_TLBI_EL2_ALL		0x20
+#define CMD_TLBI_NSNH_ALL		0x30
+#define CMD_SYNC			0x46
+
+#define IVPU_MMU_EVT_F_UUT		0x01
+#define IVPU_MMU_EVT_C_BAD_STREAMID	0x02
+#define IVPU_MMU_EVT_F_STE_FETCH	0x03
+#define IVPU_MMU_EVT_C_BAD_STE		0x04
+#define IVPU_MMU_EVT_F_BAD_ATS_TREQ	0x05
+#define IVPU_MMU_EVT_F_STREAM_DISABLED	0x06
+#define IVPU_MMU_EVT_F_TRANSL_FORBIDDEN	0x07
+#define IVPU_MMU_EVT_C_BAD_SUBSTREAMID	0x08
+#define IVPU_MMU_EVT_F_CD_FETCH		0x09
+#define IVPU_MMU_EVT_C_BAD_CD		0x0a
+#define IVPU_MMU_EVT_F_WALK_EABT	0x0b
+#define IVPU_MMU_EVT_F_TRANSLATION	0x10
+#define IVPU_MMU_EVT_F_ADDR_SIZE	0x11
+#define IVPU_MMU_EVT_F_ACCESS		0x12
+#define IVPU_MMU_EVT_F_PERMISSION	0x13
+#define IVPU_MMU_EVT_F_TLB_CONFLICT	0x20
+#define IVPU_MMU_EVT_F_CFG_CONFLICT	0x21
+#define IVPU_MMU_EVT_E_PAGE_REQUEST	0x24
+#define IVPU_MMU_EVT_F_VMS_FETCH	0x25
+
+#define IVPU_MMU_EVTS_MAX		8
+
+#define IVPU_MMU_EVT_OP_MASK		GENMASK_ULL(7, 0)
+#define IVPU_MMU_EVT_SSID_MASK		GENMASK_ULL(31, 12)
+
+#define IVPU_MMU_Q_BASE_RWA		BIT(62)
+#define IVPU_MMU_Q_BASE_ADDR_MASK	GENMASK_ULL(51, 5)
+#define IVPU_MMU_STRTAB_BASE_RA		BIT(62)
+#define IVPU_MMU_STRTAB_BASE_ADDR_MASK	GENMASK_ULL(51, 6)
+
+#define IVPU_MMU_IRQ_EVTQ_EN		BIT(2)
+#define IVPU_MMU_IRQ_GERROR_EN		BIT(0)
+
+#define IVPU_MMU_CR0_ATSCHK		BIT(4)
+#define IVPU_MMU_CR0_CMDQEN		BIT(3)
+#define IVPU_MMU_CR0_EVTQEN		BIT(2)
+#define IVPU_MMU_CR0_PRIQEN		BIT(1)
+#define IVPU_MMU_CR0_SMMUEN		BIT(0)
+
+#define IVPU_MMU_CR1_TABLE_SH		GENMASK(11, 10)
+#define IVPU_MMU_CR1_TABLE_OC		GENMASK(9, 8)
+#define IVPU_MMU_CR1_TABLE_IC		GENMASK(7, 6)
+#define IVPU_MMU_CR1_QUEUE_SH		GENMASK(5, 4)
+#define IVPU_MMU_CR1_QUEUE_OC		GENMASK(3, 2)
+#define IVPU_MMU_CR1_QUEUE_IC		GENMASK(1, 0)
+#define IVPU_MMU_CACHE_NC		0
+#define IVPU_MMU_CACHE_WB		1
+#define IVPU_MMU_CACHE_WT		2
+#define IVPU_MMU_SH_NSH			0
+#define IVPU_MMU_SH_OSH			2
+#define IVPU_MMU_SH_ISH			3
+
+#define IVPU_MMU_CMDQ_OP		GENMASK_ULL(7, 0)
+
+#define IVPU_MMU_CD_0_TCR_T0SZ		GENMASK_ULL(5, 0)
+#define IVPU_MMU_CD_0_TCR_TG0		GENMASK_ULL(7, 6)
+#define IVPU_MMU_CD_0_TCR_IRGN0		GENMASK_ULL(9, 8)
+#define IVPU_MMU_CD_0_TCR_ORGN0		GENMASK_ULL(11, 10)
+#define IVPU_MMU_CD_0_TCR_SH0		GENMASK_ULL(13, 12)
+#define IVPU_MMU_CD_0_TCR_EPD0		BIT_ULL(14)
+#define IVPU_MMU_CD_0_TCR_EPD1		BIT_ULL(30)
+#define IVPU_MMU_CD_0_ENDI		BIT(15)
+#define IVPU_MMU_CD_0_V			BIT(31)
+#define IVPU_MMU_CD_0_TCR_IPS		GENMASK_ULL(34, 32)
+#define IVPU_MMU_CD_0_TCR_TBI0		BIT_ULL(38)
+#define IVPU_MMU_CD_0_AA64		BIT(41)
+#define IVPU_MMU_CD_0_S			BIT(44)
+#define IVPU_MMU_CD_0_R			BIT(45)
+#define IVPU_MMU_CD_0_A			BIT(46)
+#define IVPU_MMU_CD_0_ASET		BIT(47)
+#define IVPU_MMU_CD_0_ASID		GENMASK_ULL(63, 48)
+
+#define IVPU_MMU_CD_1_TTB0_MASK		GENMASK_ULL(51, 4)
+
+#define IVPU_MMU_STE_0_S1CDMAX		GENMASK_ULL(63, 59)
+#define IVPU_MMU_STE_0_S1FMT		GENMASK_ULL(5, 4)
+#define IVPU_MMU_STE_0_S1FMT_LINEAR	0
+#define IVPU_MMU_STE_DWORDS		8
+#define IVPU_MMU_STE_0_CFG_S1_TRANS	5
+#define IVPU_MMU_STE_0_CFG		GENMASK_ULL(3, 1)
+#define IVPU_MMU_STE_0_S1CTXPTR_MASK	GENMASK_ULL(51, 6)
+#define IVPU_MMU_STE_0_V			BIT(0)
+
+#define IVPU_MMU_STE_1_STRW_NSEL1	0ul
+#define IVPU_MMU_STE_1_CONT		GENMASK_ULL(16, 13)
+#define IVPU_MMU_STE_1_STRW		GENMASK_ULL(31, 30)
+#define IVPU_MMU_STE_1_PRIVCFG		GENMASK_ULL(49, 48)
+#define IVPU_MMU_STE_1_PRIVCFG_UNPRIV	2ul
+#define IVPU_MMU_STE_1_INSTCFG		GENMASK_ULL(51, 50)
+#define IVPU_MMU_STE_1_INSTCFG_DATA	2ul
+#define IVPU_MMU_STE_1_MEV		BIT(19)
+#define IVPU_MMU_STE_1_S1STALLD		BIT(27)
+#define IVPU_MMU_STE_1_S1C_CACHE_NC	0ul
+#define IVPU_MMU_STE_1_S1C_CACHE_WBRA	1ul
+#define IVPU_MMU_STE_1_S1C_CACHE_WT	2ul
+#define IVPU_MMU_STE_1_S1C_CACHE_WB	3ul
+#define IVPU_MMU_STE_1_S1CIR		GENMASK_ULL(3, 2)
+#define IVPU_MMU_STE_1_S1COR		GENMASK_ULL(5, 4)
+#define IVPU_MMU_STE_1_S1CSH		GENMASK_ULL(7, 6)
+#define IVPU_MMU_STE_1_S1DSS		GENMASK_ULL(1, 0)
+#define IVPU_MMU_STE_1_S1DSS_TERMINATE	0x0
+
+#define IVPU_MMU_REG_TIMEOUT_US		(10 * USEC_PER_MSEC)
+#define IVPU_MMU_QUEUE_TIMEOUT_US	(100 * USEC_PER_MSEC)
+
+#define IVPU_MMU_GERROR_ERR_MASK ((REG_FLD(MTL_VPU_HOST_MMU_GERROR, CMDQ)) | \
+				  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, EVTQ_ABT)) | \
+				  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, PRIQ_ABT)) | \
+				  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_CMDQ_ABT)) | \
+				  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_EVTQ_ABT)) | \
+				  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_PRIQ_ABT)) | \
+				  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_ABT)) | \
+				  (REG_FLD(MTL_VPU_HOST_MMU_GERROR, SFM)))
+
+static char *ivpu_mmu_evt_to_str(u32 cmd)
+{
+	switch (cmd) {
+	case IVPU_MMU_EVT_F_UUT:
+		return "Unsupported Upstream Transaction";
+	case IVPU_MMU_EVT_C_BAD_STREAMID:
+		return "Transaction StreamID out of range";
+	case IVPU_MMU_EVT_F_STE_FETCH:
+		return "Fetch of STE caused external abort";
+	case IVPU_MMU_EVT_C_BAD_STE:
+		return "Used STE invalid";
+	case IVPU_MMU_EVT_F_BAD_ATS_TREQ:
+		return "Address Request disallowed for a StreamID";
+	case IVPU_MMU_EVT_F_STREAM_DISABLED:
+		return "Transaction marks non-substream disabled";
+	case IVPU_MMU_EVT_F_TRANSL_FORBIDDEN:
+		return "MMU bypass is disallowed for this StreamID";
+	case IVPU_MMU_EVT_C_BAD_SUBSTREAMID:
+		return "Invalid StreamID";
+	case IVPU_MMU_EVT_F_CD_FETCH:
+		return "Fetch of CD caused external abort";
+	case IVPU_MMU_EVT_C_BAD_CD:
+		return "Fetched CD invalid";
+	case IVPU_MMU_EVT_F_WALK_EABT:
+		return " An external abort occurred fetching a TLB";
+	case IVPU_MMU_EVT_F_TRANSLATION:
+		return "Translation fault";
+	case IVPU_MMU_EVT_F_ADDR_SIZE:
+		return " Output address caused address size fault";
+	case IVPU_MMU_EVT_F_ACCESS:
+		return "Access flag fault";
+	case IVPU_MMU_EVT_F_PERMISSION:
+		return "Permission fault occurred on page access";
+	case IVPU_MMU_EVT_F_TLB_CONFLICT:
+		return "A TLB conflict";
+	case IVPU_MMU_EVT_F_CFG_CONFLICT:
+		return "A configuration cache conflict";
+	case IVPU_MMU_EVT_E_PAGE_REQUEST:
+		return "Page request hint from a client device";
+	case IVPU_MMU_EVT_F_VMS_FETCH:
+		return "Fetch of VMS caused external abort";
+	default:
+		return "Unknown CMDQ command";
+	}
+}
+
+static int ivpu_mmu_config_check(struct ivpu_device *vdev)
+{
+	u32 val_ref;
+	u32 val;
+
+	if (ivpu_is_simics(vdev))
+		val_ref = IVPU_MMU_IDR0_REF_SIMICS;
+	else
+		val_ref = IVPU_MMU_IDR0_REF;
+
+	val = REGV_RD32(MTL_VPU_HOST_MMU_IDR0);
+	if (val != val_ref)
+		ivpu_err(vdev, "IDR0 0x%x != IDR0_REF 0x%x\n", val, val_ref);
+
+	val = REGV_RD32(MTL_VPU_HOST_MMU_IDR1);
+	if (val != IVPU_MMU_IDR1_REF)
+		ivpu_warn(vdev, "IDR1 0x%x != IDR1_REF 0x%x\n", val, IVPU_MMU_IDR1_REF);
+
+	val = REGV_RD32(MTL_VPU_HOST_MMU_IDR3);
+	if (val != IVPU_MMU_IDR3_REF)
+		ivpu_warn(vdev, "IDR3 0x%x != IDR3_REF 0x%x\n", val, IVPU_MMU_IDR3_REF);
+
+	if (ivpu_is_simics(vdev))
+		val_ref = IVPU_MMU_IDR5_REF_SIMICS;
+	else if (ivpu_is_fpga(vdev))
+		val_ref = IVPU_MMU_IDR5_REF_FPGA;
+	else
+		val_ref = IVPU_MMU_IDR5_REF;
+
+	val = REGV_RD32(MTL_VPU_HOST_MMU_IDR5);
+	if (val != val_ref)
+		ivpu_warn(vdev, "IDR5 0x%x != IDR5_REF 0x%x\n", val, val_ref);
+
+	return 0;
+}
+
+static int ivpu_mmu_cdtab_alloc(struct ivpu_device *vdev)
+{
+	struct ivpu_mmu_info *mmu = vdev->mmu;
+	struct ivpu_mmu_cdtab *cdtab = &mmu->cdtab;
+	size_t size = IVPU_MMU_CDTAB_ENT_COUNT * IVPU_MMU_CDTAB_ENT_SIZE;
+
+	cdtab->base = dmam_alloc_coherent(vdev->drm.dev, size, &cdtab->dma, GFP_KERNEL);
+	if (!cdtab->base)
+		return -ENOMEM;
+
+	ivpu_dbg(MMU, "CDTAB alloc: dma=%pad size=%zu\n", &cdtab->dma, size);
+
+	return 0;
+}
+
+static int ivpu_mmu_strtab_alloc(struct ivpu_device *vdev)
+{
+	struct ivpu_mmu_info *mmu = vdev->mmu;
+	struct ivpu_mmu_strtab *strtab = &mmu->strtab;
+	size_t size = IVPU_MMU_STRTAB_ENT_COUNT * IVPU_MMU_STRTAB_ENT_SIZE;
+
+	strtab->base = dmam_alloc_coherent(vdev->drm.dev, size, &strtab->dma, GFP_KERNEL);
+	if (!strtab->base)
+		return -ENOMEM;
+
+	strtab->base_cfg = IVPU_MMU_STRTAB_CFG;
+	strtab->dma_q = IVPU_MMU_STRTAB_BASE_RA;
+	strtab->dma_q |= strtab->dma & IVPU_MMU_STRTAB_BASE_ADDR_MASK;
+
+	ivpu_dbg(MMU, "STRTAB alloc: dma=%pad dma_q=%pad size=%zu\n",
+		 &strtab->dma, &strtab->dma_q, size);
+
+	return 0;
+}
+
+static int ivpu_mmu_cmdq_alloc(struct ivpu_device *vdev)
+{
+	struct ivpu_mmu_info *mmu = vdev->mmu;
+	struct ivpu_mmu_queue *q = &mmu->cmdq;
+
+	q->base = dmam_alloc_coherent(vdev->drm.dev, IVPU_MMU_CMDQ_SIZE, &q->dma, GFP_KERNEL);
+	if (!q->base)
+		return -ENOMEM;
+
+	q->dma_q = IVPU_MMU_Q_BASE_RWA;
+	q->dma_q |= q->dma & IVPU_MMU_Q_BASE_ADDR_MASK;
+	q->dma_q |= IVPU_MMU_Q_COUNT_LOG2;
+
+	ivpu_dbg(MMU, "CMDQ alloc: dma=%pad dma_q=%pad size=%u\n",
+		 &q->dma, &q->dma_q, IVPU_MMU_CMDQ_SIZE);
+
+	return 0;
+}
+
+static int ivpu_mmu_evtq_alloc(struct ivpu_device *vdev)
+{
+	struct ivpu_mmu_info *mmu = vdev->mmu;
+	struct ivpu_mmu_queue *q = &mmu->evtq;
+
+	q->base = dmam_alloc_coherent(vdev->drm.dev, IVPU_MMU_EVTQ_SIZE, &q->dma, GFP_KERNEL);
+	if (!q->base)
+		return -ENOMEM;
+
+	q->dma_q = IVPU_MMU_Q_BASE_RWA;
+	q->dma_q |= q->dma & IVPU_MMU_Q_BASE_ADDR_MASK;
+	q->dma_q |= IVPU_MMU_Q_COUNT_LOG2;
+
+	ivpu_dbg(MMU, "EVTQ alloc: dma=%pad dma_q=%pad size=%u\n",
+		 &q->dma, &q->dma_q, IVPU_MMU_EVTQ_SIZE);
+
+	return 0;
+}
+
+static int ivpu_mmu_structs_alloc(struct ivpu_device *vdev)
+{
+	int ret;
+
+	ret = ivpu_mmu_cdtab_alloc(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to allocate cdtab: %d\n", ret);
+		return ret;
+	}
+
+	ret = ivpu_mmu_strtab_alloc(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to allocate strtab: %d\n", ret);
+		return ret;
+	}
+
+	ret = ivpu_mmu_cmdq_alloc(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to allocate cmdq: %d\n", ret);
+		return ret;
+	}
+
+	ret = ivpu_mmu_evtq_alloc(vdev);
+	if (ret)
+		ivpu_err(vdev, "Failed to allocate evtq: %d\n", ret);
+
+	return ret;
+}
+
+static int ivpu_mmu_reg_write(struct ivpu_device *vdev, u32 reg, u32 val)
+{
+	u32 reg_ack = reg + 4; /* ACK register is 4B after base register */
+	u32 val_ack;
+	int ret;
+
+	REGV_WR32(reg, val);
+
+	ret = REGV_POLL(reg_ack, val_ack, (val == val_ack), IVPU_MMU_REG_TIMEOUT_US);
+	if (ret)
+		ivpu_err(vdev, "Failed to write register 0x%x\n", reg);
+
+	return ret;
+}
+
+static int ivpu_mmu_irqs_setup(struct ivpu_device *vdev)
+{
+	u32 irq_ctrl = IVPU_MMU_IRQ_EVTQ_EN | IVPU_MMU_IRQ_GERROR_EN;
+	int ret;
+
+	ret = ivpu_mmu_reg_write(vdev, MTL_VPU_HOST_MMU_IRQ_CTRL, 0);
+	if (ret)
+		return ret;
+
+	return ivpu_mmu_reg_write(vdev, MTL_VPU_HOST_MMU_IRQ_CTRL, irq_ctrl);
+}
+
+static int ivpu_mmu_cmdq_wait_for_cons(struct ivpu_device *vdev)
+{
+	struct ivpu_mmu_queue *cmdq = &vdev->mmu->cmdq;
+
+	return REGV_POLL(MTL_VPU_HOST_MMU_CMDQ_CONS, cmdq->cons, (cmdq->prod == cmdq->cons),
+			 IVPU_MMU_QUEUE_TIMEOUT_US);
+}
+
+static int ivpu_mmu_cmdq_cmd_write(struct ivpu_device *vdev, const char *name, u64 data0, u64 data1)
+{
+	struct ivpu_mmu_queue *q = &vdev->mmu->cmdq;
+	u64 *queue_buffer = q->base;
+	int idx = IVPU_MMU_Q_IDX(q->prod) * (IVPU_MMU_CMDQ_CMD_SIZE / sizeof(*queue_buffer));
+
+	if (!CIRC_SPACE(IVPU_MMU_Q_IDX(q->prod), IVPU_MMU_Q_IDX(q->cons), IVPU_MMU_Q_COUNT)) {
+		ivpu_err(vdev, "Failed to write MMU CMD %s\n", name);
+		return -EBUSY;
+	}
+
+	queue_buffer[idx] = data0;
+	queue_buffer[idx + 1] = data1;
+	q->prod = (q->prod + 1) & IVPU_MMU_Q_WRAP_MASK;
+
+	ivpu_dbg(MMU, "CMD write: %s data: 0x%llx 0x%llx\n", name, data0, data1);
+
+	return 0;
+}
+
+static int ivpu_mmu_cmdq_sync(struct ivpu_device *vdev)
+{
+	struct ivpu_mmu_queue *q = &vdev->mmu->cmdq;
+	u64 val;
+	int ret;
+
+	val = FIELD_PREP(IVPU_MMU_CMD_OPCODE, CMD_SYNC) |
+	      FIELD_PREP(IVPU_MMU_CMD_SYNC_0_CS, 0x2) |
+	      FIELD_PREP(IVPU_MMU_CMD_SYNC_0_MSH, 0x3) |
+	      FIELD_PREP(IVPU_MMU_CMD_SYNC_0_MSI_ATTR, 0xf);
+
+	ret = ivpu_mmu_cmdq_cmd_write(vdev, "SYNC", val, 0);
+	if (ret)
+		return ret;
+
+	clflush_cache_range(q->base, IVPU_MMU_CMDQ_SIZE);
+	REGV_WR32(MTL_VPU_HOST_MMU_CMDQ_PROD, q->prod);
+
+	ret = ivpu_mmu_cmdq_wait_for_cons(vdev);
+	if (ret)
+		ivpu_err(vdev, "Timed out waiting for consumer: %d\n", ret);
+
+	return ret;
+}
+
+static int ivpu_mmu_cmdq_write_cfgi_all(struct ivpu_device *vdev)
+{
+	u64 data0 = FIELD_PREP(IVPU_MMU_CMD_OPCODE, CMD_CFGI_ALL);
+	u64 data1 = FIELD_PREP(IVPU_MMU_CMD_CFGI_1_RANGE, 0x1f);
+
+	return ivpu_mmu_cmdq_cmd_write(vdev, "CFGI_ALL", data0, data1);
+}
+
+static int ivpu_mmu_cmdq_write_tlbi_nh_asid(struct ivpu_device *vdev, u16 ssid)
+{
+	u64 val = FIELD_PREP(IVPU_MMU_CMD_OPCODE, CMD_TLBI_NH_ASID) |
+		  FIELD_PREP(IVPU_MMU_CMD_TLBI_0_ASID, ssid);
+
+	return ivpu_mmu_cmdq_cmd_write(vdev, "TLBI_NH_ASID", val, 0);
+}
+
+static int ivpu_mmu_cmdq_write_tlbi_nsnh_all(struct ivpu_device *vdev)
+{
+	u64 val = FIELD_PREP(IVPU_MMU_CMD_OPCODE, CMD_TLBI_NSNH_ALL);
+
+	return ivpu_mmu_cmdq_cmd_write(vdev, "TLBI_NSNH_ALL", val, 0);
+}
+
+static int ivpu_mmu_reset(struct ivpu_device *vdev)
+{
+	struct ivpu_mmu_info *mmu = vdev->mmu;
+	u32 val;
+	int ret;
+
+	memset(mmu->cmdq.base, 0, IVPU_MMU_CMDQ_SIZE);
+	clflush_cache_range(mmu->cmdq.base, IVPU_MMU_CMDQ_SIZE);
+	mmu->cmdq.prod = 0;
+	mmu->cmdq.cons = 0;
+
+	memset(mmu->evtq.base, 0, IVPU_MMU_EVTQ_SIZE);
+	clflush_cache_range(mmu->evtq.base, IVPU_MMU_EVTQ_SIZE);
+	mmu->evtq.prod = 0;
+	mmu->evtq.cons = 0;
+
+	ret = ivpu_mmu_reg_write(vdev, MTL_VPU_HOST_MMU_CR0, 0);
+	if (ret)
+		return ret;
+
+	val = FIELD_PREP(IVPU_MMU_CR1_TABLE_SH, IVPU_MMU_SH_ISH) |
+	      FIELD_PREP(IVPU_MMU_CR1_TABLE_OC, IVPU_MMU_CACHE_WB) |
+	      FIELD_PREP(IVPU_MMU_CR1_TABLE_IC, IVPU_MMU_CACHE_WB) |
+	      FIELD_PREP(IVPU_MMU_CR1_QUEUE_SH, IVPU_MMU_SH_ISH) |
+	      FIELD_PREP(IVPU_MMU_CR1_QUEUE_OC, IVPU_MMU_CACHE_WB) |
+	      FIELD_PREP(IVPU_MMU_CR1_QUEUE_IC, IVPU_MMU_CACHE_WB);
+	REGV_WR32(MTL_VPU_HOST_MMU_CR1, val);
+
+	REGV_WR64(MTL_VPU_HOST_MMU_STRTAB_BASE, mmu->strtab.dma_q);
+	REGV_WR32(MTL_VPU_HOST_MMU_STRTAB_BASE_CFG, mmu->strtab.base_cfg);
+
+	REGV_WR64(MTL_VPU_HOST_MMU_CMDQ_BASE, mmu->cmdq.dma_q);
+	REGV_WR32(MTL_VPU_HOST_MMU_CMDQ_PROD, 0);
+	REGV_WR32(MTL_VPU_HOST_MMU_CMDQ_CONS, 0);
+
+	val = IVPU_MMU_CR0_CMDQEN;
+	ret = ivpu_mmu_reg_write(vdev, MTL_VPU_HOST_MMU_CR0, val);
+	if (ret)
+		return ret;
+
+	ret = ivpu_mmu_cmdq_write_cfgi_all(vdev);
+	if (ret)
+		return ret;
+
+	ret = ivpu_mmu_cmdq_write_tlbi_nsnh_all(vdev);
+	if (ret)
+		return ret;
+
+	ret = ivpu_mmu_cmdq_sync(vdev);
+	if (ret)
+		return ret;
+
+	REGV_WR64(MTL_VPU_HOST_MMU_EVTQ_BASE, mmu->evtq.dma_q);
+	REGV_WR32(MTL_VPU_HOST_MMU_EVTQ_PROD_SEC, 0);
+	REGV_WR32(MTL_VPU_HOST_MMU_EVTQ_CONS_SEC, 0);
+
+	val |= IVPU_MMU_CR0_EVTQEN;
+	ret = ivpu_mmu_reg_write(vdev, MTL_VPU_HOST_MMU_CR0, val);
+	if (ret)
+		return ret;
+
+	val |= IVPU_MMU_CR0_ATSCHK;
+	ret = ivpu_mmu_reg_write(vdev, MTL_VPU_HOST_MMU_CR0, val);
+	if (ret)
+		return ret;
+
+	ret = ivpu_mmu_irqs_setup(vdev);
+	if (ret)
+		return ret;
+
+	val |= IVPU_MMU_CR0_SMMUEN;
+	return ivpu_mmu_reg_write(vdev, MTL_VPU_HOST_MMU_CR0, val);
+}
+
+static void ivpu_mmu_strtab_link_cd(struct ivpu_device *vdev, u32 sid)
+{
+	struct ivpu_mmu_info *mmu = vdev->mmu;
+	struct ivpu_mmu_strtab *strtab = &mmu->strtab;
+	struct ivpu_mmu_cdtab *cdtab = &mmu->cdtab;
+	u64 *entry = strtab->base + (sid * IVPU_MMU_STRTAB_ENT_SIZE);
+	u64 str[2];
+
+	str[0] = FIELD_PREP(IVPU_MMU_STE_0_CFG, IVPU_MMU_STE_0_CFG_S1_TRANS) |
+		 FIELD_PREP(IVPU_MMU_STE_0_S1CDMAX, IVPU_MMU_CDTAB_ENT_COUNT_LOG2) |
+		 FIELD_PREP(IVPU_MMU_STE_0_S1FMT, IVPU_MMU_STE_0_S1FMT_LINEAR) |
+		 IVPU_MMU_STE_0_V |
+		 (cdtab->dma & IVPU_MMU_STE_0_S1CTXPTR_MASK);
+
+	str[1] = FIELD_PREP(IVPU_MMU_STE_1_S1DSS, IVPU_MMU_STE_1_S1DSS_TERMINATE) |
+		 FIELD_PREP(IVPU_MMU_STE_1_S1CIR, IVPU_MMU_STE_1_S1C_CACHE_NC) |
+		 FIELD_PREP(IVPU_MMU_STE_1_S1COR, IVPU_MMU_STE_1_S1C_CACHE_NC) |
+		 FIELD_PREP(IVPU_MMU_STE_1_S1CSH, IVPU_MMU_SH_NSH) |
+		 FIELD_PREP(IVPU_MMU_STE_1_PRIVCFG, IVPU_MMU_STE_1_PRIVCFG_UNPRIV) |
+		 FIELD_PREP(IVPU_MMU_STE_1_INSTCFG, IVPU_MMU_STE_1_INSTCFG_DATA) |
+		 FIELD_PREP(IVPU_MMU_STE_1_STRW, IVPU_MMU_STE_1_STRW_NSEL1) |
+		 FIELD_PREP(IVPU_MMU_STE_1_CONT, IVPU_MMU_STRTAB_CFG_LOG2SIZE) |
+		 IVPU_MMU_STE_1_MEV |
+		 IVPU_MMU_STE_1_S1STALLD;
+
+	WRITE_ONCE(entry[1], str[1]);
+	WRITE_ONCE(entry[0], str[0]);
+
+	clflush_cache_range(entry, IVPU_MMU_STRTAB_ENT_SIZE);
+
+	ivpu_dbg(MMU, "STRTAB write entry (SSID=%u): 0x%llx, 0x%llx\n",
+		 sid, str[0], str[1]);
+}
+
+static int ivpu_mmu_strtab_init(struct ivpu_device *vdev)
+{
+	ivpu_mmu_strtab_link_cd(vdev, IVPU_MMU_STREAM_ID0);
+	ivpu_mmu_strtab_link_cd(vdev, IVPU_MMU_STREAM_ID3);
+
+	return 0;
+}
+
+int ivpu_mmu_invalidate_tlb(struct ivpu_device *vdev, u16 ssid)
+{
+	struct ivpu_mmu_info *mmu = vdev->mmu;
+	int ret;
+
+	if (mutex_lock_interruptible(&mmu->lock))
+		return -EINTR;
+
+	if (!mmu->on) {
+		ret = 0;
+		goto unlock;
+	}
+
+	ret = ivpu_mmu_cmdq_write_tlbi_nh_asid(vdev, ssid);
+	if (ret)
+		goto unlock;
+
+	ret = ivpu_mmu_cmdq_sync(vdev);
+unlock:
+	mutex_unlock(&mmu->lock);
+	return ret;
+}
+
+static int ivpu_mmu_cd_add(struct ivpu_device *vdev, u32 ssid, u64 cd_dma)
+{
+	struct ivpu_mmu_info *mmu = vdev->mmu;
+	struct ivpu_mmu_cdtab *cdtab = &mmu->cdtab;
+	u64 *entry;
+	u64 cd[4];
+	int ret;
+
+	if (ssid > IVPU_MMU_CDTAB_ENT_COUNT)
+		return -EINVAL;
+
+	if (mutex_lock_interruptible(&mmu->lock))
+		return -EINTR;
+
+	entry = cdtab->base + (ssid * IVPU_MMU_CDTAB_ENT_SIZE);
+
+	if (cd_dma != 0) {
+		cd[0] = FIELD_PREP(IVPU_MMU_CD_0_TCR_T0SZ, 26) |
+			FIELD_PREP(IVPU_MMU_CD_0_TCR_TG0, 0) |
+			FIELD_PREP(IVPU_MMU_CD_0_TCR_IRGN0, 0) |
+			FIELD_PREP(IVPU_MMU_CD_0_TCR_ORGN0, 0) |
+			FIELD_PREP(IVPU_MMU_CD_0_TCR_SH0, 0) |
+			FIELD_PREP(IVPU_MMU_CD_0_TCR_IPS, 3) |
+			FIELD_PREP(IVPU_MMU_CD_0_ASID, ssid) |
+			IVPU_MMU_CD_0_TCR_EPD1 |
+			IVPU_MMU_CD_0_AA64 |
+			IVPU_MMU_CD_0_R |
+			IVPU_MMU_CD_0_A |
+			IVPU_MMU_CD_0_ASET |
+			IVPU_MMU_CD_0_V;
+		cd[1] = cd_dma & IVPU_MMU_CD_1_TTB0_MASK;
+		cd[2] = 0;
+		cd[3] = 0x0000000000007444;
+	} else {
+		memset(cd, 0, sizeof(cd));
+	}
+
+	WRITE_ONCE(entry[1], cd[1]);
+	WRITE_ONCE(entry[2], cd[2]);
+	WRITE_ONCE(entry[3], cd[3]);
+	WRITE_ONCE(entry[0], cd[0]);
+
+	clflush_cache_range(entry, IVPU_MMU_CDTAB_ENT_SIZE);
+
+	ivpu_dbg(MMU, "CDTAB %s entry (SSID=%u, dma=%pad): 0x%llx, 0x%llx, 0x%llx, 0x%llx\n",
+		 cd_dma ? "write" : "clear", ssid, &cd_dma, cd[0], cd[1], cd[2], cd[3]);
+
+	if (!mmu->on) {
+		ret = 0;
+		goto unlock;
+	}
+
+	ret = ivpu_mmu_cmdq_write_cfgi_all(vdev);
+	if (ret)
+		goto unlock;
+
+	ret = ivpu_mmu_cmdq_sync(vdev);
+unlock:
+	mutex_unlock(&mmu->lock);
+	return ret;
+}
+
+static int ivpu_mmu_cd_add_gbl(struct ivpu_device *vdev)
+{
+	int ret;
+
+	ret = ivpu_mmu_cd_add(vdev, 0, vdev->gctx.pgtable.pgd_dma);
+	if (ret)
+		ivpu_err(vdev, "Failed to add global CD entry: %d\n", ret);
+
+	return ret;
+}
+
+static int ivpu_mmu_cd_add_user(struct ivpu_device *vdev, u32 ssid, dma_addr_t cd_dma)
+{
+	int ret;
+
+	if (ssid == 0) {
+		ivpu_err(vdev, "Invalid SSID: %u\n", ssid);
+		return -EINVAL;
+	}
+
+	ret = ivpu_mmu_cd_add(vdev, ssid, cd_dma);
+	if (ret)
+		ivpu_err(vdev, "Failed to add CD entry SSID=%u: %d\n", ssid, ret);
+
+	return ret;
+}
+
+void ivpu_mmu_fini(struct ivpu_device *vdev)
+{
+	mutex_destroy(&vdev->mmu->lock);
+}
+
+int ivpu_mmu_init(struct ivpu_device *vdev)
+{
+	struct ivpu_mmu_info *mmu = vdev->mmu;
+	int ret;
+
+	ivpu_dbg(MMU, "Init..\n");
+
+	mutex_init(&mmu->lock);
+
+	ret = ivpu_mmu_config_check(vdev);
+	if (ret)
+		goto err;
+
+	ret = ivpu_mmu_structs_alloc(vdev);
+	if (ret)
+		goto err;
+
+	ret = ivpu_mmu_strtab_init(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to initialize strtab: %d\n", ret);
+		goto err;
+	}
+
+	ret = ivpu_mmu_cd_add_gbl(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to initialize strtab: %d\n", ret);
+		goto err;
+	}
+
+	ret = ivpu_mmu_enable(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to resume MMU: %d\n", ret);
+		goto err;
+	}
+
+	ivpu_dbg(MMU, "Init done\n");
+
+	return 0;
+
+err:
+	ivpu_mmu_fini(vdev);
+	return ret;
+}
+
+int ivpu_mmu_enable(struct ivpu_device *vdev)
+{
+	struct ivpu_mmu_info *mmu = vdev->mmu;
+	int ret;
+
+	mutex_lock(&mmu->lock);
+
+	mmu->on = true;
+
+	ret = ivpu_mmu_reset(vdev);
+	if (ret) {
+		ivpu_err(vdev, "Failed to reset MMU: %d\n", ret);
+		goto err;
+	}
+
+	ret = ivpu_mmu_cmdq_write_cfgi_all(vdev);
+	if (ret)
+		goto err;
+
+	ret = ivpu_mmu_cmdq_write_tlbi_nsnh_all(vdev);
+	if (ret)
+		goto err;
+
+	ret = ivpu_mmu_cmdq_sync(vdev);
+	if (ret)
+		goto err;
+
+	mutex_unlock(&mmu->lock);
+
+	return 0;
+err:
+	mmu->on = false;
+	mutex_unlock(&mmu->lock);
+	return ret;
+}
+
+void ivpu_mmu_disable(struct ivpu_device *vdev)
+{
+	struct ivpu_mmu_info *mmu = vdev->mmu;
+
+	mutex_lock(&mmu->lock);
+	mmu->on = false;
+	mutex_unlock(&mmu->lock);
+}
+
+irqreturn_t ivpu_mmu_irq_evtq_handler(struct ivpu_device *vdev)
+{
+	struct ivpu_mmu_queue *evtq = &vdev->mmu->evtq;
+	u64 in_addr, fetch_addr;
+	u32 *evt, op, ssid, sid, counter = 0;
+
+	ivpu_dbg(IRQ, "MMU event queue\n");
+
+	do {
+		evt = (evtq->base + (IVPU_MMU_Q_IDX(evtq->cons) * IVPU_MMU_EVTQ_CMD_SIZE));
+		clflush_cache_range(evt, IVPU_MMU_EVTQ_CMD_SIZE);
+
+		op = FIELD_GET(IVPU_MMU_EVT_OP_MASK, evt[0]);
+		ssid = FIELD_GET(IVPU_MMU_EVT_SSID_MASK, evt[0]);
+		sid = evt[1];
+		in_addr = ((u64)evt[5]) << 32 | evt[4];
+		fetch_addr = ((u64)evt[7]) << 32 | evt[6];
+
+		ivpu_err(vdev, "MMU EVTQ: 0x%x (%s) SSID: %d SID: %d, e[2] %08x, e[3] %08x, in addr: 0x%llx, fetch addr: 0x%llx\n",
+			 op, ivpu_mmu_evt_to_str(op), ssid, sid, evt[2], evt[3],
+			 in_addr, fetch_addr);
+		evtq->cons = (evtq->cons + 1) & IVPU_MMU_Q_WRAP_MASK;
+
+		REGV_WR32(MTL_VPU_HOST_MMU_EVTQ_CONS_SEC, evtq->cons);
+
+		evtq->prod = REGV_RD32(MTL_VPU_HOST_MMU_EVTQ_PROD_SEC);
+
+		if (counter++ >= IVPU_MMU_EVTS_MAX)
+			break;
+
+	} while (evtq->prod != evtq->cons);
+
+	return IRQ_HANDLED;
+}
+
+irqreturn_t ivpu_mmu_irq_gerr_handler(struct ivpu_device *vdev)
+{
+	u32 gerror_val, gerrorn_val, active;
+
+	ivpu_dbg(IRQ, "MMU error\n");
+
+	gerror_val = REGV_RD32(MTL_VPU_HOST_MMU_GERROR);
+	gerrorn_val = REGV_RD32(MTL_VPU_HOST_MMU_GERRORN);
+
+	active = gerror_val ^ gerrorn_val;
+	if (!(active & IVPU_MMU_GERROR_ERR_MASK))
+		return IRQ_NONE;
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_MMU_GERROR, SFM, active))
+		ivpu_err_ratelimited(vdev, "MMU has entered service failure mode\n");
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_ABT, active))
+		ivpu_warn_ratelimited(vdev, "MMU MSI ABT write aborted\n");
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_PRIQ_ABT, active))
+		ivpu_warn_ratelimited(vdev, "MMU PRIQ MSI ABT write aborted\n");
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_EVTQ_ABT, active))
+		ivpu_warn_ratelimited(vdev, "MMU EVTQ MSI ABT write aborted\n");
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_CMDQ_ABT, active))
+		ivpu_warn_ratelimited(vdev, "MMU CMDQ MSI ABT write aborted\n");
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_MMU_GERROR, PRIQ_ABT, active))
+		ivpu_err_ratelimited(vdev, "MMU PRIQ write aborted\n");
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_MMU_GERROR, EVTQ_ABT, active))
+		ivpu_err_ratelimited(vdev, "MMU EVTQ write aborted\n");
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_MMU_GERROR, CMDQ, active))
+		ivpu_err_ratelimited(vdev, "MMU CMDQ write aborted\n");
+
+	REGV_WR32(MTL_VPU_HOST_MMU_GERRORN, gerror_val);
+
+	return IRQ_HANDLED;
+}
+
+int ivpu_mmu_set_pgtable(struct ivpu_device *vdev, int ssid, struct ivpu_mmu_pgtable *pgtable)
+{
+	return ivpu_mmu_cd_add_user(vdev, ssid, pgtable->pgd_dma);
+}
+
+void ivpu_mmu_clear_pgtable(struct ivpu_device *vdev, int ssid)
+{
+	ivpu_mmu_cd_add_user(vdev, ssid, 0); /* 0 will clear CD entry */
+}
drivers/gpu/drm/ivpu/ivpu_mmu.h new file +53 -0
new file mode 100644
index 000000000000..af671c312e63
@@ -0,0 +1,53 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#ifndef __IVPU_MMU_H__
+#define __IVPU_MMU_H__
+
+#include <linux/irqreturn.h>
+
+struct ivpu_device;
+
+struct ivpu_mmu_cdtab {
+	void *base;
+	dma_addr_t dma;
+};
+
+struct ivpu_mmu_strtab {
+	void *base;
+	dma_addr_t dma;
+	u64 dma_q;
+	u32 base_cfg;
+};
+
+struct ivpu_mmu_queue {
+	void *base;
+	dma_addr_t dma;
+	u64 dma_q;
+	u32 prod;
+	u32 cons;
+};
+
+struct ivpu_mmu_info {
+	struct mutex lock; /* Protects cdtab, strtab, cmdq, on */
+	struct ivpu_mmu_cdtab cdtab;
+	struct ivpu_mmu_strtab strtab;
+	struct ivpu_mmu_queue cmdq;
+	struct ivpu_mmu_queue evtq;
+	bool on;
+};
+
+int ivpu_mmu_init(struct ivpu_device *vdev);
+void ivpu_mmu_fini(struct ivpu_device *vdev);
+void ivpu_mmu_disable(struct ivpu_device *vdev);
+int ivpu_mmu_enable(struct ivpu_device *vdev);
+int ivpu_mmu_set_pgtable(struct ivpu_device *vdev, int ssid, struct ivpu_mmu_pgtable *pgtable);
+void ivpu_mmu_clear_pgtable(struct ivpu_device *vdev, int ssid);
+int ivpu_mmu_invalidate_tlb(struct ivpu_device *vdev, u16 ssid);
+
+irqreturn_t ivpu_mmu_irq_evtq_handler(struct ivpu_device *vdev);
+irqreturn_t ivpu_mmu_irq_gerr_handler(struct ivpu_device *vdev);
+
+#endif /* __IVPU_MMU_H__ */
drivers/gpu/drm/ivpu/ivpu_mmu_context.c new file +419 -0
new file mode 100644
index 000000000000..3849ff200450
@@ -0,0 +1,419 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#include <linux/bitfield.h>
+#include <linux/highmem.h>
+
+#include "ivpu_drv.h"
+#include "ivpu_hw.h"
+#include "ivpu_mmu.h"
+#include "ivpu_mmu_context.h"
+
+#define IVPU_MMU_PGD_INDEX_MASK          GENMASK(38, 30)
+#define IVPU_MMU_PMD_INDEX_MASK          GENMASK(29, 21)
+#define IVPU_MMU_PTE_INDEX_MASK          GENMASK(20, 12)
+#define IVPU_MMU_ENTRY_FLAGS_MASK        GENMASK(11, 0)
+#define IVPU_MMU_ENTRY_FLAG_NG           BIT(11)
+#define IVPU_MMU_ENTRY_FLAG_AF           BIT(10)
+#define IVPU_MMU_ENTRY_FLAG_USER         BIT(6)
+#define IVPU_MMU_ENTRY_FLAG_LLC_COHERENT BIT(2)
+#define IVPU_MMU_ENTRY_FLAG_TYPE_PAGE    BIT(1)
+#define IVPU_MMU_ENTRY_FLAG_VALID        BIT(0)
+
+#define IVPU_MMU_PAGE_SIZE    SZ_4K
+#define IVPU_MMU_PTE_MAP_SIZE (IVPU_MMU_PGTABLE_ENTRIES * IVPU_MMU_PAGE_SIZE)
+#define IVPU_MMU_PMD_MAP_SIZE (IVPU_MMU_PGTABLE_ENTRIES * IVPU_MMU_PTE_MAP_SIZE)
+#define IVPU_MMU_PGTABLE_SIZE (IVPU_MMU_PGTABLE_ENTRIES * sizeof(u64))
+
+#define IVPU_MMU_DUMMY_ADDRESS 0xdeadb000
+#define IVPU_MMU_ENTRY_VALID   (IVPU_MMU_ENTRY_FLAG_TYPE_PAGE | IVPU_MMU_ENTRY_FLAG_VALID)
+#define IVPU_MMU_ENTRY_INVALID (IVPU_MMU_DUMMY_ADDRESS & ~IVPU_MMU_ENTRY_FLAGS_MASK)
+#define IVPU_MMU_ENTRY_MAPPED  (IVPU_MMU_ENTRY_FLAG_AF | IVPU_MMU_ENTRY_FLAG_USER | \
+				IVPU_MMU_ENTRY_FLAG_NG | IVPU_MMU_ENTRY_VALID)
+
+static int ivpu_mmu_pgtable_init(struct ivpu_device *vdev, struct ivpu_mmu_pgtable *pgtable)
+{
+	dma_addr_t pgd_dma;
+	u64 *pgd;
+
+	pgd = dma_alloc_wc(vdev->drm.dev, IVPU_MMU_PGTABLE_SIZE, &pgd_dma, GFP_KERNEL);
+	if (!pgd)
+		return -ENOMEM;
+
+	pgtable->pgd = pgd;
+	pgtable->pgd_dma = pgd_dma;
+
+	return 0;
+}
+
+static void ivpu_mmu_pgtable_free(struct ivpu_device *vdev, struct ivpu_mmu_pgtable *pgtable)
+{
+	int pgd_index, pmd_index;
+
+	for (pgd_index = 0; pgd_index < IVPU_MMU_PGTABLE_ENTRIES; ++pgd_index) {
+		u64 **pmd_entries = pgtable->pgd_cpu_entries[pgd_index];
+		u64 *pmd = pgtable->pgd_entries[pgd_index];
+
+		if (!pmd_entries)
+			continue;
+
+		for (pmd_index = 0; pmd_index < IVPU_MMU_PGTABLE_ENTRIES; ++pmd_index) {
+			if (pmd_entries[pmd_index])
+				dma_free_wc(vdev->drm.dev, IVPU_MMU_PGTABLE_SIZE,
+					    pmd_entries[pmd_index],
+					    pmd[pmd_index] & ~IVPU_MMU_ENTRY_FLAGS_MASK);
+		}
+
+		kfree(pmd_entries);
+		dma_free_wc(vdev->drm.dev, IVPU_MMU_PGTABLE_SIZE, pgtable->pgd_entries[pgd_index],
+			    pgtable->pgd[pgd_index] & ~IVPU_MMU_ENTRY_FLAGS_MASK);
+	}
+
+	dma_free_wc(vdev->drm.dev, IVPU_MMU_PGTABLE_SIZE, pgtable->pgd,
+		    pgtable->pgd_dma & ~IVPU_MMU_ENTRY_FLAGS_MASK);
+}
+
+static u64*
+ivpu_mmu_ensure_pmd(struct ivpu_device *vdev, struct ivpu_mmu_pgtable *pgtable, u64 pgd_index)
+{
+	u64 **pmd_entries;
+	dma_addr_t pmd_dma;
+	u64 *pmd;
+
+	if (pgtable->pgd_entries[pgd_index])
+		return pgtable->pgd_entries[pgd_index];
+
+	pmd = dma_alloc_wc(vdev->drm.dev, IVPU_MMU_PGTABLE_SIZE, &pmd_dma, GFP_KERNEL);
+	if (!pmd)
+		return NULL;
+
+	pmd_entries = kzalloc(IVPU_MMU_PGTABLE_SIZE, GFP_KERNEL);
+	if (!pmd_entries)
+		goto err_free_pgd;
+
+	pgtable->pgd_entries[pgd_index] = pmd;
+	pgtable->pgd_cpu_entries[pgd_index] = pmd_entries;
+	pgtable->pgd[pgd_index] = pmd_dma | IVPU_MMU_ENTRY_VALID;
+
+	return pmd;
+
+err_free_pgd:
+	dma_free_wc(vdev->drm.dev, IVPU_MMU_PGTABLE_SIZE, pmd, pmd_dma);
+	return NULL;
+}
+
+static u64*
+ivpu_mmu_ensure_pte(struct ivpu_device *vdev, struct ivpu_mmu_pgtable *pgtable,
+		    int pgd_index, int pmd_index)
+{
+	dma_addr_t pte_dma;
+	u64 *pte;
+
+	if (pgtable->pgd_cpu_entries[pgd_index][pmd_index])
+		return pgtable->pgd_cpu_entries[pgd_index][pmd_index];
+
+	pte = dma_alloc_wc(vdev->drm.dev, IVPU_MMU_PGTABLE_SIZE, &pte_dma, GFP_KERNEL);
+	if (!pte)
+		return NULL;
+
+	pgtable->pgd_cpu_entries[pgd_index][pmd_index] = pte;
+	pgtable->pgd_entries[pgd_index][pmd_index] = pte_dma | IVPU_MMU_ENTRY_VALID;
+
+	return pte;
+}
+
+static int
+ivpu_mmu_context_map_page(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,
+			  u64 vpu_addr, dma_addr_t dma_addr, int prot)
+{
+	u64 *pte;
+	int pgd_index = FIELD_GET(IVPU_MMU_PGD_INDEX_MASK, vpu_addr);
+	int pmd_index = FIELD_GET(IVPU_MMU_PMD_INDEX_MASK, vpu_addr);
+	int pte_index = FIELD_GET(IVPU_MMU_PTE_INDEX_MASK, vpu_addr);
+
+	/* Allocate PMD - second level page table if needed */
+	if (!ivpu_mmu_ensure_pmd(vdev, &ctx->pgtable, pgd_index))
+		return -ENOMEM;
+
+	/* Allocate PTE - third level page table if needed */
+	pte = ivpu_mmu_ensure_pte(vdev, &ctx->pgtable, pgd_index, pmd_index);
+	if (!pte)
+		return -ENOMEM;
+
+	/* Update PTE - third level page table with DMA address */
+	pte[pte_index] = dma_addr | prot;
+
+	return 0;
+}
+
+static void ivpu_mmu_context_unmap_page(struct ivpu_mmu_context *ctx, u64 vpu_addr)
+{
+	int pgd_index = FIELD_GET(IVPU_MMU_PGD_INDEX_MASK, vpu_addr);
+	int pmd_index = FIELD_GET(IVPU_MMU_PMD_INDEX_MASK, vpu_addr);
+	int pte_index = FIELD_GET(IVPU_MMU_PTE_INDEX_MASK, vpu_addr);
+
+	/* Update PTE with dummy physical address and clear flags */
+	ctx->pgtable.pgd_cpu_entries[pgd_index][pmd_index][pte_index] = IVPU_MMU_ENTRY_INVALID;
+}
+
+static void
+ivpu_mmu_context_flush_page_tables(struct ivpu_mmu_context *ctx, u64 vpu_addr, size_t size)
+{
+	u64 end_addr = vpu_addr + size;
+	u64 *pgd = ctx->pgtable.pgd;
+
+	/* Align to PMD entry (2 MB) */
+	vpu_addr &= ~(IVPU_MMU_PTE_MAP_SIZE - 1);
+
+	while (vpu_addr < end_addr) {
+		int pgd_index = FIELD_GET(IVPU_MMU_PGD_INDEX_MASK, vpu_addr);
+		u64 pmd_end = (pgd_index + 1) * (u64)IVPU_MMU_PMD_MAP_SIZE;
+		u64 *pmd = ctx->pgtable.pgd_entries[pgd_index];
+
+		while (vpu_addr < end_addr && vpu_addr < pmd_end) {
+			int pmd_index = FIELD_GET(IVPU_MMU_PMD_INDEX_MASK, vpu_addr);
+			u64 *pte = ctx->pgtable.pgd_cpu_entries[pgd_index][pmd_index];
+
+			clflush_cache_range(pte, IVPU_MMU_PGTABLE_SIZE);
+			vpu_addr += IVPU_MMU_PTE_MAP_SIZE;
+		}
+		clflush_cache_range(pmd, IVPU_MMU_PGTABLE_SIZE);
+	}
+	clflush_cache_range(pgd, IVPU_MMU_PGTABLE_SIZE);
+}
+
+static int
+ivpu_mmu_context_map_pages(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,
+			   u64 vpu_addr, dma_addr_t dma_addr, size_t size, int prot)
+{
+	while (size) {
+		int ret = ivpu_mmu_context_map_page(vdev, ctx, vpu_addr, dma_addr, prot);
+
+		if (ret)
+			return ret;
+
+		vpu_addr += IVPU_MMU_PAGE_SIZE;
+		dma_addr += IVPU_MMU_PAGE_SIZE;
+		size -= IVPU_MMU_PAGE_SIZE;
+	}
+
+	return 0;
+}
+
+static void ivpu_mmu_context_unmap_pages(struct ivpu_mmu_context *ctx, u64 vpu_addr, size_t size)
+{
+	while (size) {
+		ivpu_mmu_context_unmap_page(ctx, vpu_addr);
+		vpu_addr += IVPU_MMU_PAGE_SIZE;
+		size -= IVPU_MMU_PAGE_SIZE;
+	}
+}
+
+int
+ivpu_mmu_context_map_sgt(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,
+			 u64 vpu_addr, struct sg_table *sgt,  bool llc_coherent)
+{
+	struct scatterlist *sg;
+	int prot;
+	int ret;
+	u64 i;
+
+	if (!IS_ALIGNED(vpu_addr, IVPU_MMU_PAGE_SIZE))
+		return -EINVAL;
+	/*
+	 * VPU is only 32 bit, but DMA engine is 38 bit
+	 * Ranges < 2 GB are reserved for VPU internal registers
+	 * Limit range to 8 GB
+	 */
+	if (vpu_addr < SZ_2G || vpu_addr > SZ_8G)
+		return -EINVAL;
+
+	prot = IVPU_MMU_ENTRY_MAPPED;
+	if (llc_coherent)
+		prot |= IVPU_MMU_ENTRY_FLAG_LLC_COHERENT;
+
+	mutex_lock(&ctx->lock);
+
+	for_each_sgtable_dma_sg(sgt, sg, i) {
+		u64 dma_addr = sg_dma_address(sg) - sg->offset;
+		size_t size = sg_dma_len(sg) + sg->offset;
+
+		ret = ivpu_mmu_context_map_pages(vdev, ctx, vpu_addr, dma_addr, size, prot);
+		if (ret) {
+			ivpu_err(vdev, "Failed to map context pages\n");
+			mutex_unlock(&ctx->lock);
+			return ret;
+		}
+		ivpu_mmu_context_flush_page_tables(ctx, vpu_addr, size);
+		vpu_addr += size;
+	}
+
+	mutex_unlock(&ctx->lock);
+
+	ret = ivpu_mmu_invalidate_tlb(vdev, ctx->id);
+	if (ret)
+		ivpu_err(vdev, "Failed to invalidate TLB for ctx %u: %d\n", ctx->id, ret);
+	return ret;
+}
+
+void
+ivpu_mmu_context_unmap_sgt(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,
+			   u64 vpu_addr, struct sg_table *sgt)
+{
+	struct scatterlist *sg;
+	int ret;
+	u64 i;
+
+	if (!IS_ALIGNED(vpu_addr, IVPU_MMU_PAGE_SIZE))
+		ivpu_warn(vdev, "Unaligned vpu_addr: 0x%llx\n", vpu_addr);
+
+	mutex_lock(&ctx->lock);
+
+	for_each_sgtable_dma_sg(sgt, sg, i) {
+		size_t size = sg_dma_len(sg) + sg->offset;
+
+		ivpu_mmu_context_unmap_pages(ctx, vpu_addr, size);
+		ivpu_mmu_context_flush_page_tables(ctx, vpu_addr, size);
+		vpu_addr += size;
+	}
+
+	mutex_unlock(&ctx->lock);
+
+	ret = ivpu_mmu_invalidate_tlb(vdev, ctx->id);
+	if (ret)
+		ivpu_warn(vdev, "Failed to invalidate TLB for ctx %u: %d\n", ctx->id, ret);
+}
+
+int
+ivpu_mmu_context_insert_node_locked(struct ivpu_mmu_context *ctx,
+				    const struct ivpu_addr_range *range,
+				    u64 size, struct drm_mm_node *node)
+{
+	lockdep_assert_held(&ctx->lock);
+
+	return drm_mm_insert_node_in_range(&ctx->mm, node, size, IVPU_MMU_PAGE_SIZE,
+					  0, range->start, range->end, DRM_MM_INSERT_BEST);
+}
+
+void
+ivpu_mmu_context_remove_node_locked(struct ivpu_mmu_context *ctx, struct drm_mm_node *node)
+{
+	lockdep_assert_held(&ctx->lock);
+
+	drm_mm_remove_node(node);
+}
+
+static int
+ivpu_mmu_context_init(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx, u32 context_id)
+{
+	u64 start, end;
+	int ret;
+
+	mutex_init(&ctx->lock);
+	INIT_LIST_HEAD(&ctx->bo_list);
+
+	ret = ivpu_mmu_pgtable_init(vdev, &ctx->pgtable);
+	if (ret)
+		return ret;
+
+	if (!context_id) {
+		start = vdev->hw->ranges.global_low.start;
+		end = vdev->hw->ranges.global_high.end;
+	} else {
+		start = vdev->hw->ranges.user_low.start;
+		end = vdev->hw->ranges.user_high.end;
+	}
+
+	drm_mm_init(&ctx->mm, start, end - start);
+	ctx->id = context_id;
+
+	return 0;
+}
+
+static void ivpu_mmu_context_fini(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx)
+{
+	WARN_ON(!ctx->pgtable.pgd);
+
+	mutex_destroy(&ctx->lock);
+	ivpu_mmu_pgtable_free(vdev, &ctx->pgtable);
+	drm_mm_takedown(&ctx->mm);
+}
+
+int ivpu_mmu_global_context_init(struct ivpu_device *vdev)
+{
+	return ivpu_mmu_context_init(vdev, &vdev->gctx, IVPU_GLOBAL_CONTEXT_MMU_SSID);
+}
+
+void ivpu_mmu_global_context_fini(struct ivpu_device *vdev)
+{
+	return ivpu_mmu_context_fini(vdev, &vdev->gctx);
+}
+
+int ivpu_mmu_user_context_init(struct ivpu_file_priv *file_priv)
+{
+	struct ivpu_device *vdev = file_priv->vdev;
+	u32 context_id;
+	void *old;
+	int ret;
+
+	mutex_lock(&file_priv->lock);
+
+	if (file_priv->ctx.id)
+		goto unlock;
+
+	ret = xa_alloc(&vdev->context_xa, &context_id, NULL, vdev->context_xa_limit, GFP_KERNEL);
+	if (ret) {
+		ivpu_err(vdev, "Failed to allocate context_id\n");
+		goto err_unlock;
+	}
+
+	ret = ivpu_mmu_context_init(vdev, &file_priv->ctx, context_id);
+	if (ret) {
+		ivpu_err(vdev, "Failed to initialize context\n");
+		goto err_erase_context_id;
+	}
+
+	ret = ivpu_mmu_set_pgtable(vdev, context_id, &file_priv->ctx.pgtable);
+	if (ret) {
+		ivpu_err(vdev, "Failed to set page table\n");
+		goto err_context_fini;
+	}
+
+	old = xa_store(&vdev->context_xa, context_id, file_priv, GFP_KERNEL);
+	if (xa_is_err(old)) {
+		ret = xa_err(old);
+		ivpu_err(vdev, "Failed to store context %u: %d\n", context_id, ret);
+		goto err_clear_pgtable;
+	}
+
+	ivpu_dbg(FILE, "file_priv context init: id %u process %s pid %d\n",
+		 context_id, current->comm, task_pid_nr(current));
+
+unlock:
+	mutex_unlock(&file_priv->lock);
+	return 0;
+
+err_clear_pgtable:
+	ivpu_mmu_clear_pgtable(vdev, context_id);
+err_context_fini:
+	ivpu_mmu_context_fini(vdev, &file_priv->ctx);
+err_erase_context_id:
+	xa_erase(&vdev->context_xa, context_id);
+err_unlock:
+	mutex_unlock(&file_priv->lock);
+	return ret;
+}
+
+void ivpu_mmu_user_context_fini(struct ivpu_file_priv *file_priv)
+{
+	struct ivpu_device *vdev = file_priv->vdev;
+
+	WARN_ON(!file_priv->ctx.id);
+
+	xa_store(&vdev->context_xa, file_priv->ctx.id,  NULL, GFP_KERNEL);
+	ivpu_mmu_clear_pgtable(vdev, file_priv->ctx.id);
+	ivpu_mmu_context_fini(vdev, &file_priv->ctx);
+	xa_erase(&vdev->context_xa, file_priv->ctx.id);
+}
drivers/gpu/drm/ivpu/ivpu_mmu_context.h new file +49 -0
new file mode 100644
index 000000000000..af163dfb0e04
@@ -0,0 +1,49 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#ifndef __IVPU_MMU_CONTEXT_H__
+#define __IVPU_MMU_CONTEXT_H__
+
+#include <drm/drm_mm.h>
+
+struct ivpu_device;
+struct ivpu_file_priv;
+struct ivpu_addr_range;
+
+#define IVPU_MMU_PGTABLE_ENTRIES	512
+
+struct ivpu_mmu_pgtable {
+	u64             **pgd_cpu_entries[IVPU_MMU_PGTABLE_ENTRIES];
+	u64		*pgd_entries[IVPU_MMU_PGTABLE_ENTRIES];
+	u64		*pgd;
+	dma_addr_t	pgd_dma;
+};
+
+struct ivpu_mmu_context {
+	struct mutex lock; /* protects: mm, pgtable, bo_list */
+	struct drm_mm mm;
+	struct ivpu_mmu_pgtable pgtable;
+	struct list_head bo_list;
+	u32 id;
+};
+
+int ivpu_mmu_global_context_init(struct ivpu_device *vdev);
+void ivpu_mmu_global_context_fini(struct ivpu_device *vdev);
+
+int ivpu_mmu_user_context_init(struct ivpu_file_priv *file_priv);
+void ivpu_mmu_user_context_fini(struct ivpu_file_priv *file_priv);
+
+int ivpu_mmu_context_insert_node_locked(struct ivpu_mmu_context *ctx,
+					const struct ivpu_addr_range *range,
+				       u64 size, struct drm_mm_node *node);
+void ivpu_mmu_context_remove_node_locked(struct ivpu_mmu_context *ctx,
+					 struct drm_mm_node *node);
+
+int ivpu_mmu_context_map_sgt(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,
+			     u64 vpu_addr, struct sg_table *sgt, bool llc_coherent);
+void ivpu_mmu_context_unmap_sgt(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,
+				u64 vpu_addr, struct sg_table *sgt);
+
+#endif /* __IVPU_MMU_CONTEXT_H__ */
include/uapi/drm/ivpu_drm.h +4 -0
index 7a7dbe2f521b..cb48d6118412 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 · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Maxfeldstr. 5, 90409 Nürnberg, Germany
(HRB 36809, AG Nürnberg)
Geschäftsführer: Ivo Totev
#19 lore raw

Re: [PATCH v3 2/7] drm/ivpu: Add Intel VPU MMU support

Hi,

Quoted 211 lines · Thomas Zimmermann

Hi

Am 24.09.22 um 17:11 schrieb Jacek Lawrynowicz:

Quoted 208 lines

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

Address space is implemented as a struct 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 to 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/gpu/drm/ivpu/Makefile           |   4 +- drivers/gpu/drm/ivpu/ivpu_drv.c         |  59 +- drivers/gpu/drm/ivpu/ivpu_drv.h         |   7 + drivers/gpu/drm/ivpu/ivpu_hw_mtl.c      |  10 + drivers/gpu/drm/ivpu/ivpu_mmu.c         | 883 ++++++++++++++++++++++++ drivers/gpu/drm/ivpu/ivpu_mmu.h         |  53 ++ drivers/gpu/drm/ivpu/ivpu_mmu_context.c | 419 +++++++++++ drivers/gpu/drm/ivpu/ivpu_mmu_context.h |  49 ++ include/uapi/drm/ivpu_drm.h             |   4 +

  9 files changed, 1485 insertions(+), 3 deletions(-)

  create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu.h create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu_context.c create mode 100644 drivers/gpu/drm/ivpu/ivpu_mmu_context.h

drivers/gpu/drm/ivpu/Makefile +3 -1
index e59dc65abe6a..95bb04f26296 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_IVPU) += intel_vpu.o

drivers/gpu/drm/ivpu/ivpu_drv.c +22 -1
index a01c7244f6e5..cbeb9a801a31 100644
@@ -14,6 +14,8 @@
     #include "ivpu_drv.h"
   #include "ivpu_hw.h"
+#include "ivpu_mmu.h"
+#include "ivpu_mmu_context.h"
     #ifndef DRIVER_VERSION_STR
   #define DRIVER_VERSION_STR __stringify(DRM_IVPU_DRIVER_MAJOR) "." \
@@ -50,6 +52,11 @@ char *ivpu_platform_to_str(u32 platform)
     void ivpu_file_priv_get(struct ivpu_file_priv *file_priv, struct ivpu_file_priv **link)
   {
+    struct ivpu_device *vdev = file_priv->vdev;
+
+    ivpu_dbg(KREF, "file_priv get: ctx %u refcount %u\n",
+         file_priv->ctx.id, kref_read(&file_priv->ref));
+
       kref_get(&file_priv->ref);
       *link = file_priv;
   }
@@ -57,6 +64,12 @@ void ivpu_file_priv_get(struct ivpu_file_priv *file_priv, struct ivpu_file_priv
   static void file_priv_release(struct kref *ref)
   {
       struct ivpu_file_priv *file_priv = container_of(ref, struct ivpu_file_priv, ref);
+    struct ivpu_device *vdev = file_priv->vdev;
+
+    ivpu_dbg(FILE, "file_priv release: ctx %u\n", file_priv->ctx.id);
+
+    if (file_priv->ctx.id)
+        ivpu_mmu_user_context_fini(file_priv);
         kfree(file_priv);
   }
@@ -64,6 +77,10 @@ static void file_priv_release(struct kref *ref)
   void ivpu_file_priv_put(struct ivpu_file_priv **link)
   {
       struct ivpu_file_priv *file_priv = *link;
+    struct ivpu_device *vdev = file_priv->vdev;
+
+    ivpu_dbg(KREF, "file_priv put: ctx %u refcount %u\n",
+         file_priv->ctx.id, kref_read(&file_priv->ref));
         *link = NULL;
       kref_put(&file_priv->ref, file_priv_release);
@@ -75,7 +92,11 @@ static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_f
       struct ivpu_device *vdev = file_priv->vdev;
       struct pci_dev *pdev = to_pci_dev(vdev->drm.dev);
       struct drm_ivpu_param *args = data;
-    int ret = 0;
+    int ret;
+
+    ret = ivpu_mmu_user_context_init(file_priv);
+    if (ret)
+        return ret;
         switch (args->param) {

      case DRM_IVPU_PARAM_DEVICE_ID:

drivers/gpu/drm/ivpu/ivpu_drv.c +0 -0
@@ -99,6 +120,9 @@ static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_f

      case DRM_IVPU_PARAM_CONTEXT_PRIORITY:
          args->value = file_priv->priority;
          break;
+    case DRM_IVPU_PARAM_CONTEXT_ID:
+        args->value = file_priv->ctx.id;
+        break;
      default:
          ret = -EINVAL;
      }

drivers/gpu/drm/ivpu/ivpu_drv.c +5 -1
@@ -110,7 +134,11 @@ static int ivpu_set_param_ioctl(struct drm_device *dev, void *data, struct drm_f
   {
       struct ivpu_file_priv *file_priv = file->driver_priv;
       struct drm_ivpu_param *args = data;
-    int ret = 0;
+    int ret;
+
+    ret = ivpu_mmu_user_context_init(file_priv);
+    if (ret)
+        return ret;
         switch (args->param) {

      case DRM_IVPU_PARAM_CONTEXT_PRIORITY:

drivers/gpu/drm/ivpu/ivpu_drv.c +4 -0
@@ -139,9 +167,13 @@ static int ivpu_open(struct drm_device *dev, struct drm_file *file)
       file_priv->priority = DRM_IVPU_CONTEXT_PRIORITY_NORMAL;
         kref_init(&file_priv->ref);
+    mutex_init(&file_priv->lock);
         file->driver_priv = file_priv;
   +    ivpu_dbg(FILE, "file_priv alloc: process %s pid %d\n",
+         current->comm, task_pid_nr(current));
+
       return 0;
   }
   @@ -164,6 +196,7 @@ int ivpu_shutdown(struct ivpu_device *vdev)
       int ret;
         ivpu_hw_irq_disable(vdev);
+    ivpu_mmu_disable(vdev);

        ret = ivpu_hw_power_down(vdev);
      if (ret)

drivers/gpu/drm/ivpu/ivpu_drv.c +3 -0
@@ -272,6 +305,10 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
       if (!vdev->hw)
           return -ENOMEM;
   +    vdev->mmu = devm_kzalloc(vdev->drm.dev, sizeof(*vdev->mmu), GFP_KERNEL);
+    if (!vdev->mmu)
+        return -ENOMEM;
+
       vdev->hw->ops = &ivpu_hw_mtl_ops;
       vdev->platform = IVPU_PLATFORM_INVALID;
   @@ -303,8 +340,24 @@ static int ivpu_dev_init(struct ivpu_device *vdev)

          goto err_irq_fini;
      }
  +    ret = ivpu_mmu_global_context_init(vdev);
+    if (ret) {
+        ivpu_err(vdev, "Failed to initialize global MMU context: %d\n", ret); +        goto err_power_down;
+    }

drivers/gpu/drm/ivpu/ivpu_drv.c +7 -0
+
+    ret = ivpu_mmu_init(vdev);
+    if (ret) {
+        ivpu_err(vdev, "Failed to initialize MMU device: %d\n", ret);
+        goto err_mmu_gctx_fini;
+    }
+

      return 0;
  +err_mmu_gctx_fini:
+    ivpu_mmu_global_context_fini(vdev);
+err_power_down:
+    ivpu_hw_power_down(vdev);
  err_irq_fini:
      ivpu_irq_fini(vdev);
  err_pci_fini:

drivers/gpu/drm/ivpu/ivpu_drv.c +1 -0
@@ -316,6 +369,8 @@ static void ivpu_dev_fini(struct ivpu_device *vdev)
   {
       ivpu_shutdown(vdev);
   +    ivpu_mmu_fini(vdev);
+    ivpu_mmu_global_context_fini(vdev);
       ivpu_irq_fini(vdev);
       ivpu_pci_fini(vdev);

  diff --git a/drivers/gpu/drm/ivpu/ivpu_drv.h b/drivers/gpu/drm/ivpu/ivpu_drv.h index 43dfa78544c6..6eec3eb76c2f 100644

drivers/gpu/drm/ivpu/ivpu_drv.h +0 -0
@@ -14,6 +14,8 @@
   #include <linux/xarray.h>
   #include <uapi/drm/ivpu_drm.h>

  +#include "ivpu_mmu_context.h"
+
  #define DRIVER_NAME "intel_vpu"
  #define DRIVER_DESC "Driver for Intel Versatile Processing Unit (VPU)" #define DRIVER_DATE "20220913"

drivers/gpu/drm/ivpu/ivpu_drv.h +1 -0
@@ -70,6 +72,7 @@ struct ivpu_wa_table {
   };
     struct ivpu_hw_info;
+struct ivpu_mmu_info;
     struct ivpu_device {

      struct drm_device drm; /* Must be first */

drivers/gpu/drm/ivpu/ivpu_drv.h +1 -0
@@ -80,7 +83,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;
Quoted 8 lines
Quoted 6 lines
drivers/gpu/drm/ivpu/ivpu_drv.h +0 -0
       struct xa_limit context_xa_limit;
   @@ -95,6 +100,8 @@ struct ivpu_device {
   struct ivpu_file_priv {

      struct kref ref;
      struct ivpu_device *vdev;
+    struct mutex lock;

There was another lock in the mmu struct, but what's this lock for?

It is used for protecting lazy context init and cmdq added in patch 6. The comment for this mutex is also in patch 6.
I've removed lazy context init so it will be now used only for cmdq. The comment will also be in the correct patch.

Regards,
Jacek

#20 lore raw

PATCH v3 3/7 drm/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 relased when the buffer is destroyed.
Eviction mechism is planned for future versions.

Add three new IOCTLs: BO_CREATE, BO_INFO, BO_USERPTR

---
 drivers/gpu/drm/ivpu/Makefile   |   1 +
 drivers/gpu/drm/ivpu/ivpu_drv.c |   9 +
 drivers/gpu/drm/ivpu/ivpu_gem.c | 823 ++++++++++++++++++++++++++++++++
 drivers/gpu/drm/ivpu/ivpu_gem.h | 128 +++++
 include/uapi/drm/ivpu_drm.h     | 127 +++++
 5 files changed, 1088 insertions(+)
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_gem.c
 create mode 100644 drivers/gpu/drm/ivpu/ivpu_gem.h
drivers/gpu/drm/ivpu/Makefile +1 -0
index 95bb04f26296..4053fe341d56 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/gpu/drm/ivpu/ivpu_drv.c +9 -0
index cbeb9a801a31..b0442e2a4960 100644
@@ -11,8 +11,10 @@
 #include <drm/drm_file.h>
 #include <drm/drm_gem.h>
 #include <drm/drm_ioctl.h>
+#include <drm/drm_prime.h>
 
 #include "ivpu_drv.h"
+#include "ivpu_gem.h"
 #include "ivpu_hw.h"
 #include "ivpu_mmu.h"
 #include "ivpu_mmu_context.h"
@@ -187,6 +189,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, DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(IVPU_SET_PARAM, ivpu_set_param_ioctl, DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(IVPU_BO_CREATE, ivpu_bo_create_ioctl, DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(IVPU_BO_INFO, ivpu_bo_info_ioctl, DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(IVPU_BO_USERPTR, ivpu_bo_userptr_ioctl, DRM_RENDER_ALLOW),
 };
 
 DEFINE_DRM_GEM_FOPS(ivpu_fops);
@@ -210,6 +215,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/gpu/drm/ivpu/ivpu_gem.c new file +823 -0

823 lines not on this page · raw message

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

128 lines not on this page · raw message

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

145 lines not on this page · raw message

Signature · 1 line
2.34.1