Commit caebb6c5 authored by Lyude's avatar Lyude

Add basic code for opening up mali kernel device

Nothing fancy yet, just report the name of the kernel driver we found to
make sure things work.
parent 8c018562
cmake_minimum_required(VERSION 3.5)
include(CheckCSourceCompiles)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
message(STATUS "Checking if compiler supports __attribute__((packed))")
check_c_source_compiles("struct s { int a; } __attribute__((packed)); main;"
ATTRIBUTE_PACKED_SUPPORT)
if (NOT ATTRIBUTE_PACKED_SUPPORT)
message(FATAL_ERROR "Compiler must support __attribute__((packed))")
endif()
include_directories("include")
add_subdirectory(src)
# vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab :
/*
* © Copyright 2017 The BiOpenly Community
*
* 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 license.
*
* 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.
*
*/
/**
* Definitions for all of the ioctls for the original open source bifrost GPU
* kernel driver, written by ARM.
*/
#ifndef __MALI_IOCTL_H__
#define __MALI_IOCTL_H__
#include <inttypes.h>
#include <panloader-util.h>
enum mali_func_id {
MALI_FUNC_GET_VERSION = 0,
};
/**
* Since these structs are passed to and from the kernel we need to make sure
* that we get the size of each struct to match exactly what the kernel is
* expecting. So, when editing this file make sure to add static asserts that
* check each struct's size against the arg length you see in strace.
*/
/**
* Header used by all ioctls
*/
union mali_ioctl_func_header {
/* [in] The ID of the UK function being called */
enum mali_func_id id :32;
/* [out] The return value of the UK function that was called */
uint32_t rc :32;
uint64_t :64;
} __attribute__((packed));
ASSERT_SIZEOF_TYPE(union mali_ioctl_func_header, 8);
struct mali_ioctl_get_version {
union mali_ioctl_func_header header;
uint16_t major; /* [out] */
uint16_t minor; /* [out] */
uint32_t :32;
} __attribute__((packed));
ASSERT_SIZEOF_TYPE(struct mali_ioctl_get_version, 16);
#endif /* __MALI_IOCTL_H__ */
/*
* © Copyright 2017 The BiOpenly Community
*
* 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.
*
*/
/**
* Miscellanious utilities
*/
#ifndef __PANLOADER_UTIL_H__
#define __PANLOADER_UTIL_H__
#define ASSERT_SIZEOF_TYPE(type__, size__) \
_Static_assert(sizeof(type__) == size__, \
#type__ " does not match expected size " #size__)
#endif /* __PANLOADER_UTIL_H__ */
project(panloader)
set(SRCS main.c)
set(SRCS main.c pandev.c)
add_executable(panloader ${SRCS})
/*
* © Copyright 2017 The BiOpenly Community
*
* 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.
*
*/
#include <stdio.h>
#include "pandev.h"
int main(int argc, char **argv)
{
printf("Hello world! I am destined for greater things...\n");
int fd = pandev_open();
if (fd < 0) {
printf("pandev_open() failed with rc%d\n", -fd);
return -fd;
}
printf("More to come soon :)\n");
return 0;
}
/*
* © Copyright 2017 The BiOpenly Community
*
* 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.
*
*/
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/ioctl.h>
#include <mali-ioctl.h>
static int
pandev_get_driver_version(int fd, unsigned *major, unsigned *minor)
{
struct mali_ioctl_get_version args = {0};
int rc;
/* So far this seems to be the only ioctl that uses 0x80 for dir */
rc = ioctl(fd, _IOWR(0x80, MALI_FUNC_GET_VERSION, args), &args);
if (rc)
return rc;
*major = args.major;
*minor = args.minor;
return 0;
}
/**
* Open the device file for communicating with the mali kernelspace driver,
* and make sure it's a version of the kernel driver we're familiar with.
*
* Returns: fd on success, -1 on failure
*/
int
pandev_open()
{
int fd = open("/dev/mali0", O_RDWR | O_NONBLOCK | O_CLOEXEC),
rc;
unsigned major, minor;
if (fd < 0)
return fd;
rc = pandev_get_driver_version(fd, &major, &minor);
if (rc)
return rc;
printf("Found kernel driver version v%d.%d at /dev/mali0\n",
major, minor);
/* We only support using v10 since this is the kernel driver version
* HiKey 960's come with pre-built on Android. Mali changes things a
* lot, so it's not worth the effort to support anything else
*/
if (major != 10) {
fprintf(stderr,
"Warning! This has only been tested with v10 of the "
"Bifrost kernel driver. There is no guarantee anything "
"will work with this version.\n");
}
return fd;
}
/*
* © Copyright 2017 The BiOpenly Community
*
* 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.
*
*/
#ifndef __PANDEV_H__
#define __PANDEV_H__
int pandev_open();
#endif /* __PANDEV_H__ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment