From patchwork Thu Oct 13 08:37:40 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rongrong Zou X-Patchwork-Id: 9374567 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 6B21660839 for ; Thu, 13 Oct 2016 09:01:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5A42029E6A for ; Thu, 13 Oct 2016 09:01:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4F1DA29E70; Thu, 13 Oct 2016 09:01:23 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 7E4AF29E90 for ; Thu, 13 Oct 2016 09:01:22 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id EF5946E9C1; Thu, 13 Oct 2016 09:01:17 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-yw0-x242.google.com (mail-yw0-x242.google.com [IPv6:2607:f8b0:4002:c05::242]) by gabe.freedesktop.org (Postfix) with ESMTPS id D555D6E9A5 for ; Thu, 13 Oct 2016 08:38:31 +0000 (UTC) Received: by mail-yw0-x242.google.com with SMTP id e5so2745987ywc.3 for ; Thu, 13 Oct 2016 01:38:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=ylv2368JNesEoY6ZxcgY1c5e0+bO9qmkX0RBrFNv2SE=; b=Zzs6u4V4kj++HZWrMhqg2wo1BeM11SzSW0eUKXEGjfwGkiaSS9nqFnR4o4eJqe3nZi nARGavIL0Mdxdx10E95wFpARroV4Al3UQ2Wu40M+D+svME8WQcTcWmQ31JVqhVYwRmm2 7n1EEA6fxe7a4pi31+1zYe+hAEkDjXtaZmUj1yXSTdpJV8o5qodrqUbQ89eyMHdUoa0O Eo1BvIy3S26p0Ust2jcKJlvDlqQmMGW5Goca6DdleTi4hwCjPXRwygkKyDo7W2v/0a7o /oRg0x1NXVJEuJ5zyT8xof6j2DYC9HzSEvqhNKGTNA0KiOCjctfG49sjJvmA0ZodzOcl V+yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=ylv2368JNesEoY6ZxcgY1c5e0+bO9qmkX0RBrFNv2SE=; b=NU2eoYFqxCwg4OpNPpJPe3IpJ3FEbOkRxTg1C01cFbhJzQv0mz2wEo52BpHqdL5QZi kDK/GbzWlipNtx5o2q6pS6usQJV8Z4A0RKOBc1B7fym9eyYGiJlnL6dFSoU9ZqaViqUY casHKFjZ0FapLSf+raKVZpwFWc+HOib7lG81U3Ym5BPpUULK+2WvRPtrzbH65W/gxDnj zKjk8Zi3O9l+gXeF7kfW04DPt2AurD3V/jIGnGERMScqbqq9FkWqwM6v5fl3f6HWkGed Lvn3BGJdl0iHZLbewTg8oVbrRh7EmGJ+Zg7kn+E8y5DGpPkjL7Yr/PQfywZ1P8iMK5HZ 899Q== X-Gm-Message-State: AA6/9RmtL7SIJ/NICnGW3lIRFwmMFxyZNWQxTCB6tWe0qTahlBuaMNny9xq151EoruP9dA== X-Received: by 10.129.74.68 with SMTP id x65mr4383969ywa.157.1476347910910; Thu, 13 Oct 2016 01:38:30 -0700 (PDT) Received: from localhost.localdomain ([119.145.15.121]) by smtp.gmail.com with ESMTPSA id x188sm4281836ywx.8.2016.10.13.01.38.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 13 Oct 2016 01:38:30 -0700 (PDT) From: Rongrong Zou To: airlied@linux.ie, daniel@ffwll.ch, dri-devel@lists.freedesktop.org, emil.l.velikov@gmail.com Subject: [PATCH v3 3/9] drm/hisilicon/hibmc: Add support for frame buffer Date: Thu, 13 Oct 2016 16:37:40 +0800 Message-Id: <1476347866-71197-4-git-send-email-zourongrong@gmail.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1476347866-71197-1-git-send-email-zourongrong@gmail.com> References: <1476347866-71197-1-git-send-email-zourongrong@gmail.com> X-Mailman-Approved-At: Thu, 13 Oct 2016 09:01:05 +0000 Cc: tomeu.vizoso@collabora.com, linuxarm@huawei.com, guohanjun@huawei.com, lijianhua@huawei.com X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP Add support for fbdev and kms fb management. Signed-off-by: Rongrong Zou --- drivers/gpu/drm/hisilicon/hibmc/Makefile | 2 +- drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.c | 18 ++ drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h | 24 ++ drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_fbdev.c | 259 ++++++++++++++++++++++ drivers/gpu/drm/hisilicon/hibmc/hibmc_ttm.c | 68 ++++++ 5 files changed, 370 insertions(+), 1 deletion(-) create mode 100644 drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_fbdev.c diff --git a/drivers/gpu/drm/hisilicon/hibmc/Makefile b/drivers/gpu/drm/hisilicon/hibmc/Makefile index d5c40b8..810a37e 100644 --- a/drivers/gpu/drm/hisilicon/hibmc/Makefile +++ b/drivers/gpu/drm/hisilicon/hibmc/Makefile @@ -1,5 +1,5 @@ ccflags-y := -Iinclude/drm -hibmc-drm-y := hibmc_drm_drv.o hibmc_drm_power.o hibmc_ttm.o +hibmc-drm-y := hibmc_drm_drv.o hibmc_drm_fbdev.o hibmc_drm_power.o hibmc_ttm.o obj-$(CONFIG_DRM_HISI_HIBMC) +=hibmc-drm.o #obj-y += hibmc-drm.o diff --git a/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.c b/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.c index de3b3c3..ba31ead 100644 --- a/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.c +++ b/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.c @@ -73,9 +73,16 @@ static int hibmc_pm_suspend(struct device *dev) { struct pci_dev *pdev = to_pci_dev(dev); struct drm_device *drm_dev = pci_get_drvdata(pdev); + struct hibmc_drm_device *hidev = drm_dev->dev_private; drm_kms_helper_poll_disable(drm_dev); + if (hidev->fbdev.initialized) { + console_lock(); + drm_fb_helper_set_suspend(&hidev->fbdev.helper, 1); + console_unlock(); + } + return 0; } @@ -83,9 +90,16 @@ static int hibmc_pm_resume(struct device *dev) { struct pci_dev *pdev = to_pci_dev(dev); struct drm_device *drm_dev = pci_get_drvdata(pdev); + struct hibmc_drm_device *hidev = drm_dev->dev_private; drm_helper_resume_force_mode(drm_dev); + if (hidev->fbdev.initialized) { + console_lock(); + drm_fb_helper_set_suspend(&hidev->fbdev.helper, 0); + console_unlock(); + } + drm_kms_helper_poll_enable(drm_dev); return 0; @@ -187,6 +201,7 @@ static int hibmc_unload(struct drm_device *dev) { struct hibmc_drm_device *hidev = dev->dev_private; + hibmc_fbdev_fini(hidev); hibmc_hw_fini(hidev); dev->dev_private = NULL; return 0; @@ -208,7 +223,10 @@ static int hibmc_load(struct drm_device *dev, unsigned long flags) goto err; ret = hibmc_mm_init(hidev); + if (ret) + goto err; + ret = hibmc_fbdev_init(hidev); if (ret) goto err; diff --git a/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h b/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h index d5ef34b..c37ce2b 100644 --- a/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h +++ b/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h @@ -28,6 +28,19 @@ #include #include +struct hibmc_framebuffer { + struct drm_framebuffer fb; + struct drm_gem_object *obj; + bool is_fbdev_fb; +}; + +struct hibmc_fbdev { + struct drm_fb_helper helper; + struct hibmc_framebuffer fb; + int size; + bool initialized; +}; + struct hibmc_drm_device { /* hw */ void __iomem *mmio; @@ -46,9 +59,13 @@ struct hibmc_drm_device { bool initialized; } ttm; + /* fbdev */ + struct hibmc_fbdev fbdev; bool mm_inited; }; +#define to_hibmc_framebuffer(x) container_of(x, struct hibmc_framebuffer, fb) + struct hibmc_bo { struct ttm_buffer_object bo; struct ttm_placement placement; @@ -70,8 +87,15 @@ static inline struct hibmc_bo *gem_to_hibmc_bo(struct drm_gem_object *gem) #define DRM_FILE_PAGE_OFFSET (0x100000000ULL >> PAGE_SHIFT) +int hibmc_fbdev_init(struct hibmc_drm_device *hidev); +void hibmc_fbdev_fini(struct hibmc_drm_device *hidev); + int hibmc_gem_create(struct drm_device *dev, u32 size, bool iskernel, struct drm_gem_object **obj); +int hibmc_framebuffer_init(struct drm_device *dev, + struct hibmc_framebuffer *gfb, + const struct drm_mode_fb_cmd2 *mode_cmd, + struct drm_gem_object *obj); int hibmc_mm_init(struct hibmc_drm_device *hibmc); int hibmc_bo_pin(struct hibmc_bo *bo, u32 pl_flag, u64 *gpu_addr); diff --git a/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_fbdev.c b/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_fbdev.c new file mode 100644 index 0000000..3219511 --- /dev/null +++ b/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_fbdev.c @@ -0,0 +1,259 @@ +/* Hisilicon Hibmc SoC drm driver + * + * Based on the bochs drm driver. + * + * Copyright (c) 2016 Huawei Limited. + * + * Author: + * Rongrong Zou + * Rongrong Zou + * Jianhua Li + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + */ + +#include +#include +#include +#include + +#include "hibmc_drm_drv.h" + +/* ---------------------------------------------------------------------- */ + +static int hibmcfb_create_object( + struct hibmc_drm_device *hidev, + const struct drm_mode_fb_cmd2 *mode_cmd, + struct drm_gem_object **gobj_p) +{ + struct drm_gem_object *gobj; + struct drm_device *dev = hidev->dev; + u32 size; + int ret = 0; + + size = mode_cmd->pitches[0] * mode_cmd->height; + ret = hibmc_gem_create(dev, size, true, &gobj); + if (ret) + return ret; + + *gobj_p = gobj; + return ret; +} + +static struct fb_ops hibmc_drm_fb_ops = { + .owner = THIS_MODULE, + .fb_check_var = drm_fb_helper_check_var, + .fb_set_par = drm_fb_helper_set_par, + .fb_fillrect = drm_fb_helper_sys_fillrect, + .fb_copyarea = drm_fb_helper_sys_copyarea, + .fb_imageblit = drm_fb_helper_sys_imageblit, + .fb_pan_display = drm_fb_helper_pan_display, + .fb_blank = drm_fb_helper_blank, + .fb_setcmap = drm_fb_helper_setcmap, +}; + +static int hibmc_drm_fb_create(struct drm_fb_helper *helper, + struct drm_fb_helper_surface_size *sizes) +{ + struct hibmc_fbdev *gfbdev = + container_of(helper, struct hibmc_fbdev, helper); + struct hibmc_drm_device *hidev = + container_of(helper, struct hibmc_drm_device, fbdev.helper); + struct fb_info *info; + struct drm_framebuffer *fb; + struct drm_mode_fb_cmd2 mode_cmd; + struct drm_gem_object *gobj = NULL; + int ret = 0; + size_t size; + unsigned int bytes_per_pixel; + struct hibmc_bo *bo = NULL; + + DRM_DEBUG_DRIVER("surface width(%d), height(%d) and bpp(%d)\n", + sizes->surface_width, sizes->surface_height, + sizes->surface_bpp); + sizes->surface_depth = 32; + + bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8); + + mode_cmd.width = sizes->surface_width; + mode_cmd.height = sizes->surface_height; + mode_cmd.pitches[0] = mode_cmd.width * bytes_per_pixel; + mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, + sizes->surface_depth); + + size = roundup(mode_cmd.pitches[0] * mode_cmd.height, PAGE_SIZE); + + ret = hibmcfb_create_object(hidev, &mode_cmd, &gobj); + if (ret) { + DRM_ERROR("failed to create fbcon backing object %d\r\n", ret); + return -ENOMEM; + } + + bo = gem_to_hibmc_bo(gobj); + + ret = ttm_bo_reserve(&bo->bo, true, false, NULL); + if (ret) + return ret; + + ret = hibmc_bo_pin(bo, TTM_PL_FLAG_VRAM, NULL); + if (ret) { + DRM_ERROR("failed to pin fbcon\n"); + return ret; + } + + ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap); + + if (ret) { + DRM_ERROR("failed to kmap fbcon\n"); + ttm_bo_unreserve(&bo->bo); + return ret; + } + + ttm_bo_unreserve(&bo->bo); + + info = drm_fb_helper_alloc_fbi(helper); + if (IS_ERR(info)) + ret = PTR_ERR(info); + + info->par = gfbdev; + + ret = hibmc_framebuffer_init(hidev->dev, &hidev->fbdev.fb, + &mode_cmd, gobj); + if (ret) { + drm_fb_helper_release_fbi(helper); + return ret; + } + + hidev->fbdev.size = size; + fb = &gfbdev->fb.fb; + if (!fb) { + DRM_INFO("fb is NULL\n"); + return -EINVAL; + } + + gfbdev->helper.fb = fb; + + strcpy(info->fix.id, "hibmcdrmfb"); + + info->flags = FBINFO_DEFAULT; + info->fbops = &hibmc_drm_fb_ops; + + drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth); + drm_fb_helper_fill_var(info, &hidev->fbdev.helper, sizes->fb_width, + sizes->fb_height); + + info->screen_base = bo->kmap.virtual; + info->screen_size = size; + + info->fix.smem_start = bo->bo.mem.bus.offset + bo->bo.mem.bus.base; + info->fix.smem_len = size; + + return 0; +} + +static int hibmc_fbdev_destroy(struct drm_device *dev, + struct hibmc_fbdev *fbdev) +{ + struct hibmc_framebuffer *gfb = &fbdev->fb; + struct drm_fb_helper *fbh = &fbdev->helper; + + DRM_DEBUG_DRIVER("hibmc_fbdev_destroy\n"); + + drm_fb_helper_unregister_fbi(fbh); + drm_fb_helper_release_fbi(fbh); + + if (gfb->obj) { + drm_gem_object_unreference_unlocked(gfb->obj); + gfb->obj = NULL; + } + + drm_fb_helper_fini(fbh); + + drm_framebuffer_unregister_private(&gfb->fb); + drm_framebuffer_cleanup(&gfb->fb); + + return 0; +} + +static const struct drm_fb_helper_funcs hibmc_fbdev_helper_funcs = { + .fb_probe = hibmc_drm_fb_create, +}; + +int hibmc_fbdev_init(struct hibmc_drm_device *hidev) +{ + int ret; + struct fb_var_screeninfo *var; + struct fb_fix_screeninfo *fix; + + drm_fb_helper_prepare(hidev->dev, &hidev->fbdev.helper, + &hibmc_fbdev_helper_funcs); + + /* Now just one crtc and one channel */ + ret = drm_fb_helper_init(hidev->dev, + &hidev->fbdev.helper, 1, 1); + + if (ret) + return ret; + + ret = drm_fb_helper_single_add_all_connectors(&hidev->fbdev.helper); + if (ret) + goto fini; + + drm_helper_disable_unused_functions(hidev->dev); + + ret = drm_fb_helper_initial_config(&hidev->fbdev.helper, 16); + if (ret) + goto fini; + + hidev->fbdev.initialized = true; + + var = &hidev->fbdev.helper.fbdev->var; + fix = &hidev->fbdev.helper.fbdev->fix; + + DRM_DEBUG("Member of info->var is :\n" + "xres=%d\n" + "yres=%d\n" + "xres_virtual=%d\n" + "yres_virtual=%d\n" + "xoffset=%d\n" + "yoffset=%d\n" + "bits_per_pixel=%d\n" + "...\n", var->xres, var->yres, var->xres_virtual, + var->yres_virtual, var->xoffset, var->yoffset, + var->bits_per_pixel); + DRM_DEBUG("Member of info->fix is :\n" + "smem_start=%lx\n" + "smem_len=%d\n" + "type=%d\n" + "type_aux=%d\n" + "visual=%d\n" + "xpanstep=%d\n" + "ypanstep=%d\n" + "ywrapstep=%d\n" + "line_length=%d\n" + "accel=%d\n" + "capabilities=%d\n" + "...\n", fix->smem_start, fix->smem_len, fix->type, + fix->type_aux, fix->visual, fix->xpanstep, + fix->ypanstep, fix->ywrapstep, fix->line_length, + fix->accel, fix->capabilities); + + return 0; + +fini: + drm_fb_helper_fini(&hidev->fbdev.helper); + return ret; +} + +void hibmc_fbdev_fini(struct hibmc_drm_device *hidev) +{ + if (!hidev->fbdev.initialized) + return; + + hibmc_fbdev_destroy(hidev->dev, &hidev->fbdev); + hidev->fbdev.initialized = false; +} diff --git a/drivers/gpu/drm/hisilicon/hibmc/hibmc_ttm.c b/drivers/gpu/drm/hisilicon/hibmc/hibmc_ttm.c index 6789970..0f301ac 100644 --- a/drivers/gpu/drm/hisilicon/hibmc/hibmc_ttm.c +++ b/drivers/gpu/drm/hisilicon/hibmc/hibmc_ttm.c @@ -499,3 +499,71 @@ int hibmc_dumb_mmap_offset(struct drm_file *file, struct drm_device *dev, drm_gem_object_unreference_unlocked(obj); return 0; } + +/* ---------------------------------------------------------------------- */ + +static void hibmc_user_framebuffer_destroy(struct drm_framebuffer *fb) +{ + struct hibmc_framebuffer *hibmc_fb = to_hibmc_framebuffer(fb); + + if (hibmc_fb->obj) + drm_gem_object_unreference_unlocked(hibmc_fb->obj); + drm_framebuffer_cleanup(fb); + kfree(fb); +} + +static const struct drm_framebuffer_funcs hibmc_fb_funcs = { + .destroy = hibmc_user_framebuffer_destroy, +}; + +int hibmc_framebuffer_init(struct drm_device *dev, + struct hibmc_framebuffer *gfb, + const struct drm_mode_fb_cmd2 *mode_cmd, + struct drm_gem_object *obj) +{ + int ret; + + drm_helper_mode_fill_fb_struct(&gfb->fb, mode_cmd); + gfb->obj = obj; + ret = drm_framebuffer_init(dev, &gfb->fb, &hibmc_fb_funcs); + if (ret) { + DRM_ERROR("drm_framebuffer_init failed: %d\n", ret); + return ret; + } + return 0; +} + +static struct drm_framebuffer * +hibmc_user_framebuffer_create(struct drm_device *dev, + struct drm_file *filp, + const struct drm_mode_fb_cmd2 *mode_cmd) +{ + struct drm_gem_object *obj; + struct hibmc_framebuffer *hibmc_fb; + int ret; + + DRM_DEBUG_DRIVER("%dx%d, format %c%c%c%c\n", + mode_cmd->width, mode_cmd->height, + (mode_cmd->pixel_format) & 0xff, + (mode_cmd->pixel_format >> 8) & 0xff, + (mode_cmd->pixel_format >> 16) & 0xff, + (mode_cmd->pixel_format >> 24) & 0xff); + + obj = drm_gem_object_lookup(filp, mode_cmd->handles[0]); + if (!obj) + return ERR_PTR(-ENOENT); + + hibmc_fb = kzalloc(sizeof(*hibmc_fb), GFP_KERNEL); + if (!hibmc_fb) { + drm_gem_object_unreference_unlocked(obj); + return ERR_PTR(-ENOMEM); + } + + ret = hibmc_framebuffer_init(dev, hibmc_fb, mode_cmd, obj); + if (ret) { + drm_gem_object_unreference_unlocked(obj); + kfree(hibmc_fb); + return ERR_PTR(ret); + } + return &hibmc_fb->fb; +}