/*
- * Copyright (C) ROCKCHIP, Inc.
- * Author:yzq<yzq@rock-chips.com>
- *
- * based on exynos_drm_fb.c
+ * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
+ * Author:Mark Yao <mark.yao@rock-chips.com>
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
+
+#include <linux/kernel.h>
+#include <drm/drm.h>
#include <drm/drmP.h>
-#include <drm/drm_crtc.h>
-#include <drm/drm_crtc_helper.h>
+#include <drm/drm_atomic.h>
#include <drm/drm_fb_helper.h>
-#include <uapi/drm/rockchip_drm.h>
+#include <drm/drm_crtc_helper.h>
+#include <linux/memblock.h>
#include "rockchip_drm_drv.h"
-#include "rockchip_drm_fb.h"
#include "rockchip_drm_gem.h"
-#include "rockchip_drm_iommu.h"
-#include "rockchip_drm_encoder.h"
-#define to_rockchip_fb(x) container_of(x, struct rockchip_drm_fb, fb)
+#define to_rockchip_fb(x) container_of(x, struct rockchip_drm_fb, fb)
-/*
- * rockchip specific framebuffer structure.
- *
- * @fb: drm framebuffer obejct.
- * @buf_cnt: a buffer count to drm framebuffer.
- * @rockchip_gem_obj: array of rockchip specific gem object containing a gem object.
- */
struct rockchip_drm_fb {
- struct drm_framebuffer fb;
- unsigned int buf_cnt;
- struct rockchip_drm_gem_obj *rockchip_gem_obj[MAX_FB_BUFFER];
+ struct drm_framebuffer fb;
+ dma_addr_t dma_addr[ROCKCHIP_MAX_FB_BUFFER];
+ struct drm_gem_object *obj[ROCKCHIP_MAX_FB_BUFFER];
+ struct rockchip_logo *logo;
};
-static int check_fb_gem_memory_type(struct drm_device *drm_dev,
- struct rockchip_drm_gem_obj *rockchip_gem_obj)
+dma_addr_t rockchip_fb_get_dma_addr(struct drm_framebuffer *fb,
+ unsigned int plane)
{
- unsigned int flags;
+ struct rockchip_drm_fb *rk_fb = to_rockchip_fb(fb);
- /*
- * if rockchip drm driver supports iommu then framebuffer can use
- * all the buffer types.
- */
- if (is_drm_iommu_supported(drm_dev))
+ if (WARN_ON(plane >= ROCKCHIP_MAX_FB_BUFFER))
return 0;
- flags = rockchip_gem_obj->flags;
-
- /*
- * without iommu support, not support physically non-continuous memory
- * for framebuffer.
- */
- if (IS_NONCONTIG_BUFFER(flags)) {
- DRM_ERROR("cannot use this gem memory type for fb.\n");
- return -EINVAL;
- }
-
- return 0;
+ return rk_fb->dma_addr[plane];
}
static void rockchip_drm_fb_destroy(struct drm_framebuffer *fb)
{
struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
- unsigned int i;
-
- DRM_DEBUG_KMS("%s\n", __FILE__);
-
- /* make sure that overlay data are updated before relesing fb. */
- rockchip_drm_encoder_complete_scanout(fb);
+ struct drm_gem_object *obj;
+ int i;
- drm_framebuffer_cleanup(fb);
+ for (i = 0; i < ROCKCHIP_MAX_FB_BUFFER; i++) {
+ obj = rockchip_fb->obj[i];
+ if (obj)
+ drm_gem_object_unreference_unlocked(obj);
+ }
- for (i = 0; i < ARRAY_SIZE(rockchip_fb->rockchip_gem_obj); i++) {
- struct drm_gem_object *obj;
+#ifndef MODULE
+ if (rockchip_fb->logo) {
+ struct rockchip_logo *logo = rockchip_fb->logo;
- if (rockchip_fb->rockchip_gem_obj[i] == NULL)
- continue;
+ if (!--logo->count) {
+ void *start = phys_to_virt(logo->start);
+ void *end = phys_to_virt(logo->size);
- obj = &rockchip_fb->rockchip_gem_obj[i]->base;
- drm_gem_object_unreference_unlocked(obj);
+ dma_unmap_sg(fb->dev->dev, logo->sgt->sgl,
+ logo->sgt->nents, DMA_TO_DEVICE);
+ sg_free_table(logo->sgt);
+ memblock_free(logo->start, logo->size);
+ free_reserved_area(start, end, -1, "drm_logo");
+ }
}
+#else
+ WARN_ON(rockchip_fb->logo);
+#endif
+ drm_framebuffer_cleanup(fb);
kfree(rockchip_fb);
- rockchip_fb = NULL;
}
static int rockchip_drm_fb_create_handle(struct drm_framebuffer *fb,
- struct drm_file *file_priv,
- unsigned int *handle)
+ struct drm_file *file_priv,
+ unsigned int *handle)
{
struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
- DRM_DEBUG_KMS("%s\n", __FILE__);
-
- /* This fb should have only one gem object. */
- if (WARN_ON(rockchip_fb->buf_cnt != 1))
- return -EINVAL;
-
return drm_gem_handle_create(file_priv,
- &rockchip_fb->rockchip_gem_obj[0]->base, handle);
+ rockchip_fb->obj[0], handle);
}
-static int rockchip_drm_fb_dirty(struct drm_framebuffer *fb,
- struct drm_file *file_priv, unsigned flags,
- unsigned color, struct drm_clip_rect *clips,
- unsigned num_clips)
-{
- DRM_DEBUG_KMS("%s\n", __FILE__);
-
- /* TODO */
-
- return 0;
-}
-
-static struct drm_framebuffer_funcs rockchip_drm_fb_funcs = {
+static const struct drm_framebuffer_funcs rockchip_drm_fb_funcs = {
.destroy = rockchip_drm_fb_destroy,
.create_handle = rockchip_drm_fb_create_handle,
- .dirty = rockchip_drm_fb_dirty,
};
-void rockchip_drm_fb_set_buf_cnt(struct drm_framebuffer *fb,
- unsigned int cnt)
+struct drm_framebuffer *
+rockchip_fb_alloc(struct drm_device *dev, struct drm_mode_fb_cmd2 *mode_cmd,
+ struct drm_gem_object **obj, struct rockchip_logo *logo,
+ unsigned int num_planes)
{
struct rockchip_drm_fb *rockchip_fb;
+ struct rockchip_gem_object *rk_obj;
+ int ret = 0;
+ int i;
- rockchip_fb = to_rockchip_fb(fb);
+ rockchip_fb = kzalloc(sizeof(*rockchip_fb), GFP_KERNEL);
+ if (!rockchip_fb)
+ return ERR_PTR(-ENOMEM);
- rockchip_fb->buf_cnt = cnt;
-}
+ drm_helper_mode_fill_fb_struct(&rockchip_fb->fb, mode_cmd);
-unsigned int rockchip_drm_fb_get_buf_cnt(struct drm_framebuffer *fb)
-{
- struct rockchip_drm_fb *rockchip_fb;
+ ret = drm_framebuffer_init(dev, &rockchip_fb->fb,
+ &rockchip_drm_fb_funcs);
+ if (ret) {
+ dev_err(dev->dev, "Failed to initialize framebuffer: %d\n",
+ ret);
+ goto err_free_fb;
+ }
+
+ if (obj) {
+ for (i = 0; i < num_planes; i++)
+ rockchip_fb->obj[i] = obj[i];
- rockchip_fb = to_rockchip_fb(fb);
+ for (i = 0; i < num_planes; i++) {
+ rk_obj = to_rockchip_obj(obj[i]);
+ rockchip_fb->dma_addr[i] = rk_obj->dma_addr;
+ }
+#ifndef MODULE
+ } else if (logo) {
+ rockchip_fb->dma_addr[0] = logo->dma_addr;
+ rockchip_fb->logo = logo;
+ logo->count++;
+#endif
+ } else {
+ ret = -EINVAL;
+ dev_err(dev->dev, "Failed to find available buffer\n");
+ goto err_deinit_drm_fb;
+ }
- return rockchip_fb->buf_cnt;
+ return &rockchip_fb->fb;
+
+err_deinit_drm_fb:
+ drm_framebuffer_cleanup(&rockchip_fb->fb);
+err_free_fb:
+ kfree(rockchip_fb);
+ return ERR_PTR(ret);
}
-struct drm_framebuffer *
-rockchip_drm_framebuffer_init(struct drm_device *dev,
- struct drm_mode_fb_cmd2 *mode_cmd,
- struct drm_gem_object *obj)
+static struct drm_framebuffer *
+rockchip_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
+ struct drm_mode_fb_cmd2 *mode_cmd)
{
- struct rockchip_drm_fb *rockchip_fb;
- struct rockchip_drm_gem_obj *rockchip_gem_obj;
+ struct drm_framebuffer *fb;
+ struct drm_gem_object *objs[ROCKCHIP_MAX_FB_BUFFER];
+ struct drm_gem_object *obj;
+ unsigned int hsub;
+ unsigned int vsub;
+ int num_planes;
int ret;
+ int i;
- rockchip_gem_obj = to_rockchip_gem_obj(obj);
+ hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
+ vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);
+ num_planes = min(drm_format_num_planes(mode_cmd->pixel_format),
+ ROCKCHIP_MAX_FB_BUFFER);
- ret = check_fb_gem_memory_type(dev, rockchip_gem_obj);
- if (ret < 0) {
- DRM_ERROR("cannot use this gem memory type for fb.\n");
- return ERR_PTR(-EINVAL);
- }
+ for (i = 0; i < num_planes; i++) {
+ unsigned int width = mode_cmd->width / (i ? hsub : 1);
+ unsigned int height = mode_cmd->height / (i ? vsub : 1);
+ unsigned int min_size;
+ unsigned int bpp =
+ drm_format_plane_bpp(mode_cmd->pixel_format, i);
- rockchip_fb = kzalloc(sizeof(*rockchip_fb), GFP_KERNEL);
- if (!rockchip_fb) {
- DRM_ERROR("failed to allocate rockchip drm framebuffer\n");
- return ERR_PTR(-ENOMEM);
- }
+ obj = drm_gem_object_lookup(dev, file_priv,
+ mode_cmd->handles[i]);
+ if (!obj) {
+ dev_err(dev->dev, "Failed to lookup GEM object\n");
+ ret = -ENXIO;
+ goto err_gem_object_unreference;
+ }
- drm_helper_mode_fill_fb_struct(&rockchip_fb->fb, mode_cmd);
- rockchip_fb->rockchip_gem_obj[0] = rockchip_gem_obj;
+ min_size = (height - 1) * mode_cmd->pitches[i] +
+ mode_cmd->offsets[i] + roundup(width * bpp, 8) / 8;
+ if (obj->size < min_size) {
+ drm_gem_object_unreference_unlocked(obj);
+ ret = -EINVAL;
+ goto err_gem_object_unreference;
+ }
+ objs[i] = obj;
+ }
- ret = drm_framebuffer_init(dev, &rockchip_fb->fb, &rockchip_drm_fb_funcs);
- if (ret) {
- DRM_ERROR("failed to initialize framebuffer\n");
- return ERR_PTR(ret);
+ fb = rockchip_fb_alloc(dev, mode_cmd, objs, NULL, i);
+ if (IS_ERR(fb)) {
+ ret = PTR_ERR(fb);
+ goto err_gem_object_unreference;
}
- return &rockchip_fb->fb;
+ return fb;
+
+err_gem_object_unreference:
+ for (i--; i >= 0; i--)
+ drm_gem_object_unreference_unlocked(objs[i]);
+ return ERR_PTR(ret);
}
-static u32 rockchip_drm_format_num_buffers(struct drm_mode_fb_cmd2 *mode_cmd)
+static void rockchip_drm_output_poll_changed(struct drm_device *dev)
{
- unsigned int cnt = 0;
-
- if (mode_cmd->pixel_format != DRM_FORMAT_NV12)
- return drm_format_num_planes(mode_cmd->pixel_format);
+ struct rockchip_drm_private *private = dev->dev_private;
+ struct drm_fb_helper *fb_helper = private->fbdev_helper;
- while (cnt != MAX_FB_BUFFER) {
- if (!mode_cmd->handles[cnt])
- break;
- cnt++;
- }
+ if (fb_helper)
+ drm_fb_helper_hotplug_event(fb_helper);
+}
- /*
- * check if NV12 or NV12M.
- *
- * NV12
- * handles[0] = base1, offsets[0] = 0
- * handles[1] = base1, offsets[1] = Y_size
- *
- * NV12M
- * handles[0] = base1, offsets[0] = 0
- * handles[1] = base2, offsets[1] = 0
- */
- if (cnt == 2) {
- /*
- * in case of NV12 format, offsets[1] is not 0 and
- * handles[0] is same as handles[1].
- */
- if (mode_cmd->offsets[1] &&
- mode_cmd->handles[0] == mode_cmd->handles[1])
- cnt = 1;
- }
+static void rockchip_crtc_wait_for_update(struct drm_crtc *crtc)
+{
+ struct rockchip_drm_private *priv = crtc->dev->dev_private;
+ int pipe = drm_crtc_index(crtc);
+ const struct rockchip_crtc_funcs *crtc_funcs = priv->crtc_funcs[pipe];
- return cnt;
+ if (crtc_funcs && crtc_funcs->wait_for_update)
+ crtc_funcs->wait_for_update(crtc);
}
-static struct drm_framebuffer *
-rockchip_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
- struct drm_mode_fb_cmd2 *mode_cmd)
+/*
+ * We can't use drm_atomic_helper_wait_for_vblanks() because rk3288 and rk3066
+ * have hardware counters for neither vblanks nor scanlines, which results in
+ * a race where:
+ * | <-- HW vsync irq and reg take effect
+ * plane_commit --> |
+ * get_vblank and wait --> |
+ * | <-- handle_vblank, vblank->count + 1
+ * cleanup_fb --> |
+ * iommu crash --> |
+ * | <-- HW vsync irq and reg take effect
+ *
+ * This function is equivalent but uses rockchip_crtc_wait_for_update() instead
+ * of waiting for vblank_count to change.
+ */
+static void
+rockchip_atomic_wait_for_complete(struct drm_device *dev, struct drm_atomic_state *old_state)
{
- struct drm_gem_object *obj;
- struct rockchip_drm_gem_obj *rockchip_gem_obj;
- struct rockchip_drm_fb *rockchip_fb;
+ struct drm_crtc_state *old_crtc_state;
+ struct drm_crtc *crtc;
int i, ret;
- DRM_DEBUG_KMS("%s\n", __FILE__);
+ for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
+ /* No one cares about the old state, so abuse it for tracking
+ * and store whether we hold a vblank reference (and should do a
+ * vblank wait) in the ->enable boolean.
+ */
+ old_crtc_state->enable = false;
- rockchip_fb = kzalloc(sizeof(*rockchip_fb), GFP_KERNEL);
- if (!rockchip_fb) {
- DRM_ERROR("failed to allocate rockchip drm framebuffer\n");
- return ERR_PTR(-ENOMEM);
+ if (!crtc->state->active)
+ continue;
+
+ if (!drm_atomic_helper_framebuffer_changed(dev,
+ old_state, crtc))
+ continue;
+
+ ret = drm_crtc_vblank_get(crtc);
+ if (ret != 0)
+ continue;
+
+ old_crtc_state->enable = true;
}
- obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
- if (!obj) {
- DRM_ERROR("failed to lookup gem object\n");
- ret = -ENOENT;
- goto err_free;
+ for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
+ if (!old_crtc_state->enable)
+ continue;
+
+ rockchip_crtc_wait_for_update(crtc);
+ drm_crtc_vblank_put(crtc);
}
+}
- drm_helper_mode_fill_fb_struct(&rockchip_fb->fb, mode_cmd);
- rockchip_fb->rockchip_gem_obj[0] = to_rockchip_gem_obj(obj);
- rockchip_fb->buf_cnt = rockchip_drm_format_num_buffers(mode_cmd);
+static void
+rockchip_atomic_commit_complete(struct rockchip_atomic_commit *commit)
+{
+ struct drm_atomic_state *state = commit->state;
+ struct drm_device *dev = commit->dev;
- DRM_DEBUG_KMS("buf_cnt = %d\n", rockchip_fb->buf_cnt);
+ /*
+ * TODO: do fence wait here.
+ */
- for (i = 1; i < rockchip_fb->buf_cnt; i++) {
- obj = drm_gem_object_lookup(dev, file_priv,
- mode_cmd->handles[i]);
- if (!obj) {
- DRM_ERROR("failed to lookup gem object\n");
- ret = -ENOENT;
- rockchip_fb->buf_cnt = i;
- goto err_unreference;
- }
+ /*
+ * Rockchip crtc support runtime PM, can't update display planes
+ * when crtc is disabled.
+ *
+ * drm_atomic_helper_commit comments detail that:
+ * For drivers supporting runtime PM the recommended sequence is
+ *
+ * drm_atomic_helper_commit_modeset_disables(dev, state);
+ *
+ * drm_atomic_helper_commit_modeset_enables(dev, state);
+ *
+ * drm_atomic_helper_commit_planes(dev, state, true);
+ *
+ * See the kerneldoc entries for these three functions for more details.
+ */
+ drm_atomic_helper_commit_modeset_disables(dev, state);
- rockchip_gem_obj = to_rockchip_gem_obj(obj);
- rockchip_fb->rockchip_gem_obj[i] = rockchip_gem_obj;
+ drm_atomic_helper_commit_modeset_enables(dev, state);
- ret = check_fb_gem_memory_type(dev, rockchip_gem_obj);
- if (ret < 0) {
- DRM_ERROR("cannot use this gem memory type for fb.\n");
- goto err_unreference;
- }
- }
+ drm_atomic_helper_commit_planes(dev, state, true);
- ret = drm_framebuffer_init(dev, &rockchip_fb->fb, &rockchip_drm_fb_funcs);
- if (ret) {
- DRM_ERROR("failed to init framebuffer.\n");
- goto err_unreference;
- }
+ rockchip_atomic_wait_for_complete(dev, state);
- return &rockchip_fb->fb;
+ drm_atomic_helper_cleanup_planes(dev, state);
-err_unreference:
- for (i = 0; i < rockchip_fb->buf_cnt; i++) {
- struct drm_gem_object *obj;
+ drm_atomic_state_free(state);
+}
- obj = &rockchip_fb->rockchip_gem_obj[i]->base;
- if (obj)
- drm_gem_object_unreference_unlocked(obj);
- }
-err_free:
- kfree(rockchip_fb);
- return ERR_PTR(ret);
+void rockchip_drm_atomic_work(struct work_struct *work)
+{
+ struct rockchip_atomic_commit *commit = container_of(work,
+ struct rockchip_atomic_commit, work);
+
+ rockchip_atomic_commit_complete(commit);
}
-struct rockchip_drm_gem_buf *rockchip_drm_fb_buffer(struct drm_framebuffer *fb,
- int index)
+int rockchip_drm_atomic_commit(struct drm_device *dev,
+ struct drm_atomic_state *state,
+ bool async)
{
- struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
- struct rockchip_drm_gem_buf *buffer;
+ struct rockchip_drm_private *private = dev->dev_private;
+ struct rockchip_atomic_commit *commit = &private->commit;
+ int ret;
- DRM_DEBUG_KMS("%s\n", __FILE__);
+ ret = drm_atomic_helper_prepare_planes(dev, state);
+ if (ret)
+ return ret;
- if (index >= MAX_FB_BUFFER)
- return NULL;
+ /* serialize outstanding asynchronous commits */
+ mutex_lock(&commit->lock);
+ flush_work(&commit->work);
- buffer = rockchip_fb->rockchip_gem_obj[index]->buffer;
- if (!buffer)
- return NULL;
+ drm_atomic_helper_swap_state(dev, state);
- DRM_DEBUG_KMS("dma_addr = 0x%lx\n", (unsigned long)buffer->dma_addr);
+ commit->dev = dev;
+ commit->state = state;
- return buffer;
-}
+ if (async)
+ schedule_work(&commit->work);
+ else
+ rockchip_atomic_commit_complete(commit);
-static void rockchip_drm_output_poll_changed(struct drm_device *dev)
-{
- struct rockchip_drm_private *private = dev->dev_private;
- struct drm_fb_helper *fb_helper = private->fb_helper;
+ mutex_unlock(&commit->lock);
- if (fb_helper)
- drm_fb_helper_hotplug_event(fb_helper);
+ return 0;
}
static const struct drm_mode_config_funcs rockchip_drm_mode_config_funcs = {
.fb_create = rockchip_user_fb_create,
.output_poll_changed = rockchip_drm_output_poll_changed,
+ .atomic_check = drm_atomic_helper_check,
+ .atomic_commit = rockchip_drm_atomic_commit,
};
+struct drm_framebuffer *
+rockchip_drm_framebuffer_init(struct drm_device *dev,
+ struct drm_mode_fb_cmd2 *mode_cmd,
+ struct drm_gem_object *obj)
+{
+ struct drm_framebuffer *fb;
+
+ fb = rockchip_fb_alloc(dev, mode_cmd, &obj, NULL, 1);
+ if (IS_ERR(fb))
+ return NULL;
+
+ return fb;
+}
+
void rockchip_drm_mode_config_init(struct drm_device *dev)
{
dev->mode_config.min_width = 0;