#include <linux/component.h>
 #include <linux/clk.h>
 #include <linux/dma-mapping.h>
+#include <linux/io.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
 #include <linux/of_device.h>
 #include <drm/drm_color_mgmt.h>
 #include <drm/drm_crtc.h>
 #include <drm/drm_crtc_helper.h>
+#include <drm/drm_damage_helper.h>
 #include <drm/drm_drv.h>
 #include <drm/drm_encoder.h>
 #include <drm/drm_gem_cma_helper.h>
 struct jz_soc_info {
        bool needs_dev_clk;
        bool has_osd;
+       bool map_noncoherent;
        unsigned int max_width, max_height;
        const u32 *formats_f0, *formats_f1;
        unsigned int num_formats_f0, num_formats_f1;
             old_plane_state->fb->format->format != new_plane_state->fb->format->format))
                crtc_state->mode_changed = true;
 
+       if (priv->soc_info->map_noncoherent)
+               drm_atomic_helper_check_plane_damage(state, new_plane_state);
+
        return 0;
 }
 
        }
 }
 
+bool ingenic_drm_map_noncoherent(const struct device *dev)
+{
+       const struct ingenic_drm *priv = dev_get_drvdata(dev);
+
+       return priv->soc_info->map_noncoherent;
+}
+
 static void ingenic_drm_update_palette(struct ingenic_drm *priv,
                                       const struct drm_color_lut *lut)
 {
                                            struct drm_atomic_state *state)
 {
        struct ingenic_drm *priv = drm_device_get_priv(plane->dev);
-       struct drm_plane_state *newstate = drm_atomic_get_new_plane_state(state,
-                                                                         plane);
+       struct drm_plane_state *newstate = drm_atomic_get_new_plane_state(state, plane);
+       struct drm_plane_state *oldstate = drm_atomic_get_old_plane_state(state, plane);
        struct drm_crtc_state *crtc_state;
        struct ingenic_dma_hwdesc *hwdesc;
        unsigned int width, height, cpp, offset;
        u32 fourcc;
 
        if (newstate && newstate->fb) {
+               if (priv->soc_info->map_noncoherent)
+                       drm_fb_cma_sync_non_coherent(&priv->drm, oldstate, newstate);
+
                crtc_state = newstate->crtc->state;
 
                addr = drm_fb_cma_get_gem_addr(newstate->fb, newstate, 0);
        regmap_update_bits(priv->map, JZ_REG_LCD_CTRL, JZ_LCD_CTRL_EOF_IRQ, 0);
 }
 
+static struct drm_framebuffer *
+ingenic_drm_gem_fb_create(struct drm_device *drm, struct drm_file *file,
+                         const struct drm_mode_fb_cmd2 *mode_cmd)
+{
+       struct ingenic_drm *priv = drm_device_get_priv(drm);
+
+       if (priv->soc_info->map_noncoherent)
+               return drm_gem_fb_create_with_dirty(drm, file, mode_cmd);
+
+       return drm_gem_fb_create(drm, file, mode_cmd);
+}
+
+static struct drm_gem_object *
+ingenic_drm_gem_create_object(struct drm_device *drm, size_t size)
+{
+       struct ingenic_drm *priv = drm_device_get_priv(drm);
+       struct drm_gem_cma_object *obj;
+
+       obj = kzalloc(sizeof(*obj), GFP_KERNEL);
+       if (!obj)
+               return ERR_PTR(-ENOMEM);
+
+       obj->map_noncoherent = priv->soc_info->map_noncoherent;
+
+       return &obj->base;
+}
+
 DEFINE_DRM_GEM_CMA_FOPS(ingenic_drm_fops);
 
 static const struct drm_driver ingenic_drm_driver_data = {
        .patchlevel             = 0,
 
        .fops                   = &ingenic_drm_fops,
+       .gem_create_object      = ingenic_drm_gem_create_object,
        DRM_GEM_CMA_DRIVER_OPS,
 
        .irq_handler            = ingenic_drm_irq_handler,
 };
 
 static const struct drm_mode_config_funcs ingenic_drm_mode_config_funcs = {
-       .fb_create              = drm_gem_fb_create,
+       .fb_create              = ingenic_drm_gem_fb_create,
        .output_poll_changed    = drm_fb_helper_output_poll_changed,
        .atomic_check           = drm_atomic_helper_check,
        .atomic_commit          = drm_atomic_helper_commit,
                return ret;
        }
 
+       if (soc_info->map_noncoherent)
+               drm_plane_enable_fb_damage_clips(&priv->f1);
+
        drm_crtc_helper_add(&priv->crtc, &ingenic_drm_crtc_helper_funcs);
 
        ret = drm_crtc_init_with_planes(drm, &priv->crtc, primary,
                        return ret;
                }
 
+               if (soc_info->map_noncoherent)
+                       drm_plane_enable_fb_damage_clips(&priv->f0);
+
                if (IS_ENABLED(CONFIG_DRM_INGENIC_IPU) && has_components) {
                        ret = component_bind_all(dev, drm);
                        if (ret) {
 static const struct jz_soc_info jz4740_soc_info = {
        .needs_dev_clk = true,
        .has_osd = false,
+       .map_noncoherent = false,
        .max_width = 800,
        .max_height = 600,
        .formats_f1 = jz4740_formats,
 static const struct jz_soc_info jz4725b_soc_info = {
        .needs_dev_clk = false,
        .has_osd = true,
+       .map_noncoherent = false,
        .max_width = 800,
        .max_height = 600,
        .formats_f1 = jz4725b_formats_f1,
 static const struct jz_soc_info jz4770_soc_info = {
        .needs_dev_clk = false,
        .has_osd = true,
+       .map_noncoherent = true,
        .max_width = 1280,
        .max_height = 720,
        .formats_f1 = jz4770_formats_f1,
 
 
 #include <drm/drm_atomic.h>
 #include <drm/drm_atomic_helper.h>
+#include <drm/drm_damage_helper.h>
 #include <drm/drm_drv.h>
 #include <drm/drm_fb_cma_helper.h>
 #include <drm/drm_fourcc.h>
 #include <drm/drm_gem_atomic_helper.h>
+#include <drm/drm_gem_cma_helper.h>
+#include <drm/drm_gem_framebuffer_helper.h>
 #include <drm/drm_plane.h>
 #include <drm/drm_plane_helper.h>
 #include <drm/drm_property.h>
                                            struct drm_atomic_state *state)
 {
        struct ingenic_ipu *ipu = plane_to_ingenic_ipu(plane);
-       struct drm_plane_state *newstate = drm_atomic_get_new_plane_state(state,
-                                                                         plane);
+       struct drm_plane_state *newstate = drm_atomic_get_new_plane_state(state, plane);
+       struct drm_plane_state *oldstate = drm_atomic_get_old_plane_state(state, plane);
        const struct drm_format_info *finfo;
        u32 ctrl, stride = 0, coef_index = 0, format = 0;
        bool needs_modeset, upscaling_w, upscaling_h;
                                JZ_IPU_CTRL_CHIP_EN | JZ_IPU_CTRL_LCDC_SEL);
        }
 
+       if (ingenic_drm_map_noncoherent(ipu->master))
+               drm_fb_cma_sync_non_coherent(ipu->drm, oldstate, newstate);
+
        /* New addresses will be committed in vblank handler... */
        ipu->addr_y = drm_fb_cma_get_gem_addr(newstate->fb, newstate, 0);
        if (finfo->num_planes > 1)
 
        if (!new_plane_state->crtc ||
            !crtc_state->mode.hdisplay || !crtc_state->mode.vdisplay)
-               return 0;
+               goto out_check_damage;
 
        /* Plane must be fully visible */
        if (new_plane_state->crtc_x < 0 || new_plane_state->crtc_y < 0 ||
                return -EINVAL;
 
        if (!osd_changed(new_plane_state, old_plane_state))
-               return 0;
+               goto out_check_damage;
 
        crtc_state->mode_changed = true;
 
        ipu->denom_w = denom_w;
        ipu->denom_h = denom_h;
 
+out_check_damage:
+       if (ingenic_drm_map_noncoherent(ipu->master))
+               drm_atomic_helper_check_plane_damage(state, new_plane_state);
+
        return 0;
 }
 
                return err;
        }
 
+       if (ingenic_drm_map_noncoherent(master))
+               drm_plane_enable_fb_damage_clips(plane);
+
        /*
         * Sharpness settings range is [0,32]
         * 0       : nearest-neighbor