为 ARM64 上的 mali450 GPU 内核驱动程序移植 pre-4.15 计时器代码

Porting pre-4.15 timer code for mali450 GPU kernel driver on ARM64

我有一个基于 Amlogic S905X ARM64 SoC 的旧电视盒,一个 Tanix TX5,最初 运行 Android 在上面。我不喜欢被锁定在我的硬件潜力之外,所以我密切关注 Armbian 项目的进展,我最近安装了 5.67 版本,它基于 Ubuntu Bionic,运行在 Linux 4.19.6.

一切正常,以太网、wifi、蓝牙等都有可用的驱动程序,但遗憾的是没有包含内核驱动程序来支持集成的 mali450 gpu。 hdmi 输出确实有效,但 Xorg 在 fbdev 上是 运行,屏幕更新非常慢,无法全屏观看视频。我不太确定这个故事是什么,但显然 amlogic 保留了他们当前版本的驱动程序。但是,通过访问他们的 FTP 站点,我找到了内核驱动程序 ( http://openlinux.amlogic.com:8000/download/ARM/gpu/gpu-2016-08-18-fe6d7b1d1b.tar.gz ) 的旧源代码树。它已经过时了,但我在将它移植到当前内核方面取得了一些进展,到目前为止,大部分修复都很简单。我现在可以编译大约一半的源文件了。

我应该澄清一下,我不是 linux 开发人员,我什至不是 C 开发人员...我涉猎,我知道的只是危险,而且我很有动力.虽然我的日常交易是 C#,所以我或多或少知道我在做什么。

我目前遇到的问题是 mali/linux/mali_osk_timers.c。这是原始代码:

/*
 * Copyright (C) 2010-2014, 2016 ARM Limited. All rights reserved.
 * 
 * This program is free software and is provided to you under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation, and any use by you of this program is subject to the terms of such GNU licence.
 * 
 * A copy of the licence is included with the program, and can also be obtained from Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

/**
 * @file mali_osk_timers.c
 * Implementation of the OS abstraction layer for the kernel device driver
 */

#include <linux/timer.h>
#include <linux/slab.h>
#include "mali_osk.h"
#include "mali_kernel_common.h"

struct _mali_osk_timer_t_struct {
    struct timer_list timer;
};

typedef void (*timer_timeout_function_t)(unsigned long);

_mali_osk_timer_t *_mali_osk_timer_init(void)
{
    _mali_osk_timer_t *t = (_mali_osk_timer_t *)kmalloc(sizeof(_mali_osk_timer_t), GFP_KERNEL);
    if (NULL != t) init_timer(&t->timer);
    return t;
}

void _mali_osk_timer_add(_mali_osk_timer_t *tim, unsigned long ticks_to_expire)
{
    MALI_DEBUG_ASSERT_POINTER(tim);
    tim->timer.expires = jiffies + ticks_to_expire;
    add_timer(&(tim->timer));
}

void _mali_osk_timer_mod(_mali_osk_timer_t *tim, unsigned long ticks_to_expire)
{
    MALI_DEBUG_ASSERT_POINTER(tim);
    mod_timer(&(tim->timer), jiffies + ticks_to_expire);
}

void _mali_osk_timer_del(_mali_osk_timer_t *tim)
{
    MALI_DEBUG_ASSERT_POINTER(tim);
    del_timer_sync(&(tim->timer));
}

void _mali_osk_timer_del_async(_mali_osk_timer_t *tim)
{
    MALI_DEBUG_ASSERT_POINTER(tim);
    del_timer(&(tim->timer));
}

mali_bool _mali_osk_timer_pending(_mali_osk_timer_t *tim)
{
    MALI_DEBUG_ASSERT_POINTER(tim);
    return 1 == timer_pending(&(tim->timer));
}

void _mali_osk_timer_setcallback(_mali_osk_timer_t *tim, _mali_osk_timer_callback_t callback, void *data)
{
    MALI_DEBUG_ASSERT_POINTER(tim);
    tim->timer.data = (unsigned long)data;
    tim->timer.function = (timer_timeout_function_t)callback;
}

void _mali_osk_timer_term(_mali_osk_timer_t *tim)
{
    MALI_DEBUG_ASSERT_POINTER(tim);
    kfree(tim);
}

这里的主要问题是这段代码通过一个很久以前就被弃用的接口使用定时器,并在 4.15 中从内核中完全删除。我能够查找如何移植这段代码,并管理几乎整个文件,但最后我对 C 语法和指针使用规则不够熟悉,无法弄清楚如何去做。主要问题是 _mali_osk_timer_setcallback() 函数。我不太确定如何在修改它的同时保持相同的函数签名。

编辑

这是当前代码和编译器的当前输出:

/*
 * Copyright (C) 2010-2014, 2016 ARM Limited. All rights reserved.
 * 
 * This program is free software and is provided to you under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation, and any use by you of this program is subject to the terms of such GNU licence.
 * 
 * A copy of the licence is included with the program, and can also be obtained from Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

/**
 * @file mali_osk_timers.c
 * Implementation of the OS abstraction layer for the kernel device driver
 */

#include <linux/timer.h>
#include <linux/slab.h>
#include "mali_osk.h"
#include "mali_kernel_common.h"

#define MALI_TIMER_FLAGS 0

struct _mali_osk_timer_t_struct {
    struct timer_list timer;
    void (*ticked)(unsigned long data);
};

static void tick_trampoline(struct timer_list *t) {
     typedef struct _mali_osk_timer_t_struct Tldr;
     Tldr *m = container_of(t, Tldr, timer);
     m->ticked(t->data);
}

typedef void (*timer_timeout_function_t)(unsigned long);

_mali_osk_timer_t *_mali_osk_timer_init(void)
{
    _mali_osk_timer_t *t = (_mali_osk_timer_t *)kmalloc(sizeof(_mali_osk_timer_t), GFP_KERNEL);
    if (NULL != t) timer_setup(&(t->timer), tick_trampoline, MALI_TIMER_FLAGS);
    return t;
}

void _mali_osk_timer_add(_mali_osk_timer_t *tim, unsigned long ticks_to_expire)
{
    MALI_DEBUG_ASSERT_POINTER(tim);
    tim->timer.expires = jiffies + ticks_to_expire;
    add_timer(&(tim->timer));
}

void _mali_osk_timer_mod(_mali_osk_timer_t *tim, unsigned long ticks_to_expire)
{
    MALI_DEBUG_ASSERT_POINTER(tim);
    mod_timer(&(tim->timer), jiffies + ticks_to_expire);
}

void _mali_osk_timer_del(_mali_osk_timer_t *tim)
{
    MALI_DEBUG_ASSERT_POINTER(tim);
    del_timer_sync(&(tim->timer));
}

void _mali_osk_timer_del_async(_mali_osk_timer_t *tim)
{
    MALI_DEBUG_ASSERT_POINTER(tim);
    del_timer(&(tim->timer));
}

mali_bool _mali_osk_timer_pending(_mali_osk_timer_t *tim)
{
    MALI_DEBUG_ASSERT_POINTER(tim);
    return 1 == timer_pending(&(tim->timer));
}

void _mali_osk_timer_setcallback(_mali_osk_timer_t *tim, _mali_osk_timer_callback_t callback, void *data)
{
    MALI_DEBUG_ASSERT_POINTER(tim);
    &(tim->timer).data = (unsigned long)data;
    tim->ticked = (timer_timeout_function_t)callback; /* Note no cast */
    timer_setup(&(tim->timer), tick_trampoline, MALI_TIMER_FLAGS);
}

void _mali_osk_timer_term(_mali_osk_timer_t *tim)
{
    MALI_DEBUG_ASSERT_POINTER(tim);
    kfree(tim);
}

_

hugo@tx5:/media/hugo/7d79c22a-4ac8-4fcb-ae1c-3310a851a73d/gpu-2016-08-18-fe6d7b1d1b/mali$ sudo KDIR=../../kernel/Amlogic_s905-kernel USING_UMP=0 BUILD=release make
make ARCH=arm64 -C ../../kernel/Amlogic_s905-kernel M=/media/hugo/7d79c22a-4ac8-4fcb-ae1c-3310a851a73d/gpu-2016-08-18-fe6d7b1d1b/mali modules
make[1]: Entering directory '/media/hugo/7d79c22a-4ac8-4fcb-ae1c-3310a851a73d/kernel/Amlogic_s905-kernel'

  WARNING: Symbol version dump ./Module.symvers
           is missing; modules will have no dependencies and modversions.

  CC [M]  /media/hugo/7d79c22a-4ac8-4fcb-ae1c-3310a851a73d/gpu-2016-08-18-fe6d7b1d1b/mali/linux/mali_osk_timers.o
/media/hugo/7d79c22a-4ac8-4fcb-ae1c-3310a851a73d/gpu-2016-08-18-fe6d7b1d1b/mali/linux/mali_osk_timers.c: In function ‘tick_trampoline’:
/media/hugo/7d79c22a-4ac8-4fcb-ae1c-3310a851a73d/gpu-2016-08-18-fe6d7b1d1b/mali/linux/mali_osk_timers.c:31:17: error: ‘struct timer_list’ has no member named ‘data’
      m->ticked(t->data);
                 ^~
/media/hugo/7d79c22a-4ac8-4fcb-ae1c-3310a851a73d/gpu-2016-08-18-fe6d7b1d1b/mali/linux/mali_osk_timers.c: In function ‘_mali_osk_timer_setcallback’:
/media/hugo/7d79c22a-4ac8-4fcb-ae1c-3310a851a73d/gpu-2016-08-18-fe6d7b1d1b/mali/linux/mali_osk_timers.c:77:18: error: ‘struct timer_list’ has no member named ‘data’
     &(tim->timer).data = (unsigned long)data;
                  ^
/media/hugo/7d79c22a-4ac8-4fcb-ae1c-3310a851a73d/gpu-2016-08-18-fe6d7b1d1b/mali/linux/mali_osk_timers.c: At top level:
cc1: warning: unrecognized command line option ‘-Wno-data-time’
scripts/Makefile.build:305: recipe for target '/media/hugo/7d79c22a-4ac8-4fcb-ae1c-3310a851a73d/gpu-2016-08-18-fe6d7b1d1b/mali/linux/mali_osk_timers.o' failed
make[2]: *** [/media/hugo/7d79c22a-4ac8-4fcb-ae1c-3310a851a73d/gpu-2016-08-18-fe6d7b1d1b/mali/linux/mali_osk_timers.o] Error 1
Makefile:1517: recipe for target '_module_/media/hugo/7d79c22a-4ac8-4fcb-ae1c-3310a851a73d/gpu-2016-08-18-fe6d7b1d1b/mali' failed
make[1]: *** [_module_/media/hugo/7d79c22a-4ac8-4fcb-ae1c-3310a851a73d/gpu-2016-08-18-fe6d7b1d1b/mali] Error 2
make[1]: Leaving directory '/media/hugo/7d79c22a-4ac8-4fcb-ae1c-3310a851a73d/kernel/Amlogic_s905-kernel'
Makefile:192: recipe for target 'all' failed
make: *** [all] Error 2

new 计时器接口,gasp,将上下文值留在计时器结构内,并传递指向该结构的指针。这个想法是 数据的使用将消失,因为常见的习惯用法是数据是指向的指针 包含 timer_list 结构的结构。

由于您可能希望尽量减少对 mali 的更改,此处概述的更改应该有所帮助;它只是插入了一点协议转换功能,使它看起来像旧方法。

struct _mali_osk_timer_t_struct {
    struct timer_list timer;
    unsigned long data;
    void (*ticked)(unsigned long data);  /* Add this */
};

#define MALI_TIMER_FLAGS 0  /* NB: choose the right bits for this! */

/* This function converts between the “new” and “old” notifications */
static void tick_trampoline(struct timer *t) {
     typedef struct _mali_osk_timer_t_struct Tldr;
     Tldr *m = container_of(t, Tldr, timer);
     m->ticked(m->data);
}
/* change this one: */
void _mali_osk_timer_setcallback(_mali_osk_timer_t *tim, _mali_osk_timer_callback_t callback, void *data)
{
    MALI_DEBUG_ASSERT_POINTER(tim);
    tim->data = (unsigned long)data;
    tim->ticked = callback; /* Note no cast */
    timer_setup(&tim->ticked, tick_trampoline, MALI_TIMER_FLAGS);
}

“不强制转换”是让类型系统完成其工作的请求。