From patchwork Thu Apr 24 17:01:35 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Taras Kondratiuk X-Patchwork-Id: 29004 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-la0-f70.google.com (mail-la0-f70.google.com [209.85.215.70]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id DB3A52036A for ; Thu, 24 Apr 2014 17:02:31 +0000 (UTC) Received: by mail-la0-f70.google.com with SMTP id pv20sf1507999lab.1 for ; Thu, 24 Apr 2014 10:02:30 -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=XWfrN5nBuRHP8sqLstb//bCNI/a3JBfuuOrdn/u98m8=; b=lyhSiA5fOAasviTYjZSA36YhhP0Nz0h+tjjNU/1Z5D+Di0x7We5wDTLkEvVhoDleUf RBzPhX91s7v/mq6UZYU2/E9Q2Jy96aP/TjkhRCpJJRyKcqJORZfJANC87BJVz034GbpM nfwCsL7RE38NOxWVxoZ+VYgM0zAz2B1ukWSy7BBi9/cKLZHu2xngP0pmAWxEJxhiLniv jBqzhIhvxXh7XxBFwir07bMzjI13hJDbn/FXf8a8qEO/yDwYqJ+rVYK4S2Ab7H2PPBxy GcdG4sQkrZERufRNKewmYwGdiv0ai0Rs0Vd+ZQVv7XWkPdZ6jGXUE34DDO3QGUB5GUQK f6eQ== X-Gm-Message-State: ALoCoQmt08sFr67LeRyLLWn6AaUj9WguI8MDoW4uE3CN7/IkNkWfcBDX0x4DD8Jgs6u1wxO4qQw4 X-Received: by 10.14.8.66 with SMTP id 42mr10060eeq.7.1398358949960; Thu, 24 Apr 2014 10:02:29 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.109.71 with SMTP id k65ls1211176qgf.28.gmail; Thu, 24 Apr 2014 10:02:29 -0700 (PDT) X-Received: by 10.221.20.199 with SMTP id qp7mr1982155vcb.24.1398358949792; Thu, 24 Apr 2014 10:02:29 -0700 (PDT) Received: from mail-ve0-f182.google.com (mail-ve0-f182.google.com [209.85.128.182]) by mx.google.com with ESMTPS id ya4si1065616vec.109.2014.04.24.10.02.29 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 24 Apr 2014 10:02:29 -0700 (PDT) Received-SPF: none (google.com: patch+caf_=patchwork-forward=linaro.org@linaro.org does not designate permitted sender hosts) client-ip=209.85.128.182; Received: by mail-ve0-f182.google.com with SMTP id jw12so3310726veb.13 for ; Thu, 24 Apr 2014 10:02:29 -0700 (PDT) X-Received: by 10.220.250.203 with SMTP id mp11mr1985681vcb.2.1398358949668; Thu, 24 Apr 2014 10:02:29 -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 ib8csp31742vcb; Thu, 24 Apr 2014 10:02:26 -0700 (PDT) X-Received: by 10.224.54.68 with SMTP id p4mr4395633qag.47.1398358946295; Thu, 24 Apr 2014 10:02:26 -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 u89si2525413qga.83.2014.04.24.10.02.25 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Thu, 24 Apr 2014 10:02:26 -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-141-164-156.ec2.internal) by ip-10-141-164-156.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1WdN2A-0004I3-Ka; Thu, 24 Apr 2014 17:02:02 +0000 Received: from mail-ee0-f44.google.com ([74.125.83.44]) by ip-10-141-164-156.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1WdN1j-0004GB-BI for lng-odp@lists.linaro.org; Thu, 24 Apr 2014 17:01:35 +0000 Received: by mail-ee0-f44.google.com with SMTP id e49so2059495eek.17 for ; Thu, 24 Apr 2014 10:01:52 -0700 (PDT) X-Received: by 10.15.52.195 with SMTP id p43mr3691712eew.72.1398358912089; Thu, 24 Apr 2014 10:01:52 -0700 (PDT) Received: from uglx0153363.synapse.com ([195.238.92.128]) by mx.google.com with ESMTPSA id w46sm17371207eeo.35.2014.04.24.10.01.50 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 24 Apr 2014 10:01:51 -0700 (PDT) From: Taras Kondratiuk To: lng-odp@lists.linaro.org Date: Thu, 24 Apr 2014 20:01:35 +0300 Message-Id: <1398358899-9851-5-git-send-email-taras.kondratiuk@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1398358899-9851-1-git-send-email-taras.kondratiuk@linaro.org> References: <1398358899-9851-1-git-send-email-taras.kondratiuk@linaro.org> Cc: linaro-networking@linaro.org Subject: [lng-odp] [PATCH v4 4/8] 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: patch+caf_=patchwork-forward=linaro.org@linaro.org does not designate permitted sender hosts) 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 | 284 ++++++++++++++++++++ 2 files changed, 313 insertions(+) create mode 100644 platform/linux-keystone2/include/odp_shared_memory_internal.h create mode 100644 platform/linux-keystone2/source/odp_shared_memory.c 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 new file mode 100644 index 0000000..e595111 --- /dev/null +++ b/platform/linux-keystone2/source/odp_shared_memory.c @@ -0,0 +1,284 @@ +/* Copyright (c) 2013, Linaro Limited + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#ifdef __powerpc__ +#include +#endif +#include + +#include +#include + +#include + +#define ODP_SHM_NUM_BLOCKS 32 + + +typedef struct { + char name[ODP_SHM_NAME_LEN]; + uint64_t size; + uint64_t align; + void *addr; + int huge; + ti_em_rh_mem_config_t mem_config; +} odp_shm_block_t; + + +typedef struct { + odp_shm_block_t block[ODP_SHM_NUM_BLOCKS]; + odp_spinlock_t lock; + +} odp_shm_table_t; + + +#define SHM_FLAGS (MAP_SHARED | MAP_ANONYMOUS) + + +/* Global shared memory table */ +static odp_shm_table_t *odp_shm_tbl; + + +int odp_shm_init_global(void) +{ + void *addr; + +#ifndef MAP_HUGETLB + ODP_DBG("NOTE: mmap does not support huge pages\n"); +#endif + + addr = mmap(NULL, sizeof(odp_shm_table_t), + PROT_READ | PROT_WRITE, SHM_FLAGS, -1, 0); + + if (addr == MAP_FAILED) + return -1; + + odp_shm_tbl = addr; + + memset(odp_shm_tbl, 0, sizeof(odp_shm_table_t)); + odp_spinlock_init(&odp_shm_tbl->lock); + + return 0; +} + + +int odp_shm_init_local(void) +{ + return 0; +} + + +static int find_block(const char *name) +{ + int i; + + for (i = 0; i < ODP_SHM_NUM_BLOCKS; i++) { + if (strcmp(name, odp_shm_tbl->block[i].name) == 0) { + /* found it */ + return i; + } + } + + return -1; +} + +enum { + ODP_SHM_MMAP, + ODP_SHM_CMA +}; + +void *_odp_shm_reserve(const char *name, uint64_t size, uint64_t align, + int type) +{ + int i; + odp_shm_block_t *block; +#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(); +#endif + + odp_spinlock_lock(&odp_shm_tbl->lock); + + if (find_block(name) >= 0) { + /* Found a block with the same name */ + odp_spinlock_unlock(&odp_shm_tbl->lock); + return NULL; + } + + for (i = 0; i < ODP_SHM_NUM_BLOCKS; i++) { + if (odp_shm_tbl->block[i].addr == NULL) { + /* Found free block */ + break; + } + } + + if (i > ODP_SHM_NUM_BLOCKS - 1) { + /* Table full */ + odp_spinlock_unlock(&odp_shm_tbl->lock); + return NULL; + } + + block = &odp_shm_tbl->block[i]; + + /* 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); + } +#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); + } 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 { + ODP_ERR("Unknown shared memory type: %d\n", type); + } + + block->mem_config = mem_config; + + /* move to correct alignment */ + 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; + + odp_spinlock_unlock(&odp_shm_tbl->lock); + 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) +{ + int i; + void *addr; + + odp_spinlock_lock(&odp_shm_tbl->lock); + + i = find_block(name); + + if (i < 0) { + odp_spinlock_unlock(&odp_shm_tbl->lock); + return NULL; + } + + addr = odp_shm_tbl->block[i].addr; + odp_spinlock_unlock(&odp_shm_tbl->lock); + + return addr; +} + + +void odp_shm_print_all(void) +{ + int i; + + printf("\nShared memory\n"); + printf("--------------\n"); + printf(" page size: %"PRIu64" kB\n", odp_sys_page_size() / 1024); + printf(" huge page size: %"PRIu64" kB\n", + odp_sys_huge_page_size() / 1024); + printf("\n"); + + printf(" id name kB align huge addr paddr\n"); + + for (i = 0; i < ODP_SHM_NUM_BLOCKS; i++) { + odp_shm_block_t *block; + + block = &odp_shm_tbl->block[i]; + + if (block->addr) { + 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->mem_config.paddr); + } + } + + printf("\n"); +}