From patchwork Thu Apr 17 11:56:18 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Taras Kondratiuk X-Patchwork-Id: 28561 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ob0-f200.google.com (mail-ob0-f200.google.com [209.85.214.200]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id B151D20674 for ; Thu, 17 Apr 2014 11:57:41 +0000 (UTC) Received: by mail-ob0-f200.google.com with SMTP id uz6sf1624549obc.3 for ; Thu, 17 Apr 2014 04:57:41 -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 :errors-to:sender:x-original-sender :x-original-authentication-results:mailing-list:content-type :content-transfer-encoding; bh=Yloos8eoD3m5y3eHLZ4IQua+dJDvX3L7iqFVyFgtNpw=; b=M9f/eTP+YgXdgC/PGvUzsbvPmkSLN4BX58yeUHd0UX829lkzk3A/7q8WkXobLdlxen /40oH7ItHNStYaUZzmOQacx1OAPORCTTPQZqQ9KZtMIeXQ+M/z1TQnbfWl2SSUBRTewt /3fmnZdW2CJDPWGrmkRaRnQez5uCNzn3Tc1QkcmGq07yjJ0v7u7/07k6KIwXT66PQLgG YC8nfgasmz+h6KBxN6nfP7UAv+ijQx07IvdboXrp+lCIUanR0o6dZpAeRNy8uZFu9mBm EQ78AIKWhmWOqEjggM01CZiug1Sj9mrw02VhUjiHiNpqgdX1t/gUpWCt1X4fsozkIDT4 yhyw== X-Gm-Message-State: ALoCoQk7n/yA4PEBkyiSILHwxamLUouOlSSssXvYWjipaJMr9+YTLyhCdha8v/p9azr/Ar0n7gMj X-Received: by 10.182.158.4 with SMTP id wq4mr7175425obb.18.1397735861214; Thu, 17 Apr 2014 04:57:41 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.17.3 with SMTP id 3ls1004002qgc.83.gmail; Thu, 17 Apr 2014 04:57:41 -0700 (PDT) X-Received: by 10.58.220.161 with SMTP id px1mr11672650vec.13.1397735861062; Thu, 17 Apr 2014 04:57:41 -0700 (PDT) Received: from mail-ve0-f169.google.com (mail-ve0-f169.google.com [209.85.128.169]) by mx.google.com with ESMTPS id ej2si4414620veb.149.2014.04.17.04.57.41 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 17 Apr 2014 04:57:41 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.128.169 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.128.169; Received: by mail-ve0-f169.google.com with SMTP id pa12so381859veb.28 for ; Thu, 17 Apr 2014 04:57:41 -0700 (PDT) X-Received: by 10.52.65.132 with SMTP id x4mr986016vds.36.1397735860965; Thu, 17 Apr 2014 04:57:40 -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.220.221.72 with SMTP id ib8csp29025vcb; Thu, 17 Apr 2014 04:57:40 -0700 (PDT) X-Received: by 10.140.48.77 with SMTP id n71mr9211831qga.90.1397735860486; Thu, 17 Apr 2014 04:57:40 -0700 (PDT) Received: from ip-10-141-164-156.ec2.internal (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTPS id m9si2471649qge.21.2014.04.17.04.57.39 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Thu, 17 Apr 2014 04:57:40 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Received: from localhost ([127.0.0.1] helo=ip-10-141-164-156.ec2.internal) by ip-10-141-164-156.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1WakwZ-00067g-B3; Thu, 17 Apr 2014 11:57:27 +0000 Received: from mail-ee0-f45.google.com ([74.125.83.45]) by ip-10-141-164-156.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1Wakvn-00064c-4l for lng-odp@lists.linaro.org; Thu, 17 Apr 2014 11:56:39 +0000 Received: by mail-ee0-f45.google.com with SMTP id d17so582054eek.4 for ; Thu, 17 Apr 2014 04:56:45 -0700 (PDT) X-Received: by 10.15.64.132 with SMTP id o4mr13533768eex.14.1397735804889; Thu, 17 Apr 2014 04:56:44 -0700 (PDT) Received: from uglx0153363.synapse.com ([195.238.92.128]) by mx.google.com with ESMTPSA id z48sm66742553eel.27.2014.04.17.04.56.43 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 17 Apr 2014 04:56:44 -0700 (PDT) From: Taras Kondratiuk To: lng-odp@lists.linaro.org Date: Thu, 17 Apr 2014 14:56:18 +0300 Message-Id: <1397735782-31924-8-git-send-email-taras.kondratiuk@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1397735782-31924-1-git-send-email-taras.kondratiuk@linaro.org> References: <1397735782-31924-1-git-send-email-taras.kondratiuk@linaro.org> Cc: Filip Moerman Subject: [lng-odp] [PATCH v2 07/11] Keystone2: Add CMA shared memory allocation 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=neutral (google.com: 209.85.128.169 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) 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 To be able to send buffers to PtkDMA they should be allocated from contiguous DMA-accessible memory. In a current application model buffer pools are created from memory allocated via odp_shm_reserve(). So implement this call to allocate memory from CMA allocator. Signed-off-by: Taras Kondratiuk --- .../include/odp_shared_memory_internal.h | 29 +++++ .../linux-keystone2/source/odp_shared_memory.c | 118 +++++++++++++++----- 2 files changed, 118 insertions(+), 29 deletions(-) create mode 100644 platform/linux-keystone2/include/odp_shared_memory_internal.h diff --git a/platform/linux-keystone2/include/odp_shared_memory_internal.h b/platform/linux-keystone2/include/odp_shared_memory_internal.h new file mode 100644 index 0000000..833091e --- /dev/null +++ b/platform/linux-keystone2/include/odp_shared_memory_internal.h @@ -0,0 +1,29 @@ +/* Copyright (c) 2014, Linaro Limited + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + + +/** + * @file + * + * ODP shared memory internal + */ + +#ifndef ODP_SHARED_MEMORY_INTERNAL_H_ +#define ODP_SHARED_MEMORY_INTERNAL_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +void *_odp_shm_reserve(const char *name, uint64_t size, uint64_t align, + int type); +uintptr_t _odp_shm_get_paddr(void *vaddr); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/platform/linux-keystone2/source/odp_shared_memory.c b/platform/linux-keystone2/source/odp_shared_memory.c index 8288b46..e595111 100644 --- a/platform/linux-keystone2/source/odp_shared_memory.c +++ b/platform/linux-keystone2/source/odp_shared_memory.c @@ -5,6 +5,7 @@ */ #include +#include #include #include #include @@ -20,6 +21,7 @@ #include #include +#include #define ODP_SHM_NUM_BLOCKS 32 @@ -28,10 +30,9 @@ typedef struct { char name[ODP_SHM_NAME_LEN]; uint64_t size; uint64_t align; - void *addr_orig; void *addr; int huge; - + ti_em_rh_mem_config_t mem_config; } odp_shm_block_t; @@ -92,14 +93,19 @@ static int find_block(const char *name) return -1; } +enum { + ODP_SHM_MMAP, + ODP_SHM_CMA +}; -void *odp_shm_reserve(const char *name, uint64_t size, uint64_t align) +void *_odp_shm_reserve(const char *name, uint64_t size, uint64_t align, + int type) { int i; odp_shm_block_t *block; - void *addr; #ifdef MAP_HUGETLB uint64_t huge_sz, page_sz; + ti_em_rh_mem_config_t mem_config = {0}; huge_sz = odp_sys_huge_page_size(); page_sz = odp_sys_page_size(); @@ -128,47 +134,100 @@ void *odp_shm_reserve(const char *name, uint64_t size, uint64_t align) block = &odp_shm_tbl->block[i]; - addr = MAP_FAILED; - block->huge = 0; + /* Allocate memory */ + mem_config.size = size + align; + mem_config.flags = TI_EM_OSAL_MEM_CACHED; + /* + * alloc free mapping id. + * FIXME: mapping_id is uint32_t. + */ + mem_config.mapping_id = -1; + + if (type == ODP_SHM_CMA) { + ti_em_rh_alloc_map_cma(&mem_config); + + if (!mem_config.vaddr) { + /* Alloc failed */ + odp_spinlock_unlock(&odp_shm_tbl->lock); + ODP_ERR("%s: failed to allocate block: %-24s %4"PRIu64" %4"PRIu64"\n", + __func__, + name, + size, + align); + return NULL; + } + + } else if (type == ODP_SHM_MMAP) { + void *addr = MAP_FAILED; + block->huge = 0; #ifdef MAP_HUGETLB - /* Try first huge pages */ - if (huge_sz && (size + align) > page_sz) { - addr = mmap(NULL, size + align, PROT_READ | PROT_WRITE, - SHM_FLAGS | MAP_HUGETLB, -1, 0); - } + /* Try first huge pages */ + if (huge_sz && (size + align) > page_sz) { + addr = mmap(NULL, size + align, PROT_READ | PROT_WRITE, + SHM_FLAGS | MAP_HUGETLB, -1, 0); + } #endif - /* Use normal pages for small or failed huge page allocations */ - if (addr == MAP_FAILED) { - addr = mmap(NULL, size + align, PROT_READ | PROT_WRITE, - SHM_FLAGS, -1, 0); + /* Use normal pages for small or failed huge page allocations */ + if (addr == MAP_FAILED) { + addr = mmap(NULL, size + align, PROT_READ | PROT_WRITE, + SHM_FLAGS, -1, 0); + } else { + block->huge = 1; + } + if (addr == MAP_FAILED) { + /* Alloc failed */ + odp_spinlock_unlock(&odp_shm_tbl->lock); + return NULL; + } + mem_config.vaddr = (uintptr_t)addr; } else { - block->huge = 1; - } - - if (addr == MAP_FAILED) { - /* Alloc failed */ - odp_spinlock_unlock(&odp_shm_tbl->lock); - return NULL; + ODP_ERR("Unknown shared memory type: %d\n", type); } - block->addr_orig = addr; + block->mem_config = mem_config; /* move to correct alignment */ - addr = ODP_ALIGN_ROUNDUP_PTR(addr, align); + block->addr = ODP_ALIGN_ROUNDUP_PTR(mem_config.vaddr, align); strncpy(block->name, name, ODP_SHM_NAME_LEN - 1); block->name[ODP_SHM_NAME_LEN - 1] = 0; block->size = size; block->align = align; - block->addr = addr; odp_spinlock_unlock(&odp_shm_tbl->lock); - return addr; + ODP_DBG("%s: reserved block: %-24s %4"PRIu64" %4"PRIu64" %p\n", + __func__, + block->name, + block->size, + block->align, + block->addr); + + return block->addr; +} + +void *odp_shm_reserve(const char *name, uint64_t size, uint64_t align) +{ + return _odp_shm_reserve(name, size, align, ODP_SHM_CMA); } +uintptr_t _odp_shm_get_paddr(void *vaddr) +{ + int i; + uintptr_t addr = (uintptr_t)vaddr; + for (i = 0; i < ODP_SHM_NUM_BLOCKS; i++) { + ti_em_rh_mem_config_t *mem = &odp_shm_tbl->block[i].mem_config; + if (mem->vaddr == 0) + continue; + if ((mem->vaddr <= addr) && (addr < mem->vaddr + mem->size)) { + addr = (uintptr_t)odp_shm_tbl->block[i].addr; + return (addr - mem->vaddr) + mem->paddr; + } + } + return 0; +} void *odp_shm_lookup(const char *name) { @@ -202,7 +261,7 @@ void odp_shm_print_all(void) odp_sys_huge_page_size() / 1024); printf("\n"); - printf(" id name kB align huge addr\n"); + printf(" id name kB align huge addr paddr\n"); for (i = 0; i < ODP_SHM_NUM_BLOCKS; i++) { odp_shm_block_t *block; @@ -210,13 +269,14 @@ void odp_shm_print_all(void) block = &odp_shm_tbl->block[i]; if (block->addr) { - printf(" %2i %-24s %4"PRIu64" %4"PRIu64" %2c %p\n", + printf(" %2i %-24s %4"PRIu64" %4"PRIu64" %2c %p 0x%08x\n", i, block->name, block->size/1024, block->align, (block->huge ? '*' : ' '), - block->addr); + block->addr, + block->mem_config.paddr); } }