From patchwork Mon Apr 14 11:55:27 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Taras Kondratiuk X-Patchwork-Id: 28316 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-oa0-f72.google.com (mail-oa0-f72.google.com [209.85.219.72]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 5144520F46 for ; Mon, 14 Apr 2014 11:57:58 +0000 (UTC) Received: by mail-oa0-f72.google.com with SMTP id eb12sf44135026oac.7 for ; Mon, 14 Apr 2014 04:57:57 -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=qVG9leHAUVC34O++SnWi08X8qA8Nhf2SoibB7GwlqaU=; b=CsuyxEO7BoLYrwoAc31N5Zm8aCrTbpojc2B9dhXUAbl+1j7ijwP3qNDNo6ez7wkq7N SIBThvEU2HGmz1dSmHG+I/zsHecZvy4TyYgvFMVHKAdGClIZP0EKQUyqruzvkgZFB/e+ 1eiYAsMR9eM+fJpMh8TMs1gTYEp/wie67FSVuX9Mdib8HNoMclJCkeeSIWJApvkVEWIy dmRY9CZTx+DeBTX6jfUkmyd5UHCY4+FPmyVguUtOgS4d3FId8YCBpMy1YUl9jlncO3eD +0H5WpgHAiHaOJ3UH3DxA1RvRfEB1luEVlHKACS8v6L6rHIU30hct5VFQpMJa9skcRza 84GQ== X-Gm-Message-State: ALoCoQncIv6KRDUKXSLIIgXFfpeiEuqZXIHSiI4l3LB/Gws9047dGN+7tnNgvNQrZopgJm/fQSXv X-Received: by 10.50.70.66 with SMTP id k2mr7367424igu.7.1397476677833; Mon, 14 Apr 2014 04:57:57 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.109.131 with SMTP id l3ls2677984qgf.40.gmail; Mon, 14 Apr 2014 04:57:57 -0700 (PDT) X-Received: by 10.58.46.207 with SMTP id x15mr38340409vem.17.1397476677647; Mon, 14 Apr 2014 04:57:57 -0700 (PDT) Received: from mail-vc0-f170.google.com (mail-vc0-f170.google.com [209.85.220.170]) by mx.google.com with ESMTPS id tm8si2702099vdc.8.2014.04.14.04.57.57 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 14 Apr 2014 04:57:57 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.220.170 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.220.170; Received: by mail-vc0-f170.google.com with SMTP id hu19so7438198vcb.15 for ; Mon, 14 Apr 2014 04:57:57 -0700 (PDT) X-Received: by 10.52.183.228 with SMTP id ep4mr1374821vdc.30.1397476677559; Mon, 14 Apr 2014 04:57:57 -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 ib8csp141318vcb; Mon, 14 Apr 2014 04:57:56 -0700 (PDT) X-Received: by 10.224.8.131 with SMTP id h3mr18952877qah.61.1397476676782; Mon, 14 Apr 2014 04:57:56 -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 t105si6631632qgd.157.2014.04.14.04.57.55 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 14 Apr 2014 04:57:56 -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 1WZfWG-0001n9-37; Mon, 14 Apr 2014 11:57:48 +0000 Received: from mail-lb0-f173.google.com ([209.85.217.173]) by ip-10-141-164-156.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1WZfUR-0001jn-QN for lng-odp@lists.linaro.org; Mon, 14 Apr 2014 11:55:55 +0000 Received: by mail-lb0-f173.google.com with SMTP id p9so5626372lbv.32 for ; Mon, 14 Apr 2014 04:55:57 -0700 (PDT) X-Received: by 10.152.170.137 with SMTP id am9mr29604195lac.15.1397476557418; Mon, 14 Apr 2014 04:55:57 -0700 (PDT) Received: from uglx0153363.synapse.com ([195.238.92.128]) by mx.google.com with ESMTPSA id bm3sm14022014lbb.12.2014.04.14.04.55.56 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 14 Apr 2014 04:55:56 -0700 (PDT) From: Taras Kondratiuk To: lng-odp@lists.linaro.org Date: Mon, 14 Apr 2014 14:55:27 +0300 Message-Id: <1397476530-20816-8-git-send-email-taras.kondratiuk@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1397476530-20816-1-git-send-email-taras.kondratiuk@linaro.org> References: <1397476530-20816-1-git-send-email-taras.kondratiuk@linaro.org> Cc: Filip Moerman Subject: [lng-odp] [PATCH 07/10] 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.220.170 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 | 30 +++++ .../linux-keystone2/source/odp_shared_memory.c | 118 +++++++++++++++----- 2 files changed, 119 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..887d04e --- /dev/null +++ b/platform/linux-keystone2/include/odp_shared_memory_internal.h @@ -0,0 +1,30 @@ +/* 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); } }