tizen 2.4 release
[kernel/linux-3.0.git] / drivers / gpu / arm / mali400 / r4p0_rel0 / common / mali_mem_validation.c
1 /*
2  * Copyright (C) 2011-2012 ARM Limited. All rights reserved.
3  * 
4  * This program is free software and is provided to you under the terms of the GNU General Public License version 2
5  * as published by the Free Software Foundation, and any use by you of this program is subject to the terms of such GNU licence.
6  * 
7  * A copy of the licence is included with the program, and can also be obtained from Free Software
8  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
9  */
10
11 #include "mali_mem_validation.h"
12 #include "mali_osk.h"
13 #include "mali_kernel_common.h"
14 /* MALI_SEC */
15 #if 0
16 #if defined(CONFIG_CPU_EXYNOS4212) || defined(CONFIG_CPU_EXYNOS4412)
17 #define MALI_SEC_MEM_VALIDATION
18 #include <linux/cma.h>
19 #include <plat/pd.h>
20 #include <linux/platform_device.h>
21 #endif
22 #endif
23
24 #define MALI_INVALID_MEM_ADDR 0xFFFFFFFF
25
26 typedef struct {
27         u32 phys_base;        /**< Mali physical base of the memory, page aligned */
28         u32 size;             /**< size in bytes of the memory, multiple of page size */
29 } _mali_mem_validation_t;
30
31 /* MALI_SEC */
32 #if defined(MALI_SEC_MEM_VALIDATION)
33 extern struct platform_device exynos4_device_pd[];
34 #endif
35
36 static _mali_mem_validation_t mali_mem_validator = { MALI_INVALID_MEM_ADDR, MALI_INVALID_MEM_ADDR };
37
38 _mali_osk_errcode_t mali_mem_validation_add_range(u32 start, u32 size)
39 {
40         /* MALI_SEC */
41 #if defined(MALI_SEC_MEM_VALIDATION)
42         struct cma_info mem_info;
43 #endif
44
45         /* Check that no other MEM_VALIDATION resources exist */
46         if (MALI_INVALID_MEM_ADDR != mali_mem_validator.phys_base) {
47                 MALI_PRINT_ERROR(("Failed to add frame buffer memory; another range is already specified\n"));
48                 return _MALI_OSK_ERR_FAULT;
49         }
50
51         /* MALI_SEC */
52 #if defined(MALI_SEC_MEM_VALIDATION)
53         if (cma_info(&mem_info, &exynos4_device_pd[PD_G3D].dev, "fimd")) {
54                 MALI_PRINT_ERROR(("Failed to get framebuffer information from CMA\n"));
55                 return _MALI_OSK_ERR_FAULT;
56         } else {
57                 start = mem_info.lower_bound;
58                 size = mem_info.total_size - mem_info.free_size;
59         }
60 #endif
61
62         /* Check restrictions on page alignment */
63         if ((0 != (start & (~_MALI_OSK_CPU_PAGE_MASK))) ||
64             (0 != (size & (~_MALI_OSK_CPU_PAGE_MASK)))) {
65                 MALI_PRINT_ERROR(("Failed to add frame buffer memory; incorrect alignment\n"));
66                 return _MALI_OSK_ERR_FAULT;
67         }
68
69         mali_mem_validator.phys_base = start;
70         mali_mem_validator.size = size;
71         MALI_DEBUG_PRINT(2, ("Memory Validator installed for Mali physical address base=0x%08X, size=0x%08X\n",
72                              mali_mem_validator.phys_base, mali_mem_validator.size));
73
74         return _MALI_OSK_ERR_OK;
75 }
76
77 _mali_osk_errcode_t mali_mem_validation_check(u32 phys_addr, u32 size)
78 {
79         if (phys_addr < (phys_addr + size)) { /* Don't allow overflow (or zero size) */
80                 if ((0 == ( phys_addr & (~_MALI_OSK_CPU_PAGE_MASK))) &&
81                     (0 == ( size & (~_MALI_OSK_CPU_PAGE_MASK)))) {
82                         if ((phys_addr          >= mali_mem_validator.phys_base) &&
83                             ((phys_addr + (size - 1)) >= mali_mem_validator.phys_base) &&
84                             (phys_addr          <= (mali_mem_validator.phys_base + (mali_mem_validator.size - 1))) &&
85                             ((phys_addr + (size - 1)) <= (mali_mem_validator.phys_base + (mali_mem_validator.size - 1))) ) {
86                                 MALI_DEBUG_PRINT(3, ("Accepted range 0x%08X + size 0x%08X (= 0x%08X)\n", phys_addr, size, (phys_addr + size - 1)));
87                                 return _MALI_OSK_ERR_OK;
88                         }
89                 }
90         }
91
92         MALI_PRINT_ERROR(("MALI PHYSICAL RANGE VALIDATION ERROR: The range supplied was: phys_base=0x%08X, size=0x%08X\n", phys_addr, size));
93
94         return _MALI_OSK_ERR_FAULT;
95 }