aboutsummaryrefslogtreecommitdiff
path: root/drivers/video/arm/v5xx/resource/mve_rsrc_mem_cache.c
blob: c8a497da2c68d5fde5ba150102d3eeca0a18585e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * (C) COPYRIGHT ARM Limited. All rights reserved.
 *
 * This program is free software and is provided to you under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation, and any use by you of this program is subject to the terms
 * of such GNU licence.
 *
 * A copy of the licence is included with the program, and can also be obtained
 * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 */

#ifdef EMULATOR
#include "emulator_userspace.h"
#else
#include <linux/export.h>
#include <linux/slab.h>
#include <linux/sched.h>
#endif

#include "mve_rsrc_mem_frontend.h"
#include "mve_rsrc_mem_cache.h"
#include "mve_rsrc_log.h"

#define MAX_MEMORY_CACHE_SMALL      32
#define MAX_MEMORY_CACHE_MEDIUM     128
#define MAX_MEMORY_CACHE_LARGE      1024

static struct kmem_cache *mve_small_cachep;
static struct kmem_cache *mve_medium_cachep;
static struct kmem_cache *mve_large_cachep;

void mve_rsrc_mem_cache_init(void)
{
    mve_small_cachep = kmem_cache_create("mve_small_cache",
                                         MAX_MEMORY_CACHE_SMALL,
                                         0,
                                         SLAB_HWCACHE_ALIGN,
                                         NULL);
    mve_medium_cachep = kmem_cache_create("mve_medium_cache",
                                          MAX_MEMORY_CACHE_MEDIUM,
                                          0,
                                          SLAB_HWCACHE_ALIGN,
                                          NULL);
    mve_large_cachep = kmem_cache_create("mve_large_cache",
                                         MAX_MEMORY_CACHE_LARGE,
                                         0,
                                         SLAB_HWCACHE_ALIGN,
                                         NULL);
}

void mve_rsrc_mem_cache_deinit(void)
{
    if (NULL != mve_small_cachep)
    {
        kmem_cache_destroy(mve_small_cachep);
    }
    if (NULL != mve_medium_cachep)
    {
        kmem_cache_destroy(mve_medium_cachep);
    }
    if (NULL != mve_large_cachep)
    {
        kmem_cache_destroy(mve_large_cachep);
    }
}

static struct kmem_cache *mve_rsrc_get_cachep(uint32_t size)
{
    struct kmem_cache *cachep = NULL;

    if (size <= MAX_MEMORY_CACHE_SMALL)
    {
        cachep = mve_small_cachep;
    }
    else if (size <= MAX_MEMORY_CACHE_MEDIUM)
    {
        cachep = mve_medium_cachep;
    }
    else if (size <= MAX_MEMORY_CACHE_LARGE)
    {
        cachep = mve_large_cachep;
    }
    else
    {
        MVE_LOG_PRINT(&mve_rsrc_log, MVE_LOG_ERROR, "No allocation slab found. size=%u.", size);
    }

    return cachep;
}

void *mve_rsrc_mem_cache_alloc(uint32_t size, uint32_t flags)
{
    void *ptr;
    struct kmem_cache *cachep;

    cachep = mve_rsrc_get_cachep(size);

    if (NULL == cachep)
    {
        ptr = vmalloc(size);
    }
    else
    {
        ptr = kmem_cache_alloc(cachep, flags);
        if (ptr)
        {
            memset(ptr, 0, size);
        }
    }
    return ptr;
}

void mve_rsrc_mem_cache_free(void *ptr, uint32_t size)
{
    struct kmem_cache *cachep;

    cachep = mve_rsrc_get_cachep(size);

    if (NULL == cachep)
    {
        vfree(ptr);
    }
    else
    {
        kmem_cache_free(cachep, ptr);
    }
}
EXPORT_SYMBOL(mve_rsrc_mem_cache_alloc);
EXPORT_SYMBOL(mve_rsrc_mem_cache_free);