security: tf_driver: restore current task flags
[linux-3.10.git] / security / tf_driver / tf_comm.h
1 /**
2  * Copyright (c) 2011 Trusted Logic S.A.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
17  * MA 02111-1307 USA
18  */
19
20 #ifndef __TF_COMM_H__
21 #define __TF_COMM_H__
22
23 #include "tf_defs.h"
24 #include "tf_protocol.h"
25
26 /*----------------------------------------------------------------------------
27  * Misc
28  *----------------------------------------------------------------------------*/
29
30 void tf_set_current_time(struct tf_comm *comm);
31
32 /*
33  * Atomic accesses to 32-bit variables in the L1 Shared buffer
34  */
35 static inline u32 tf_read_reg32(const u32 *comm_buffer)
36 {
37         u32 result;
38
39         __asm__ __volatile__("@ tf_read_reg32\n"
40                 "ldrex %0, [%1]\n"
41                 : "=&r" (result)
42                 : "r" (comm_buffer)
43         );
44
45         return result;
46 }
47
48 static inline void tf_write_reg32(void *comm_buffer, u32 value)
49 {
50         u32 tmp;
51
52         __asm__ __volatile__("@ tf_write_reg32\n"
53                 "1:     ldrex %0, [%2]\n"
54                 "       strex %0, %1, [%2]\n"
55                 "       teq   %0, #0\n"
56                 "       bne   1b"
57                 : "=&r" (tmp)
58                 : "r" (value), "r" (comm_buffer)
59                 : "cc"
60         );
61 }
62
63 /*
64  * Atomic accesses to 64-bit variables in the L1 Shared buffer
65  */
66 static inline u64 tf_read_reg64(void *comm_buffer)
67 {
68         u64 result;
69
70         __asm__ __volatile__("@ tf_read_reg64\n"
71                 "ldrexd %0, [%1]\n"
72                 : "=&r" (result)
73                 : "r" (comm_buffer)
74         );
75
76         return result;
77 }
78
79 static inline void tf_write_reg64(void *comm_buffer, u64 value)
80 {
81         u64 tmp;
82
83         __asm__ __volatile__("@ tf_write_reg64\n"
84                 "1:     ldrexd %0, [%2]\n"
85                 "       strexd %0, %1, [%2]\n"
86                 "       teq    %0, #0\n"
87                 "       bne    1b"
88                 : "=&r" (tmp)
89                 : "r" (value), "r" (comm_buffer)
90                 : "cc"
91         );
92 }
93
94 /*----------------------------------------------------------------------------
95  * SMC operations
96  *----------------------------------------------------------------------------*/
97
98 /* RPC return values */
99 #define RPC_NO          0x00    /* No RPC to execute */
100 #define RPC_YIELD       0x01    /* Yield RPC */
101 #define RPC_NON_YIELD   0x02    /* non-Yield RPC */
102
103 int tf_rpc_execute(struct tf_comm *comm);
104
105 /*----------------------------------------------------------------------------
106  * Shared memory related operations
107  *----------------------------------------------------------------------------*/
108
109 #define L1_DESCRIPTOR_FAULT            (0x00000000)
110 #define L2_DESCRIPTOR_FAULT            (0x00000000)
111
112 #define L2_DESCRIPTOR_ADDR_MASK         (0xFFFFF000)
113
114 #define DESCRIPTOR_V13_12_MASK      (0x3 << PAGE_SHIFT)
115 #define DESCRIPTOR_V13_12_GET(a)    ((a & DESCRIPTOR_V13_12_MASK) >> PAGE_SHIFT)
116
117 struct tf_coarse_page_table *tf_alloc_coarse_page_table(
118         struct tf_coarse_page_table_allocation_context *alloc_context,
119         u32 type);
120
121 void tf_free_coarse_page_table(
122         struct tf_coarse_page_table_allocation_context *alloc_context,
123         struct tf_coarse_page_table *coarse_pg_table,
124         int force);
125
126 void tf_init_coarse_page_table_allocator(
127         struct tf_coarse_page_table_allocation_context *alloc_context);
128
129 void tf_release_coarse_page_table_allocator(
130         struct tf_coarse_page_table_allocation_context *alloc_context);
131
132 struct page *tf_l2_page_descriptor_to_page(u32 l2_page_descriptor);
133
134 u32 tf_get_l2_descriptor_common(u32 vaddr, struct mm_struct *mm);
135
136 void tf_cleanup_shared_memory(
137         struct tf_coarse_page_table_allocation_context *alloc_context,
138         struct tf_shmem_desc *shmem_desc,
139         u32 full_cleanup);
140
141 int tf_fill_descriptor_table(
142         struct tf_coarse_page_table_allocation_context *alloc_context,
143         struct tf_shmem_desc *shmem_desc,
144         u32 buffer,
145         struct vm_area_struct **vmas,
146         u32 descriptors[TF_MAX_COARSE_PAGES],
147         u32 buffer_size,
148         u32 *buffer_start_offset,
149         bool in_user_space,
150         u32 flags,
151         u32 *descriptor_count);
152
153 /*----------------------------------------------------------------------------
154  * Standard communication operations
155  *----------------------------------------------------------------------------*/
156
157 int tf_schedule_secure_world(struct tf_comm *comm);
158
159 int tf_send_receive(
160         struct tf_comm *comm,
161         union tf_command *command,
162         union tf_answer *answer,
163         struct tf_connection *connection,
164         bool bKillable);
165
166
167 /**
168  * get a pointer to the secure world description.
169  * This points directly into the L1 shared buffer
170  * and is valid only once the communication has
171  * been initialized
172  **/
173 u8 *tf_get_description(struct tf_comm *comm);
174
175 /*----------------------------------------------------------------------------
176  * Power management
177  *----------------------------------------------------------------------------*/
178
179 enum TF_POWER_OPERATION {
180         TF_POWER_OPERATION_HIBERNATE = 1,
181         TF_POWER_OPERATION_SHUTDOWN = 2,
182         TF_POWER_OPERATION_RESUME = 3,
183 };
184
185 int tf_pm_hibernate(struct tf_comm *comm);
186 int tf_pm_resume(struct tf_comm *comm);
187 int tf_pm_shutdown(struct tf_comm *comm);
188
189 int tf_power_management(struct tf_comm *comm,
190         enum TF_POWER_OPERATION operation);
191
192
193 /*----------------------------------------------------------------------------
194  * Communication initialization and termination
195  *----------------------------------------------------------------------------*/
196
197 int tf_init(struct tf_comm *comm);
198
199 void tf_terminate(struct tf_comm *comm);
200
201
202 #endif  /* __TF_COMM_H__ */