Initial commit: Linux kernel 4.19 for Unplugged OS

This commit is contained in:
ziv committed 2025-10-06 13:52:42 +00:00
commit d30235b04c
80519 files changed
+35800681

No files matched your search

+8
View File
@@ -0,0 +1,8 @@
config MTK_SYNC
bool "Simplified software synchronization objects"
default n
help
A simplified SW_SYNC that provides essential APIs for
other kernel modules.
default value is "n"
If you want to use mtk display driver, please set y.
+19
View File
@@ -0,0 +1,19 @@
#
# Copyright (C) 2015 MediaTek Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
#
# Makefile for the MTK sync driver
#
# MTK sync driver interface
obj-$(CONFIG_MTK_SYNC) += mtk_sync.o
+333
View File
@@ -0,0 +1,333 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/debugfs.h>
#include <linux/export.h>
#include <linux/seq_file.h>
#include <linux/file.h>
#include <linux/kthread.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/sync_file.h>
#include "mtk_sync.h"
/* ---------------------------------------------------------------- */
static inline struct sync_timeline *dma_fence_parent(struct dma_fence *fence)
{
return container_of(fence->lock, struct sync_timeline, lock);
}
static LIST_HEAD(sync_timeline_list_head);
static DEFINE_SPINLOCK(sync_timeline_list_lock);
static const struct dma_fence_ops timeline_fence_ops;
static inline struct sync_pt *fence_to_sync_pt(struct dma_fence *fence)
{
if (fence->ops != &timeline_fence_ops)
return NULL;
return container_of(fence, struct sync_pt, base);
}
static void mt_sync_timeline_debug_add(struct sync_timeline *obj)
{
unsigned long flags;
spin_lock_irqsave(&sync_timeline_list_lock, flags);
list_add_tail(&obj->sync_timeline_list, &sync_timeline_list_head);
spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
}
static void mt_sync_timeline_debug_remove(struct sync_timeline *obj)
{
unsigned long flags;
spin_lock_irqsave(&sync_timeline_list_lock, flags);
list_del(&obj->sync_timeline_list);
spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
}
static void sync_timeline_free(struct kref *kref)
{
struct sync_timeline *obj =
container_of(kref, struct sync_timeline, kref);
mt_sync_timeline_debug_remove(obj);
kfree(obj);
}
static void sync_timeline_get(struct sync_timeline *obj)
{
kref_get(&obj->kref);
}
static void sync_timeline_put(struct sync_timeline *obj)
{
kref_put(&obj->kref, sync_timeline_free);
}
/**
* sync_pt_create() - creates a sync pt
* @parent: fence's parent sync_timeline
* @size: size to allocate for this pt
* @inc: value of the fence
*
* Creates a new sync_pt as a child of @parent. @size bytes will be
* allocated allowing for implementation specific data to be kept after
* the generic sync_timeline struct. Returns the sync_pt object or
* NULL in case of error.
*/
static struct sync_pt *sync_pt_create(struct sync_timeline *obj,
unsigned int value)
{
struct sync_pt *pt;
pt = kzalloc(sizeof(*pt), GFP_KERNEL);
if (!pt)
return NULL;
sync_timeline_get(obj);
dma_fence_init(&pt->base, &timeline_fence_ops, &obj->lock,
obj->context, value);
INIT_LIST_HEAD(&pt->link);
spin_lock_irq(&obj->lock);
if (!dma_fence_is_signaled_locked(&pt->base)) {
struct rb_node **p = &obj->pt_tree.rb_node;
struct rb_node *parent = NULL;
while (*p) {
struct sync_pt *other;
int cmp;
parent = *p;
other = rb_entry(parent, typeof(*pt), node);
cmp = value - other->base.seqno;
if (cmp > 0) {
p = &parent->rb_right;
} else if (cmp < 0) {
p = &parent->rb_left;
} else {
if (dma_fence_get_rcu(&other->base)) {
dma_fence_put(&pt->base);
pt = other;
goto unlock;
}
p = &parent->rb_left;
}
}
rb_link_node(&pt->node, parent, p);
rb_insert_color(&pt->node, &obj->pt_tree);
parent = rb_next(&pt->node);
list_add_tail(&pt->link,
parent ? &rb_entry(parent, typeof(*pt), node)->link :
&obj->pt_list);
}
unlock:
spin_unlock_irq(&obj->lock);
return pt;
}
static const char *timeline_fence_get_driver_name(struct dma_fence *fence)
{
return "mtk_sync";
}
static const char *timeline_fence_get_timeline_name(struct dma_fence *fence)
{
struct sync_timeline *parent = dma_fence_parent(fence);
return parent->name;
}
static void timeline_fence_release(struct dma_fence *fence)
{
struct sync_pt *pt = fence_to_sync_pt(fence);
struct sync_timeline *parent = dma_fence_parent(fence);
unsigned long flags;
if (!pt) {
pr_info("%s:pt is null\n", __func__);
return;
}
if (!list_empty(&pt->link)) {
spin_lock_irqsave(fence->lock, flags);
if (!list_empty(&pt->link)) {
list_del(&pt->link);
rb_erase(&pt->node, &parent->pt_tree);
}
spin_unlock_irqrestore(fence->lock, flags);
}
sync_timeline_put(parent);
dma_fence_free(fence);
}
static bool timeline_fence_signaled(struct dma_fence *fence)
{
struct sync_timeline *parent = dma_fence_parent(fence);
return !__dma_fence_is_later(fence->seqno, parent->value);
}
static bool timeline_fence_enable_signaling(struct dma_fence *fence)
{
return true;
}
static void timeline_fence_value_str(struct dma_fence *fence,
char *str, int size)
{
int n;
n = snprintf(str, size, "%d", fence->seqno);
if (n < 0 || n >= size)
pr_info("%s:overflow\n", __func__);
}
static void timeline_fence_timeline_value_str(struct dma_fence *fence,
char *str, int size)
{
struct sync_timeline *parent = dma_fence_parent(fence);
int n;
n = snprintf(str, size, "%d", parent->value);
if (n < 0 || n >= size)
pr_info("%s:overflow\n", __func__);
}
static const struct dma_fence_ops timeline_fence_ops = {
.get_driver_name = timeline_fence_get_driver_name,
.get_timeline_name = timeline_fence_get_timeline_name,
.enable_signaling = timeline_fence_enable_signaling,
.signaled = timeline_fence_signaled,
.wait = dma_fence_default_wait,
.release = timeline_fence_release,
.fence_value_str = timeline_fence_value_str,
.timeline_value_str = timeline_fence_timeline_value_str,
};
/* ---------------------------------------------------------------- */
/**
* sync_timeline_signal() - signal a status change on a sync_timeline
* @obj: sync_timeline to signal
* @inc: num to increment on timeline->value
*
* A sync implementation should call this any time one of it's fences
* has signaled or has an error condition.
*/
static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
{
struct sync_pt *pt, *next;
spin_lock_irq(&obj->lock);
obj->value += inc;
list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
if (!timeline_fence_signaled(&pt->base))
break;
list_del_init(&pt->link);
rb_erase(&pt->node, &obj->pt_tree);
/*
* A signal callback may release the last reference to this
* fence, causing it to be freed. That operation has to be
* last to avoid a use after free inside this loop, and must
* be after we remove the fence from the timeline in order to
* prevent deadlocking on timeline->lock inside
* timeline_fence_release().
*/
dma_fence_signal_locked(&pt->base);
}
spin_unlock_irq(&obj->lock);
}
/**
* sync_timeline_create() - creates a sync object
* @name: sync_timeline name
*
* Creates a new sync_timeline. Returns the sync_timeline object or NULL in
* case of error.
*/
static struct sync_timeline *sync_timeline_create(const char *name)
{
struct sync_timeline *obj;
obj = kzalloc(sizeof(*obj), GFP_KERNEL);
if (!obj)
return NULL;
kref_init(&obj->kref);
obj->context = dma_fence_context_alloc(1);
strlcpy(obj->name, name, sizeof(obj->name));
obj->pt_tree = RB_ROOT;
INIT_LIST_HEAD(&obj->pt_list);
spin_lock_init(&obj->lock);
mt_sync_timeline_debug_add(obj);
return obj;
}
struct sync_timeline *timeline_create(const char *name)
{
return sync_timeline_create(name);
}
void timeline_destroy(struct sync_timeline *obj)
{
sync_timeline_put(obj);
}
void timeline_inc(struct sync_timeline *obj, u32 value)
{
sync_timeline_signal(obj, value);
}
int fence_create(struct sync_timeline *obj,
struct mtk_sync_create_fence_data *data)
{
int fd = get_unused_fd_flags(O_CLOEXEC);
int err;
struct sync_pt *pt;
struct sync_file *sync_file;
if (fd < 0)
return fd;
pt = sync_pt_create(obj, data->value);
if (!pt) {
err = -ENOMEM;
goto err;
}
sync_file = sync_file_create(&pt->base);
dma_fence_put(&pt->base);
if (!sync_file) {
err = -ENOMEM;
goto err;
}
data->fence = fd;
fd_install(fd, sync_file->file);
return 0;
err:
put_unused_fd(fd);
return err;
}
+122
View File
@@ -0,0 +1,122 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
* Author: Joey Pan <joey.pan@mediatek.com>
*/
#ifndef _MTK_SYNC_H_
#define _MTK_SYNC_H_
/*
* TIMEOUT_NEVER may be passed to the wait method to indicate that it
* should wait indefinitely for the fence to signal.
*/
#define TIMEOUT_NEVER -1
/* ---------------------------------------------------------------- */
#ifdef __KERNEL__
#include <linux/version.h>
#include <linux/dma-fence.h>
//#include <../drivers/staging/android/sw_sync.h>
/**
* struct sync_timeline - sync object
* @kref: reference count on fence.
* @name: name of the sync_timeline. Useful for debugging
* @lock: lock protecting @pt_list and @value
* @pt_tree: rbtree of active (unsignaled/errored) sync_pts
* @pt_list: list of active (unsignaled/errored) sync_pts
* @sync_timeline_list: membership in global sync_timeline_list
*/
struct sync_timeline {
struct kref kref;
char name[32];
/* protected by lock */
u64 context;
int value;
struct rb_root pt_tree;
struct list_head pt_list;
spinlock_t lock;
struct list_head sync_timeline_list;
};
/**
* struct sync_pt - sync_pt object
* @base: base fence object
* @link: link on the sync timeline's list
* @node: node in the sync timeline's tree
*/
struct sync_pt {
struct dma_fence base;
struct list_head link;
struct rb_node node;
};
/*
* sync_timeline, sync_fence data structure
*/
struct mtk_sync_create_fence_data {
__u32 value;
char name[32];
__s32 fence; /* fd of new fence */
};
/*
* sync_timeline, sync_fence API
*/
//extern struct sync_timeline *sync_timeline_create(const char *name); //cc
/**
* timeline_create() - creates a sync object
* @name: sync_timeline name
*
* The timeline_create() function creates a sync object named @name,
* which represents a 32-bit monotonically increasing counter.
*/
struct sync_timeline *timeline_create(const char *name);
/**
* timeline_destroy() - releases a sync object
* @obj: sync_timeline obj
*
* The timeline_destroy() function releases a sync object.
* The remaining active points would be put into signaled list,
* and their statuses are set to VENOENT.
*/
void timeline_destroy(struct sync_timeline *obj);
/**
* timeline_inc() - increases timeline
* @obj: sync_timeline obj
* @value: the increment to a sync object
*
* The timeline_inc() function increase the counter of @obj by @value
* Each sync point contains a value. A sync point on a parent timeline transits
* from active to signaled status when the counter of a timeline reaches
* to that of a sync point.
*/
void timeline_inc(struct sync_timeline *obj, u32 value);
/**
* fence_create() - create a fence
* @obj: sync_timeline obj
* @data: fence struct with its name and the number a sync point bears
*
* The fence_create() function creates a new sync point with @data->value,
* and assign the sync point to a newly created fence named @data->name.
* A file descriptor binded with the fence is stored in @data->fence.
*/
int fence_create(struct sync_timeline *obj,
struct mtk_sync_create_fence_data *data);
#endif /* __KERNEL __ */
#endif /* _MTK_SYNC_H_ */