From patchwork Tue Mar 10 15:31:11 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Taras Kondratiuk X-Patchwork-Id: 45594 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-lb0-f200.google.com (mail-lb0-f200.google.com [209.85.217.200]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id B9A32214BF for ; Tue, 10 Mar 2015 15:32:50 +0000 (UTC) Received: by lbdu14 with SMTP id u14sf2069650lbd.3 for ; Tue, 10 Mar 2015 08:32:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:message-id:in-reply-to :references:cc:subject:precedence:list-id:list-unsubscribe :list-archive:list-post:list-help:list-subscribe:mime-version :content-type:content-transfer-encoding:errors-to:sender :x-original-sender:x-original-authentication-results:mailing-list; bh=jLL9e5odWbpBEVM8L7D34OvQkBL4VXtSTEaTLDGKGb4=; b=B1/oDtU6OaBBljcvhlILbAfjWD9EYqASFVQ9UfLzlHgdWJV9Mrr+ba0p7nlyDNsX6d Xet+m/Ht3s4+P7WbsHLoZ7ZKBjmJ8voYXIY6EkqnpgydNdr1Li3tD2fsgxOh33id7UCZ PJ6a4321eHRF4vRdNIZqDT971saVWo7Wv4gEtcK3DTBGcQTVUdBKZ2D/BQ90nEAwKuXy 2JIxLE9pBrxo4uFI95xJM9lfwdtWphq+n1bkKvMRoq2yIj6D9Ib6ycF+0/I+Kr1AIL9h /WRli4Vj9wTUOmOs8g91q9zaVBTiM9NZWGv+XAT43OzpfCePVtE3gzX2ChMptCEs9kJy JnfA== X-Gm-Message-State: ALoCoQmQ5HtcpxykN7u90GPRa8ucqCk2Hin8aySBDim8nj36rcq4zD1SJsTHGqQtlTEZskxR5xV5 X-Received: by 10.112.44.167 with SMTP id f7mr2272826lbm.9.1426001569770; Tue, 10 Mar 2015 08:32:49 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.37.227 with SMTP id b3ls35048lak.58.gmail; Tue, 10 Mar 2015 08:32:49 -0700 (PDT) X-Received: by 10.112.110.231 with SMTP id id7mr31057643lbb.28.1426001569618; Tue, 10 Mar 2015 08:32:49 -0700 (PDT) Received: from mail-lb0-f179.google.com (mail-lb0-f179.google.com. [209.85.217.179]) by mx.google.com with ESMTPS id wu9si525729lbc.116.2015.03.10.08.32.49 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 10 Mar 2015 08:32:49 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.179 as permitted sender) client-ip=209.85.217.179; Received: by lbiz12 with SMTP id z12so2582196lbi.5 for ; Tue, 10 Mar 2015 08:32:49 -0700 (PDT) X-Received: by 10.152.87.3 with SMTP id t3mr31085508laz.19.1426001569431; Tue, 10 Mar 2015 08:32:49 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.35.133 with SMTP id h5csp2207198lbj; Tue, 10 Mar 2015 08:32:48 -0700 (PDT) X-Received: by 10.140.96.139 with SMTP id k11mr40770606qge.102.1426001567720; Tue, 10 Mar 2015 08:32:47 -0700 (PDT) Received: from ip-10-35-177-41.ec2.internal (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTPS id o80si763102qhb.75.2015.03.10.08.32.46 (version=TLSv1 cipher=RC4-SHA bits=128/128); Tue, 10 Mar 2015 08:32:47 -0700 (PDT) Received-SPF: none (google.com: lng-odp-bounces@lists.linaro.org does not designate permitted sender hosts) client-ip=54.225.227.206; Received: from localhost ([127.0.0.1] helo=ip-10-35-177-41.ec2.internal) by ip-10-35-177-41.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1YVM9E-000438-4Y; Tue, 10 Mar 2015 15:32:44 +0000 Received: from mail-lb0-f170.google.com ([209.85.217.170]) by ip-10-35-177-41.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1YVM8A-0003pj-GS for lng-odp@lists.linaro.org; Tue, 10 Mar 2015 15:31:38 +0000 Received: by lbdu14 with SMTP id u14so2628061lbd.0 for ; Tue, 10 Mar 2015 08:31:32 -0700 (PDT) X-Received: by 10.112.8.101 with SMTP id q5mr25113259lba.19.1426001492865; Tue, 10 Mar 2015 08:31:32 -0700 (PDT) Received: from uglx0153363.synapse.com ([195.238.92.128]) by mx.google.com with ESMTPSA id a2sm106609lbm.32.2015.03.10.08.31.32 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 10 Mar 2015 08:31:32 -0700 (PDT) From: Taras Kondratiuk To: lng-odp@lists.linaro.org Date: Tue, 10 Mar 2015 17:31:11 +0200 Message-Id: <1426001473-14618-14-git-send-email-taras.kondratiuk@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1426001473-14618-1-git-send-email-taras.kondratiuk@linaro.org> References: <1426001473-14618-1-git-send-email-taras.kondratiuk@linaro.org> X-Topics: patch Cc: Taras Kondratiuk Subject: [lng-odp] [KEYSTONE2 PATCH 13/15] linux-ks2: Add shared resources handling X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , MIME-Version: 1.0 Errors-To: lng-odp-bounces@lists.linaro.org Sender: lng-odp-bounces@lists.linaro.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: taras.kondratiuk@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.179 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 Signed-off-by: Taras Kondratiuk Signed-off-by: Taras Kondratiuk --- platform/linux-keystone2/Makefile.am | 2 + .../include/odp/plat/shared_resource.h | 150 +++++++++++++++++ platform/linux-keystone2/odp_shr.c | 179 +++++++++++++++++++++ 3 files changed, 331 insertions(+) create mode 100644 platform/linux-keystone2/include/odp/plat/shared_resource.h create mode 100644 platform/linux-keystone2/odp_shr.c diff --git a/platform/linux-keystone2/Makefile.am b/platform/linux-keystone2/Makefile.am index 282f406..f3d8471 100644 --- a/platform/linux-keystone2/Makefile.am +++ b/platform/linux-keystone2/Makefile.am @@ -67,6 +67,7 @@ odpplatinclude_HEADERS = \ $(srcdir)/include/odp/plat/packet_types.h \ $(srcdir)/include/odp/plat/pool_types.h \ $(srcdir)/include/odp/plat/queue_types.h \ + $(srcdir)/include/odp/plat/shared_resource.h \ $(srcdir)/include/odp/plat/state.h \ $(srcdir)/include/odp/plat/ti_mcsdk.h \ $(linux_generic_srcdir)/include/odp/plat/atomic_types.h \ @@ -140,6 +141,7 @@ __LIB__libodp_la_SOURCES = \ mcsdk/mcsdk_navig.c \ mcsdk/mcsdk_rmclient.c \ mcsdk/sockutils.c \ + odp_shr.c \ ../linux-generic/odp_barrier.c \ ../linux-generic/odp_cpumask.c \ ../linux-generic/odp_errno.c \ diff --git a/platform/linux-keystone2/include/odp/plat/shared_resource.h b/platform/linux-keystone2/include/odp/plat/shared_resource.h new file mode 100644 index 0000000..12893be --- /dev/null +++ b/platform/linux-keystone2/include/odp/plat/shared_resource.h @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2015, Linaro Limited + * Copyright (c) 2015, Texas Instruments Incorporated + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#ifndef ODP_PLAT_SHM_TABLE_H_ +#define ODP_PLAT_SHM_TABLE_H_ + +#include +#include + +typedef struct entry_head { + struct entry_head *next; + uint16_t id; + odp_spinlock_t lock; + bool allocated; +} odp_shr_head_t; + +typedef struct odp_shr_table_s { + struct entry_head *top_free; + odp_spinlock_t lock; + struct entry_head *base; + uint32_t entry_size; + uint32_t entry_num; +} *odp_shr_table_t; + +/* Define typeof for C99 compatibility */ +#define typeof __typeof__ + +#define ODP_SHR_TABLE_INVALID ((odp_shr_table_t)NULL) + +odp_shr_table_t _odp_shr_table_create(const char *name, + uint32_t entry_size, + uint32_t entry_num, + uint32_t entry_align, + uint16_t head_offset); +void odp_shr_table_destroy(odp_shr_table_t table); + +void odp_shr_table_lock(odp_shr_table_t table); +void odp_shr_table_unlock(odp_shr_table_t table); +odp_shr_head_t *_odp_shr_alloc_nolock(odp_shr_table_t table); +odp_shr_head_t *_odp_shr_alloc(odp_shr_table_t table); +void _odp_shr_free(odp_shr_table_t table, odp_shr_head_t *head); +odp_shr_head_t *_odp_shr_next(odp_shr_table_t table, odp_shr_head_t *head); +odp_shr_head_t *_odp_shr_first_allocated(odp_shr_table_t table); +odp_shr_head_t *_odp_shr_next_allocated(odp_shr_table_t table, + odp_shr_head_t *head); + +static inline +uint16_t _odp_shr_id(odp_shr_head_t *entry) +{ + ODP_ASSERT(entry, "Bad entry"); + return entry->id; +} + +static inline +uint32_t _odp_shr_table_num_entries(odp_shr_table_t table) +{ + ODP_ASSERT(table, "Bad table"); + return table->entry_num; +} + +static inline +uint32_t _odp_shr_size(odp_shr_table_t table) +{ + ODP_ASSERT(table, "Bad table"); + return table->entry_size; +} + +static inline +odp_shr_head_t *_odp_shr_from_id(odp_shr_table_t table, uint16_t id) +{ + ODP_ASSERT(table, "Bad table"); + ODP_ASSERT(id < table->entry_num, "Wrong entry ID"); + return (odp_shr_head_t *)((uintptr_t)table->base + + id * table->entry_size); +} + +static inline +bool _odp_shr_is_allocated(odp_shr_head_t *entry) +{ + ODP_ASSERT(entry, "Bad entry"); + return entry->allocated; +} + +static inline +void _odp_shr_lock(odp_shr_head_t *head) +{ + odp_spinlock_lock(&head->lock); +} + +static inline +void _odp_shr_unlock(odp_shr_head_t *head) +{ + odp_spinlock_unlock(&head->lock); +} + +#define odp_shr(head, type) \ + ({ typeof(head) __head = (head); \ + (__head ? ODP_CONTAINEROF(__head, type, entry_head) : NULL);\ + }) + +#define odp_shr_from_id(table, id, type) \ + odp_shr(_odp_shr_from_id(table, id), \ + type) + +#define odp_shr_next(entry) \ + odp_shr(_odp_shr_next(&(entry)->entry_head), \ + typeof(*entry)) + +#define odp_shr_first_allocated(table, type) \ + odp_shr(_odp_shr_first_allocated(table), \ + type) + +#define odp_shr_next_allocated(table, entry) \ + odp_shr(_odp_shr_next_allocated(table, &(entry)->entry_head), \ + typeof(*entry)) + +#define odp_shr_table_for_each_entry(table, entry) \ + for (entry = odp_shr_from_id(table, 0, typeof(*entry)); \ + entry; \ + entry = odp_shr_next(entry)) + +#define odp_shr_table_for_each_allocated_entry(table, entry) \ + for (entry = odp_shr_first_allocated(table, typeof(*entry)); \ + entry; \ + entry = odp_shr_next_allocated(table, entry)) + +#define odp_shr_table_create(name, type, num) \ + _odp_shr_table_create(name, sizeof(type), num, ODP_ALIGNOF(type), \ + ODP_OFFSETOF(type, entry_head)) + +#define odp_shr_alloc(table, type) odp_shr(_odp_shr_alloc(table), type) + +#define odp_shr_alloc_nolock(table, type) \ + odp_shr(_odp_shr_alloc_nolock(table), type) + +#define odp_shr_free(table, entry) _odp_shr_free(table, &(entry)->entry_head) + +#define odp_shr_is_allocated(entry) \ + _odp_shr_is_allocated(&(entry)->entry_head) + +#define odp_shr_lock(entry) _odp_shr_lock(&(entry)->entry_head) +#define odp_shr_unlock(entry) _odp_shr_unlock(&(entry)->entry_head) + +#define odp_shr_id(entry) _odp_shr_id(&(entry)->entry_head) +#endif diff --git a/platform/linux-keystone2/odp_shr.c b/platform/linux-keystone2/odp_shr.c new file mode 100644 index 0000000..ff0f570 --- /dev/null +++ b/platform/linux-keystone2/odp_shr.c @@ -0,0 +1,179 @@ +/* + * Copyright (c) 2015, Linaro Limited + * Copyright (c) 2015, Texas Instruments Incorporated + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include +#include +#include +#include + +static void push_entry(odp_shr_head_t **top, + odp_shr_head_t *head) +{ + head->next = *top; + *top = head; +} + +static odp_shr_head_t *pop_entry(odp_shr_head_t **top) +{ + odp_shr_head_t *head; + + head = *top; + if (!head) + return NULL; + *top = head->next; + return head; +} + +void odp_shr_table_lock(odp_shr_table_t table) +{ + odp_spinlock_lock(&table->lock); +} + +void odp_shr_table_unlock(odp_shr_table_t table) +{ + odp_spinlock_unlock(&table->lock); +} + +odp_shr_head_t *_odp_shr_alloc_nolock(odp_shr_table_t table) +{ + ODP_ASSERT(table, "Bad table"); + odp_shr_head_t *head; + head = pop_entry(&table->top_free); + ODP_ASSERT(!head->allocated, "Got already allocated entry"); + head->allocated = 1; + return head; +} + +odp_shr_head_t *_odp_shr_alloc(odp_shr_table_t table) +{ + ODP_ASSERT(table, "Bad table"); + odp_shr_head_t *head; + odp_shr_table_lock(table); + head = _odp_shr_alloc_nolock(table); + odp_shr_table_unlock(table); + ODP_DBG("Allocated entry head: %p\n", head); + return head; +} + +void _odp_shr_free(odp_shr_table_t table, odp_shr_head_t *head) +{ + ODP_ASSERT(head, "Bad head"); + ODP_ASSERT(head->allocated, "Freeing already freed entry"); + ODP_ASSERT(table, "Bad table"); + odp_shr_table_lock(table); + head->allocated = 0; + push_entry(&table->top_free, head); + odp_shr_table_unlock(table); +} + +odp_shr_table_t _odp_shr_table_create(const char *name, + uint32_t entry_size, + uint32_t entry_num, + uint32_t entry_align, + uint16_t head_offset) +{ + odp_shm_t shm; + void *addr; + uint32_t size; + uint32_t table_head_size; + odp_shr_table_t table; + odp_shr_head_t *head; + uint32_t i; + + if (entry_align == 0) + entry_align = ODP_CACHE_LINE_SIZE; + + table_head_size = ODP_ALIGN_ROUNDUP(sizeof(odp_shr_table_t), + entry_align); + entry_size = ODP_ALIGN_ROUNDUP(entry_size, entry_align); + size = table_head_size + entry_size * entry_num; + + shm = odp_shm_reserve(name, size, entry_align, ODP_SHM_SW_ONLY); + if (shm == ODP_SHM_INVALID) + return ODP_SHR_TABLE_INVALID; + + addr = odp_shm_addr(shm); + if (addr == NULL) { + odp_shm_free(shm); + return ODP_SHR_TABLE_INVALID; + } + + memset(addr, 0, size); + + table = addr; + odp_spinlock_init(&table->lock); + table->top_free = NULL; + table->entry_num = entry_num; + table->entry_size = entry_size; + + addr = (uint8_t *)addr + table_head_size + head_offset; + table->base = addr; + + for (i = 0; i < entry_num; i++) { + ODP_ASSERT(ODP_ALIGNED_CHECK((uint8_t *)addr - head_offset, + entry_align), + "Wrong entry alignment"); + head = addr; + head->id = i; + odp_spinlock_init(&head->lock); + push_entry(&table->top_free, head); + addr = (uint8_t *)addr + entry_size; + } + + return table; +} + +void odp_shr_table_destroy(odp_shr_table_t table) +{ + (void)table; + ODP_UNIMPLEMENTED(); +} + +odp_shr_head_t *_odp_shr_next(odp_shr_table_t table, odp_shr_head_t *head) +{ + ODP_ASSERT(head, "Bad head"); + ODP_ASSERT(table, "Bad table"); + uint16_t id = _odp_shr_id(head); + if (++id >= _odp_shr_table_num_entries(table)) + return NULL; + return _odp_shr_from_id(table, id); +} + + +#define _odp_shr_table_for_each(table, head) \ + for (head = _odp_shr_from_id(table, 0); \ + head; \ + head = _odp_shr_next(table, head)) + +#define _odp_shr_table_for_each_continue(table, head) \ + for (head = _odp_shr_next(table, head); \ + head; \ + head = _odp_shr_next(table, head)) + +odp_shr_head_t *_odp_shr_first_allocated(odp_shr_table_t table) +{ + odp_shr_head_t *head; + _odp_shr_table_for_each(table, head) { + if (head->allocated) + return head; + } + return NULL; +} + +odp_shr_head_t *_odp_shr_next_allocated(odp_shr_table_t table, + odp_shr_head_t *head) +{ + ODP_ASSERT(head, "Bad head"); + ODP_ASSERT(table, "Bad table"); + + _odp_shr_table_for_each_continue(table, head) { + if (head->allocated) + return head; + } + return NULL; +}