From patchwork Tue Jun 5 23:45:47 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Erik Gilling X-Patchwork-Id: 9126 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id BF05323E5A for ; Tue, 5 Jun 2012 23:46:21 +0000 (UTC) Received: from mail-yx0-f180.google.com (mail-yx0-f180.google.com [209.85.213.180]) by fiordland.canonical.com (Postfix) with ESMTP id 78AB5A18602 for ; Tue, 5 Jun 2012 23:46:21 +0000 (UTC) Received: by mail-yx0-f180.google.com with SMTP id q6so5088568yen.11 for ; Tue, 05 Jun 2012 16:46:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-forwarded-to:x-forwarded-for:delivered-to:received-spf:from:to :date:message-id:x-mailer:in-reply-to:references:subject:x-beenthere :x-mailman-version:precedence:list-id:list-unsubscribe:list-archive :list-post:list-help:list-subscribe:mime-version:content-type :content-transfer-encoding:sender:errors-to:x-gm-message-state; bh=FNsRxVpxm3ha62vLqKZaL+E8aQm7J5xycrRjGYNdK64=; b=a8hlku0XZaiompJWTcFh9qdj0KQEmECWY9vtjfRBOIsox11Yt0BLdEoY0dV007hkOx iEMIkSZXxaqlmqg0WI61qrbzz8U/8rT+vUlYyPaLUBaJwDxK4VaSkW0ZhvHSzJHLypGu EsJVtaHer8kscGd4SPbIBhuZ8r5V8Y3hBaHp0AlXZjUH/bnlSp7ZSfY5lzrSpK2z7CIu jC09Logh6Be0qOxvbF5ka1BOxPE0JlkDWH6cGc7Ua7a0ZwNTACurSFvJ0cGkiVHR3+tv T/S6yqYtSUuraQYKi5vnMeECp24OzxVwFhCDuyvRitNh27tsRYV1elYaaA68oZp8cXnr jceQ== Received: by 10.50.160.198 with SMTP id xm6mr4937583igb.0.1338939980922; Tue, 05 Jun 2012 16:46:20 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.231.24.148 with SMTP id v20csp52393ibb; Tue, 5 Jun 2012 16:46:19 -0700 (PDT) Received: by 10.236.190.4 with SMTP id d4mr13827963yhn.73.1338939978784; Tue, 05 Jun 2012 16:46:18 -0700 (PDT) Received: from mombin.canonical.com (mombin.canonical.com. [91.189.95.16]) by mx.google.com with ESMTP id y5si2907816yha.142.2012.06.05.16.46.17; Tue, 05 Jun 2012 16:46:18 -0700 (PDT) Received-SPF: neutral (google.com: 91.189.95.16 is neither permitted nor denied by best guess record for domain of linaro-mm-sig-bounces@lists.linaro.org) client-ip=91.189.95.16; Authentication-Results: mx.google.com; spf=neutral (google.com: 91.189.95.16 is neither permitted nor denied by best guess record for domain of linaro-mm-sig-bounces@lists.linaro.org) smtp.mail=linaro-mm-sig-bounces@lists.linaro.org Received: from localhost ([127.0.0.1] helo=mombin.canonical.com) by mombin.canonical.com with esmtp (Exim 4.71) (envelope-from ) id 1Sc3S1-0005je-It; Tue, 05 Jun 2012 23:46:13 +0000 Received: from mail-wi0-f202.google.com ([209.85.212.202]) by mombin.canonical.com with esmtp (Exim 4.71) (envelope-from ) id 1Sc3Rw-0005hZ-Ot for linaro-mm-sig@lists.linaro.org; Tue, 05 Jun 2012 23:46:08 +0000 Received: by wibhn6 with SMTP id hn6so284447wib.1 for ; Tue, 05 Jun 2012 16:46:08 -0700 (PDT) Received: by 10.14.94.207 with SMTP id n55mr5595247eef.16.1338939968462; Tue, 05 Jun 2012 16:46:08 -0700 (PDT) Received: by 10.14.94.207 with SMTP id n55mr5595243eef.16.1338939968383; Tue, 05 Jun 2012 16:46:08 -0700 (PDT) Received: from hpza9.eem.corp.google.com ([74.125.121.33]) by gmr-mx.google.com with ESMTPS id v14si9540eef.2.2012.06.05.16.46.08 (version=TLSv1/SSLv3 cipher=AES128-SHA); Tue, 05 Jun 2012 16:46:08 -0700 (PDT) Received: from dirtysouth.mtv.corp.google.com (dirtysouth.mtv.corp.google.com [172.18.104.93]) by hpza9.eem.corp.google.com (Postfix) with ESMTP id 0E66F5C0060; Tue, 5 Jun 2012 16:46:08 -0700 (PDT) Received: by dirtysouth.mtv.corp.google.com (Postfix, from userid 94083) id 72AD840DBA; Tue, 5 Jun 2012 16:46:07 -0700 (PDT) From: Erik Gilling To: linaro-mm-sig@lists.linaro.org Date: Tue, 5 Jun 2012 16:45:47 -0700 Message-Id: <1338939956-17312-3-git-send-email-konkers@android.com> X-Mailer: git-send-email 1.7.7.3 In-Reply-To: <1338939956-17312-1-git-send-email-konkers@android.com> References: <1338939956-17312-1-git-send-email-konkers@android.com> Subject: [Linaro-mm-sig] [RFC 02/11] sw_sync: add cpu based sync driver X-BeenThere: linaro-mm-sig@lists.linaro.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Unified memory management interest group." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: linaro-mm-sig-bounces@lists.linaro.org Errors-To: linaro-mm-sig-bounces@lists.linaro.org X-Gm-Message-State: ALoCoQmJhCtGD4W1k9ehvhvmmDRM60Y88lXlCOACNDt5Zv5lCNQF5UHwwOycYiyKbdjx/LPCXMzE This driver can be used by synchronization providers that don't need to communicate with hardware backed sync primitives. Signed-off-by: Erik Gilling --- drivers/base/Kconfig | 18 ++++ drivers/base/Makefile | 1 + drivers/base/sw_sync.c | 223 +++++++++++++++++++++++++++++++++++++++++++++++ include/linux/sw_sync.h | 58 ++++++++++++ 4 files changed, 300 insertions(+), 0 deletions(-) create mode 100644 drivers/base/sw_sync.c create mode 100644 include/linux/sw_sync.h diff --git a/drivers/base/Kconfig b/drivers/base/Kconfig index 784d08b..1131dd7 100644 --- a/drivers/base/Kconfig +++ b/drivers/base/Kconfig @@ -200,4 +200,22 @@ config SYNC This option enables the framework for synchronization between multiple drivers. Sync implementations can take advantage of hardware synchronization built into devices like GPUs. + +config SW_SYNC + bool "Software synchronization objects" + default n + depends on SYNC + help + A sync object driver that uses a 32bit counter to coordinate + syncrhronization. Useful when there is no hardware primitive backing + the synchronization. + +config SW_SYNC_USER + bool "Userspace API for SW_SYNC" + default n + depends on SW_SYNC + help + Provides a user space API to the sw sync object. + *WARNING* improper use of this can result in deadlocking kernel + drivers from userspace. endmenu diff --git a/drivers/base/Makefile b/drivers/base/Makefile index a43e369..0e4d3da 100644 --- a/drivers/base/Makefile +++ b/drivers/base/Makefile @@ -22,6 +22,7 @@ obj-$(CONFIG_REGMAP) += regmap/ obj-$(CONFIG_SOC_BUS) += soc.o obj-$(CONFIG_SYNC) += sync.o +obj-$(CONFIG_SW_SYNC) += sw_sync.o ccflags-$(CONFIG_DEBUG_DRIVER) := -DDEBUG diff --git a/drivers/base/sw_sync.c b/drivers/base/sw_sync.c new file mode 100644 index 0000000..e94ecc7 --- /dev/null +++ b/drivers/base/sw_sync.c @@ -0,0 +1,223 @@ +/* + * drivers/base/sw_sync.c + * + * Copyright (C) 2012 Google, Inc. + * + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * 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. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +static int sw_sync_cmp(u32 a, u32 b) +{ + if (a == b) + return 0; + + return ((s32)a - (s32)b) < 0 ? -1 : 1; +} + +struct sync_pt *sw_sync_pt_create(struct sw_sync_timeline *obj, u32 value) +{ + struct sw_sync_pt *pt; + + pt = (struct sw_sync_pt *) + sync_pt_create(&obj->obj, sizeof(struct sw_sync_pt)); + + pt->value = value; + + return (struct sync_pt *)pt; +} + +static struct sync_pt *sw_sync_pt_dup(struct sync_pt *sync_pt) +{ + struct sw_sync_pt *pt = (struct sw_sync_pt *) sync_pt; + struct sw_sync_timeline *obj = + (struct sw_sync_timeline *)sync_pt->parent; + + return (struct sync_pt *) sw_sync_pt_create(obj, pt->value); +} + +static int sw_sync_pt_has_signaled(struct sync_pt *sync_pt) +{ + struct sw_sync_pt *pt = (struct sw_sync_pt *)sync_pt; + struct sw_sync_timeline *obj = + (struct sw_sync_timeline *)sync_pt->parent; + + return sw_sync_cmp(obj->value, pt->value) >= 0; +} + +static int sw_sync_pt_compare(struct sync_pt *a, struct sync_pt *b) +{ + struct sw_sync_pt *pt_a = (struct sw_sync_pt *)a; + struct sw_sync_pt *pt_b = (struct sw_sync_pt *)b; + + return sw_sync_cmp(pt_a->value, pt_b->value); +} + +struct sync_timeline_ops sw_sync_timeline_ops = { + .driver_name = "sw_sync", + .dup = sw_sync_pt_dup, + .has_signaled = sw_sync_pt_has_signaled, + .compare = sw_sync_pt_compare, +}; + + +struct sw_sync_timeline *sw_sync_timeline_create(const char *name) +{ + struct sw_sync_timeline *obj = (struct sw_sync_timeline *) + sync_timeline_create(&sw_sync_timeline_ops, + sizeof(struct sw_sync_timeline), + name); + + return obj; +} + +void sw_sync_timeline_inc(struct sw_sync_timeline *obj, u32 inc) +{ + obj->value += inc; + + sync_timeline_signal(&obj->obj); +} + + +#ifdef CONFIG_SW_SYNC_USER +/* *WARNING* + * + * improper use of this can result in deadlocking kernel drivers from userspace. + */ + +/* opening sw_sync create a new sync obj */ +int sw_sync_open(struct inode *inode, struct file *file) +{ + struct sw_sync_timeline *obj; + char task_comm[TASK_COMM_LEN]; + + get_task_comm(task_comm, current); + + obj = sw_sync_timeline_create(task_comm); + if (obj == NULL) + return -ENOMEM; + + file->private_data = obj; + + return 0; +} + +int sw_sync_release(struct inode *inode, struct file *file) +{ + struct sw_sync_timeline *obj = file->private_data; + sync_timeline_destroy(&obj->obj); + return 0; +} + +long sw_sync_ioctl_create_fence(struct sw_sync_timeline *obj, unsigned long arg) +{ + int fd = get_unused_fd(); + int err; + struct sync_pt *pt; + struct sync_fence *fence; + struct sw_sync_create_fence_data data; + + if (copy_from_user(&data, (void __user *)arg, sizeof(data))) + return -EFAULT; + + pt = sw_sync_pt_create(obj, data.value); + if (pt == NULL) { + err = -ENOMEM; + goto err; + } + + data.name[sizeof(data.name) - 1] = '\0'; + fence = sync_fence_create(data.name, pt); + if (fence == NULL) { + sync_pt_free(pt); + err = -ENOMEM; + goto err; + } + + data.fence = fd; + if (copy_to_user((void __user *)arg, &data, sizeof(data))) { + sync_fence_put(fence); + err = -EFAULT; + goto err; + } + + sync_fence_install(fence, fd); + + return 0; + +err: + put_unused_fd(fd); + return err; +} + +long sw_sync_ioctl_inc(struct sw_sync_timeline *obj, unsigned long arg) +{ + u32 value; + + if (copy_from_user(&value, (void __user *)arg, sizeof(value))) + return -EFAULT; + + sw_sync_timeline_inc(obj, value); + + return 0; +} + +long sw_sync_ioctl(struct file *file, unsigned int cmd, unsigned long arg) +{ + struct sw_sync_timeline *obj = file->private_data; + + switch (cmd) { + case SW_SYNC_IOC_CREATE_FENCE: + return sw_sync_ioctl_create_fence(obj, arg); + + case SW_SYNC_IOC_INC: + return sw_sync_ioctl_inc(obj, arg); + + default: + return -ENOTTY; + } +} + +static const struct file_operations sw_sync_fops = { + .owner = THIS_MODULE, + .open = sw_sync_open, + .release = sw_sync_release, + .unlocked_ioctl = sw_sync_ioctl, +}; + +static struct miscdevice sw_sync_dev = { + .minor = MISC_DYNAMIC_MINOR, + .name = "sw_sync", + .fops = &sw_sync_fops, +}; + +int __init sw_sync_device_init(void) +{ + return misc_register(&sw_sync_dev); +} + +void __exit sw_sync_device_remove(void) +{ + misc_deregister(&sw_sync_dev); +} + +module_init(sw_sync_device_init); +module_exit(sw_sync_device_remove); + +#endif /* CONFIG_SW_SYNC_USER */ diff --git a/include/linux/sw_sync.h b/include/linux/sw_sync.h new file mode 100644 index 0000000..bd6f208 --- /dev/null +++ b/include/linux/sw_sync.h @@ -0,0 +1,58 @@ +/* + * include/linux/sw_sync.h + * + * Copyright (C) 2012 Google, Inc. + * + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * 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. + * + */ + +#ifndef _LINUX_SW_SYNC_H +#define _LINUX_SW_SYNC_H + +#include + +#ifdef __KERNEL__ + +#include + +struct sw_sync_timeline { + struct sync_timeline obj; + + u32 value; +}; + +struct sw_sync_pt { + struct sync_pt pt; + + u32 value; +}; + +struct sw_sync_timeline *sw_sync_timeline_create(const char *name); +void sw_sync_timeline_inc(struct sw_sync_timeline *obj, u32 inc); + +struct sync_pt *sw_sync_pt_create(struct sw_sync_timeline *obj, u32 value); + +#endif /* __KERNEL __ */ + +struct sw_sync_create_fence_data { + __u32 value; + char name[32]; + __s32 fence; /* fd of new fence */ +}; + +#define SW_SYNC_IOC_MAGIC 'W' + +#define SW_SYNC_IOC_CREATE_FENCE _IOWR(SW_SYNC_IOC_MAGIC, 0,\ + struct sw_sync_create_fence_data) +#define SW_SYNC_IOC_INC _IOW(SW_SYNC_IOC_MAGIC, 1, __u32) + + +#endif /* _LINUX_SW_SYNC_H */