~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/metag/kernel/tcm.c

Version: ~ [ linux-5.1-rc2 ] ~ [ linux-5.0.3 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.30 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.107 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.164 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.176 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.136 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.63 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 /*
  3  * Copyright (C) 2010 Imagination Technologies Ltd.
  4  */
  5 
  6 #include <linux/init.h>
  7 #include <linux/kernel.h>
  8 #include <linux/spinlock.h>
  9 #include <linux/stddef.h>
 10 #include <linux/genalloc.h>
 11 #include <linux/string.h>
 12 #include <linux/list.h>
 13 #include <linux/slab.h>
 14 #include <asm/page.h>
 15 #include <asm/tcm.h>
 16 
 17 struct tcm_pool {
 18         struct list_head list;
 19         unsigned int tag;
 20         unsigned long start;
 21         unsigned long end;
 22         struct gen_pool *pool;
 23 };
 24 
 25 static LIST_HEAD(pool_list);
 26 
 27 static struct tcm_pool *find_pool(unsigned int tag)
 28 {
 29         struct list_head *lh;
 30         struct tcm_pool *pool;
 31 
 32         list_for_each(lh, &pool_list) {
 33                 pool = list_entry(lh, struct tcm_pool, list);
 34                 if (pool->tag == tag)
 35                         return pool;
 36         }
 37 
 38         return NULL;
 39 }
 40 
 41 /**
 42  * tcm_alloc - allocate memory from a TCM pool
 43  * @tag: tag of the pool to allocate memory from
 44  * @len: number of bytes to be allocated
 45  *
 46  * Allocate the requested number of bytes from the pool matching
 47  * the specified tag. Returns the address of the allocated memory
 48  * or zero on failure.
 49  */
 50 unsigned long tcm_alloc(unsigned int tag, size_t len)
 51 {
 52         unsigned long vaddr;
 53         struct tcm_pool *pool;
 54 
 55         pool = find_pool(tag);
 56         if (!pool)
 57                 return 0;
 58 
 59         vaddr = gen_pool_alloc(pool->pool, len);
 60         if (!vaddr)
 61                 return 0;
 62 
 63         return vaddr;
 64 }
 65 
 66 /**
 67  * tcm_free - free a block of memory to a TCM pool
 68  * @tag: tag of the pool to free memory to
 69  * @addr: address of the memory to be freed
 70  * @len: number of bytes to be freed
 71  *
 72  * Free the requested number of bytes at a specific address to the
 73  * pool matching the specified tag.
 74  */
 75 void tcm_free(unsigned int tag, unsigned long addr, size_t len)
 76 {
 77         struct tcm_pool *pool;
 78 
 79         pool = find_pool(tag);
 80         if (!pool)
 81                 return;
 82         gen_pool_free(pool->pool, addr, len);
 83 }
 84 
 85 /**
 86  * tcm_lookup_tag - find the tag matching an address
 87  * @p: memory address to lookup the tag for
 88  *
 89  * Find the tag of the tcm memory region that contains the
 90  * specified address. Returns %TCM_INVALID_TAG if no such
 91  * memory region could be found.
 92  */
 93 unsigned int tcm_lookup_tag(unsigned long p)
 94 {
 95         struct list_head *lh;
 96         struct tcm_pool *pool;
 97         unsigned long addr = (unsigned long) p;
 98 
 99         list_for_each(lh, &pool_list) {
100                 pool = list_entry(lh, struct tcm_pool, list);
101                 if (addr >= pool->start && addr < pool->end)
102                         return pool->tag;
103         }
104 
105         return TCM_INVALID_TAG;
106 }
107 
108 /**
109  * tcm_add_region - add a memory region to TCM pool list
110  * @reg: descriptor of region to be added
111  *
112  * Add a region of memory to the TCM pool list. Returns 0 on success.
113  */
114 int __init tcm_add_region(struct tcm_region *reg)
115 {
116         struct tcm_pool *pool;
117 
118         pool = kmalloc(sizeof(*pool), GFP_KERNEL);
119         if (!pool) {
120                 pr_err("Failed to alloc memory for TCM pool!\n");
121                 return -ENOMEM;
122         }
123 
124         pool->tag = reg->tag;
125         pool->start = reg->res.start;
126         pool->end = reg->res.end;
127 
128         /*
129          * 2^3 = 8 bytes granularity to allow for 64bit access alignment.
130          * -1 = NUMA node specifier.
131          */
132         pool->pool = gen_pool_create(3, -1);
133 
134         if (!pool->pool) {
135                 pr_err("Failed to create TCM pool!\n");
136                 kfree(pool);
137                 return -ENOMEM;
138         }
139 
140         if (gen_pool_add(pool->pool, reg->res.start,
141                          reg->res.end - reg->res.start + 1, -1)) {
142                 pr_err("Failed to add memory to TCM pool!\n");
143                 return -ENOMEM;
144         }
145         pr_info("Added %s TCM pool (%08x bytes @ %08x)\n",
146                 reg->res.name, reg->res.end - reg->res.start + 1,
147                 reg->res.start);
148 
149         list_add_tail(&pool->list, &pool_list);
150 
151         return 0;
152 }
153 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp