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

397 Views Asked by At

I have an old TV box based off the Amlogic S905X ARM64 SoC, a Tanix TX5, which originally ran Android on it. I don't like being locked out of the potential of my hardware, so I followed closely the progress of the Armbian project, and I recently have installed version 5.67, which is based off Ubuntu Bionic, running on Linux 4.19.6.

Everything works great, there are working drivers for ethernet, wifi, blutooth, etc, but there is unfortunately no kernel driver included to support the integrated mali450 gpu. The hdmi output does work, but Xorg is running on fbdev, and screen updating is painfully slow, videos are unwatchable in full screen. I'm not too sure what the story is but apparently amlogic keeps their current version of the driver to themselves. However by going on their FTP site, I found an old source tree for the kernel driver ( http://openlinux.amlogic.com:8000/download/ARM/gpu/gpu-2016-08-18-fe6d7b1d1b.tar.gz ). It is out of date, but I have made some headway into porting it to the current kernel, with mostly easy fixes up to now. I can now compile about half of the source files just fine.

I should clarify I'm not a linux developer by trade, I'm not even a C developer... I dabble, I know just enough to be dangerous, and I'm highly motivated. My daily trade is C# though, so I do know more or less what I'm doing.

The current issue I am having is with mali/linux/mali_osk_timers.c . This is the original code:

/*
 * 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);
}

The main problem here is that this code uses the timers via an interface that was deprecated long ago, and removed entirely from the kernel in 4.15. I was able to look up how to port this code, and managed almost the entire file, but in the end I'm not familiar enough with C syntax and pointer usage rules to figure out how to do it. The main problematic is the _mali_osk_timer_setcallback() function. I'm not really sure how to go about modifying it while also keeping the same function signature.

Edit

Here is the current code, and the current output from the compiler:

/*
 * 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
1

There are 1 best solutions below

3
On BEST ANSWER

The new timer interface, gasp, leaves the context value inside the timer structure, and passes a pointer to that structure instead. The idea is that use of data will go away, as the common idiom was that data was a pointer to a structure that contained the timer_list structure.

Since you probably want to minimize the changes to mali, the changes outlined here should help; it just inserts a little protocol conversion function to make it look like the old way.

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);
}

The “No cast” is a plea to let the type system do its job.