<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<style>
    font{
        line-height: 1.6;
    }
    ul,ol{
        padding-left: 20px;
        list-style-position: inside;
    }
</style>
<div style="font-family:Helvetica,Helvetica,微软雅黑, 宋体; line-height:1.6;">
    <div></div><div>
    <div><pre wrap="soft" style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); overflow-wrap: break-word;">Add LS7A DC vsync interrupt enable and close function, and
register irq_handler function interface.
Add vbrank event processing flow.

v4:
- Replace drm_irq_install with devm_request_irq.
- Delete the irq_ hooks in drm_driver.

v3:
- Improve code readability.
- Use the to_pci_dev function to get pci_dev.

v2:
- Added error handling in the loongson_drm_load function.

Signed-off-by: Yi Li <liyi@loongson.cn>
Signed-off-by: Chenyang Li <lichenyang@loongson.cn>
---
 drivers/gpu/drm/loongson/Makefile        |  1 +
 drivers/gpu/drm/loongson/loongson_crtc.c | 37 +++++++++++
 drivers/gpu/drm/loongson/loongson_drv.c  |  9 ++-
 drivers/gpu/drm/loongson/loongson_drv.h  | 10 +++
 drivers/gpu/drm/loongson/loongson_irq.c  | 82 ++++++++++++++++++++++++
 5 files changed, 138 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/loongson/loongson_irq.c

diff --git a/drivers/gpu/drm/loongson/Makefile b/drivers/gpu/drm/loongson/Makefile
index 4a5fab7d000b..b083854d789b 100644
--- a/drivers/gpu/drm/loongson/Makefile
+++ b/drivers/gpu/drm/loongson/Makefile
@@ -10,5 +10,6 @@ loongson-y := loongson_connector.o \
        loongson_drv.o \
        loongson_encoder.o \
        loongson_i2c.o \
+       loongson_irq.o \
        loongson_plane.o
 obj-$(CONFIG_DRM_LOONGSON) += loongson.o
diff --git a/drivers/gpu/drm/loongson/loongson_crtc.c b/drivers/gpu/drm/loongson/loongson_crtc.c
index a9735fbb1c2e..10d6f9a16f4e 100644
--- a/drivers/gpu/drm/loongson/loongson_crtc.c
+++ b/drivers/gpu/drm/loongson/loongson_crtc.c
@@ -3,6 +3,7 @@
 
 #include <drm/drm_fourcc.h>
 #include <drm/drm_framebuffer.h>
+#include <drm/drm_vblank.h>
 
 #include "loongson_drv.h"
 
@@ -165,8 +166,14 @@ static void loongson_crtc_atomic_enable(struct drm_crtc *crtc,
        struct loongson_crtc *lcrtc = to_loongson_crtc(crtc);
        u32 reg_offset = lcrtc->reg_offset;
 
+       if (lcrtc->cfg_reg & CFG_ENABLE)
+               goto vblank_on;
+
        lcrtc->cfg_reg |= CFG_ENABLE;
        ls7a_mm_wreg(ldev, FB_CFG_REG + reg_offset, lcrtc->cfg_reg);
+
+vblank_on:
+       drm_crtc_vblank_on(crtc);
 }
 
 static void loongson_crtc_atomic_disable(struct drm_crtc *crtc,
@@ -179,6 +186,33 @@ static void loongson_crtc_atomic_disable(struct drm_crtc *crtc,
 
        lcrtc->cfg_reg &= ~CFG_ENABLE;
        ls7a_mm_wreg(ldev, FB_CFG_REG + reg_offset, lcrtc->cfg_reg);
+
+       spin_lock_irq(&crtc->dev->event_lock);
+       if (crtc->state->event) {
+               drm_crtc_send_vblank_event(crtc, crtc->state->event);
+               crtc->state->event = NULL;
+       }
+       spin_unlock_irq(&crtc->dev->event_lock);
+
+       drm_crtc_vblank_off(crtc);
+}
+
+static void loongson_crtc_atomic_flush(struct drm_crtc *crtc,
+                                      struct drm_atomic_state *state)
+{
+       struct drm_pending_vblank_event *event = crtc->state->event;
+
+       if (!event)
+               return;
+
+       crtc->state->event = NULL;
+
+       spin_lock_irq(&crtc->dev->event_lock);
+       if (drm_crtc_vblank_get(crtc) == 0)
+               drm_crtc_arm_vblank_event(crtc, event);
+       else
+               drm_crtc_send_vblank_event(crtc, event);
+       spin_unlock_irq(&crtc->dev->event_lock);
 }
 
 static enum drm_mode_status loongson_mode_valid(struct drm_crtc *crtc,
@@ -198,6 +232,7 @@ static enum drm_mode_status loongson_mode_valid(struct drm_crtc *crtc,
 
 static const struct drm_crtc_helper_funcs loongson_crtc_helper_funcs = {
        .mode_valid = loongson_mode_valid,
+       .atomic_flush = loongson_crtc_atomic_flush,
        .atomic_enable = loongson_crtc_atomic_enable,
        .atomic_disable = loongson_crtc_atomic_disable,
        .mode_set_nofb = loongson_crtc_mode_set_nofb,
@@ -209,6 +244,8 @@ static const struct drm_crtc_funcs loongson_crtc_funcs = {
        .reset = drm_atomic_helper_crtc_reset,
        .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
        .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
+       .enable_vblank = loongson_crtc_enable_vblank,
+       .disable_vblank = loongson_crtc_disable_vblank,
 };
 
 int loongson_crtc_init(struct loongson_device *ldev, int index)
diff --git a/drivers/gpu/drm/loongson/loongson_drv.c b/drivers/gpu/drm/loongson/loongson_drv.c
index 9120d8188ddf..c9702970a037 100644
--- a/drivers/gpu/drm/loongson/loongson_drv.c
+++ b/drivers/gpu/drm/loongson/loongson_drv.c
@@ -21,9 +21,10 @@
 /* Interface history:
  * 0.1 - original.
  * 0.2 - add i2c and connector detect.
+ * 0.3 - Vblank and vsync interrupt support.
  */
 #define DRIVER_MAJOR 0
-#define DRIVER_MINOR 2
+#define DRIVER_MINOR 3
 
 static const struct drm_mode_config_funcs loongson_mode_funcs = {
        .fb_create = drm_gem_fb_create,
@@ -165,6 +166,12 @@ static int loongson_driver_init(struct drm_device *dev)
                goto err;
        }
 
+       ret = loongson_irq_init(ldev);
+       if (ret) {
+               dev_err(dev->dev, "Fatal error during irq init: %d\n", ret);
+               goto err;
+       }
+
        drm_kms_helper_poll_init(dev);
        drm_mode_config_reset(dev);
 
diff --git a/drivers/gpu/drm/loongson/loongson_drv.h b/drivers/gpu/drm/loongson/loongson_drv.h
index e8ca0eac827c..ec22dff89e88 100644
--- a/drivers/gpu/drm/loongson/loongson_drv.h
+++ b/drivers/gpu/drm/loongson/loongson_drv.h
@@ -38,6 +38,7 @@
 #define FB_HSYNC_REG (0x1420)
 #define FB_VDISPLAY_REG (0x1480)
 #define FB_VSYNC_REG (0x14a0)
+#define FB_INT_REG (0x1570)
 
 #define CFG_FMT GENMASK(2, 0)
 #define CFG_FBSWITCH BIT(7)
@@ -49,6 +50,10 @@
 #define FB_PANCFG_DEF 0x80001311
 #define FB_HSYNC_PULSE (1 << 30)
 #define FB_VSYNC_PULSE (1 << 30)
+#define FB_VSYNC1_ENABLE (1 << 16)
+#define FB_VSYNC0_ENABLE (1 << 18)
+#define FB_VSYNC1_INT (1 << 0)
+#define FB_VSYNC0_INT (1 << 2)
 
 /* PIX PLL */
 #define LOOPC_MIN 24
@@ -126,6 +131,11 @@ struct loongson_plane *loongson_plane_init(struct drm_device *dev, int index);
 /* i2c */
 int loongson_dc_gpio_init(struct loongson_device *ldev);
 
+/* irq */
+int loongson_irq_init(struct loongson_device *ldev);
+int loongson_crtc_enable_vblank(struct drm_crtc *crtc);
+void loongson_crtc_disable_vblank(struct drm_crtc *crtc);
+
 /* device */
 u32 loongson_gpu_offset(struct drm_plane_state *state,
                        struct loongson_device *dev);
diff --git a/drivers/gpu/drm/loongson/loongson_irq.c b/drivers/gpu/drm/loongson/loongson_irq.c
new file mode 100644
index 000000000000..89e7198115d3
--- /dev/null
+++ b/drivers/gpu/drm/loongson/loongson_irq.c
@@ -0,0 +1,82 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/* Copyright (C) 2022 Loongson Inc. */
+
+#include <linux/pci.h>
+
+#include <drm/drm_vblank.h>
+
+#include "loongson_drv.h"
+
+static irqreturn_t loongson_irq_handler(int irq, void *arg)
+{
+       struct drm_device *dev = (struct drm_device *) arg;
+       struct loongson_device *ldev = to_loongson_device(dev);
+       struct loongson_crtc *lcrtc;
+       u32 val;
+
+       val = ls7a_mm_rreg(ldev, FB_INT_REG);
+       ls7a_mm_wreg(ldev, FB_INT_REG, val & (0xffff << 16));
+
+       if (val & FB_VSYNC0_INT)
+               lcrtc = ldev->mode_info[0].crtc;
+       else if (val & FB_VSYNC1_INT)
+               lcrtc = ldev->mode_info[1].crtc;
+
+       drm_crtc_handle_vblank(&lcrtc->base);
+
+       return IRQ_HANDLED;
+}
+
+int loongson_irq_init(struct loongson_device *ldev)
+{
+       int ret;
+       struct drm_device *dev = &ldev->dev;
+       int irq = to_pci_dev(dev->dev)->irq;
+
+       ret = drm_vblank_init(dev, ldev->num_crtc);
+       if (ret) {
+               dev_err(dev->dev, "Fatal error during vblank init: %d\n", ret);
+               return ret;
+       }
+       DRM_INFO("drm vblank init finished\n");
+
+       ret = devm_request_irq(dev->dev, irq, loongson_irq_handler, 0,
+                              "loongson-drm", dev);
+       if (ret) {
+               dev_err(dev->dev, "Fatal error during irq install: %d\n", ret);
+               return ret;
+       }
+       DRM_INFO("loongson irq initialized\n");
+
+       return 0;
+}
+
+int loongson_crtc_enable_vblank(struct drm_crtc *crtc)
+{
+       struct loongson_crtc *lcrtc = to_loongson_crtc(crtc);
+       struct loongson_device *ldev = lcrtc->ldev;
+       u32 reg_val = ls7a_mm_rreg(ldev, FB_INT_REG);
+
+       if (lcrtc->crtc_id)
+               reg_val |= FB_VSYNC1_ENABLE;
+       else
+               reg_val |= FB_VSYNC0_ENABLE;
+
+       ls7a_mm_wreg(ldev, FB_INT_REG, reg_val);
+
+       return 0;
+}
+
+void loongson_crtc_disable_vblank(struct drm_crtc *crtc)
+{
+       struct loongson_crtc *lcrtc = to_loongson_crtc(crtc);
+       struct loongson_device *ldev = lcrtc->ldev;
+       u32 reg_val = ls7a_mm_rreg(ldev, FB_INT_REG);
+
+       if (lcrtc->crtc_id)
+               reg_val &= ~FB_VSYNC1_ENABLE;
+       else
+               reg_val &= ~FB_VSYNC0_ENABLE;
+
+       ls7a_mm_wreg(ldev, FB_INT_REG, reg_val);
+}
-- 
2.25.1</pre></div>
</div><!--�-->
</div>
</body>
</html>