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

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

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

~ [ 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