1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - OS - include
3   File:     protectionRegion.h
4 
5   Copyright 2003-2008 Nintendo.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2008-09-18#$
14   $Rev: 8573 $
15   $Author: okubata_ryoma $
16 
17  *---------------------------------------------------------------------------*/
18 
19 #ifndef NITRO_OS_PROTECTIONREGION_H_
20 #define NITRO_OS_PROTECTIONREGION_H_
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 #include <nitro/misc.h>
27 #include <nitro/types.h>
28 
29 #define OS_PROTECTION_REGION_NUM	8
30 
31 #define OS_PR0_ACCESS_MASK   (HW_C5_PERMIT_MASK << HW_C5_PR0_SFT)
32 #define OS_PR0_ACCESS_NA     (HW_C5_PERMIT_NA   << HW_C5_PR0_SFT)
33 #define OS_PR0_ACCESS_RW     (HW_C5_PERMIT_RW   << HW_C5_PR0_SFT)
34 #define OS_PR0_ACCESS_RO     (HW_C5_PERMIT_RO   << HW_C5_PR0_SFT)
35 
36 #define OS_PR1_ACCESS_MASK   (HW_C5_PERMIT_MASK << HW_C5_PR1_SFT)
37 #define OS_PR1_ACCESS_NA     (HW_C5_PERMIT_NA   << HW_C5_PR1_SFT)
38 #define OS_PR1_ACCESS_RW     (HW_C5_PERMIT_RW   << HW_C5_PR1_SFT)
39 #define OS_PR1_ACCESS_RO     (HW_C5_PERMIT_RO   << HW_C5_PR1_SFT)
40 
41 #define OS_PR2_ACCESS_MASK   (HW_C5_PERMIT_MASK << HW_C5_PR2_SFT)
42 #define OS_PR2_ACCESS_NA     (HW_C5_PERMIT_NA   << HW_C5_PR2_SFT)
43 #define OS_PR2_ACCESS_RW     (HW_C5_PERMIT_RW   << HW_C5_PR2_SFT)
44 #define OS_PR2_ACCESS_RO     (HW_C5_PERMIT_RO   << HW_C5_PR2_SFT)
45 
46 #define OS_PR3_ACCESS_MASK   (HW_C5_PERMIT_MASK << HW_C5_PR3_SFT)
47 #define OS_PR3_ACCESS_NA     (HW_C5_PERMIT_NA   << HW_C5_PR3_SFT)
48 #define OS_PR3_ACCESS_RW     (HW_C5_PERMIT_RW   << HW_C5_PR3_SFT)
49 #define OS_PR3_ACCESS_RO     (HW_C5_PERMIT_RO   << HW_C5_PR3_SFT)
50 
51 #define OS_PR4_ACCESS_MASK   (HW_C5_PERMIT_MASK << HW_C5_PR4_SFT)
52 #define OS_PR4_ACCESS_NA     (HW_C5_PERMIT_NA   << HW_C5_PR4_SFT)
53 #define OS_PR4_ACCESS_RW     (HW_C5_PERMIT_RW   << HW_C5_PR4_SFT)
54 #define OS_PR4_ACCESS_RO     (HW_C5_PERMIT_RO   << HW_C5_PR4_SFT)
55 
56 #define OS_PR5_ACCESS_MASK   (HW_C5_PERMIT_MASK << HW_C5_PR5_SFT)
57 #define OS_PR5_ACCESS_NA     (HW_C5_PERMIT_NA   << HW_C5_PR5_SFT)
58 #define OS_PR5_ACCESS_RW     (HW_C5_PERMIT_RW   << HW_C5_PR5_SFT)
59 #define OS_PR5_ACCESS_RO     (HW_C5_PERMIT_RO   << HW_C5_PR5_SFT)
60 
61 #define OS_PR6_ACCESS_MASK   (HW_C5_PERMIT_MASK << HW_C5_PR6_SFT)
62 #define OS_PR6_ACCESS_NA     (HW_C5_PERMIT_NA   << HW_C5_PR6_SFT)
63 #define OS_PR6_ACCESS_RW     (HW_C5_PERMIT_RW   << HW_C5_PR6_SFT)
64 #define OS_PR6_ACCESS_RO     (HW_C5_PERMIT_RO   << HW_C5_PR6_SFT)
65 
66 #define OS_PR7_ACCESS_MASK   (HW_C5_PERMIT_MASK << HW_C5_PR7_SFT)
67 #define OS_PR7_ACCESS_NA     (HW_C5_PERMIT_NA   << HW_C5_PR7_SFT)
68 #define OS_PR7_ACCESS_RW     (HW_C5_PERMIT_RW   << HW_C5_PR7_SFT)
69 #define OS_PR7_ACCESS_RO     (HW_C5_PERMIT_RO   << HW_C5_PR7_SFT)
70 
71 //================================================================================
72 //               Instruction Cache for each protection region
73 //================================================================================
74 /*---------------------------------------------------------------------------*
75   Name:         OS_EnableICacheForProtectionRegion
76                 OS_DisableICacheForProtectionRegion
77 
78   Description:  enable/disable instruction cache for each protection region
79 
80   Arguments:    flags :  Each bit shows the region to be enable/disable.
81                            bit0(LSB) ... protection region 0
82                            bit1      ... protection region 1
83                              :                  :
84                            bit7      ... protection region 7
85 
86   Returns:      None
87  *---------------------------------------------------------------------------*/
88 void    OS_EnableICacheForProtectionRegion(register u32 flags);
89 void    OS_DisableICacheForProtectionRegion(register u32 flags);
90 
91 
92 /*---------------------------------------------------------------------------*
93   Name:         OS_GetICacheEnableFlagsForProtectionRegion
94 
95   Description:  get instruction cache status for each protection region
96 
97   Arguments:    None
98 
99   Returns:      instruction cache status.
100                 bit for each protection region, means 1... enable, 0... disable
101 
102                            bit0(LSB) ... protection region 0
103                            bit1      ... protection region 1
104                              :                  :
105                            bit7      ... protection region 7
106 
107  *---------------------------------------------------------------------------*/
108 u32     OS_GetICacheEnableFlagsForProtectionRegion(void);
109 
110 /*---------------------------------------------------------------------------*
111   Name:         OS_SetIPermissionsForProtectionRegion
112 
113   Description:  set instruction access limitation for each protection region
114 
115   Arguments:    setMask : set mask bit, which is or-ed by mask.
116                 flags   : flag bit, which is or-ed by access limitation.
117 
118                    (mask)
119                           OS_PRn_ACCESS_MASK (n=0-7)
120 
121                    (access limitation)
122                           OS_PRn_ACCESS_NA    no permission to access
123                           OS_PRn_ACCESS_RW    read and write permission
124                           OS_PRn_ACCESS_RO    read only permission
125                           (n=0-7)
126 
127                 ex.
128                     In case to set region3 readonly and region6 no permission, call
129                     OS_SetIPermissionsForProtectionRegion(
130                              OS_PR3_ACCESS_MASK | OS_PR6_ACCESS_MASK,
131                              OS_PR3_ACCESS_RO | OS_PR6_ACCESS_NA );
132 
133   Returns:      None
134  *---------------------------------------------------------------------------*/
135 void    OS_SetIPermissionsForProtectionRegion(u32 setMask, u32 flags);
136 
137 /*---------------------------------------------------------------------------*
138   Name:         OS_GetIPermissionsForProtectionRegion
139 
140   Description:  get instruction access limitation for each protection region
141 
142   Arguments:    None
143 
144   Returns:      Return value & OS_PRn_ACCESS_MASK (n=0-7) shows access limitation.
145 
146                    (access limitation)
147                           OS_PRn_ACCESS_NA    no permission to access
148                           OS_PRn_ACCESS_RW    read and write permission
149                           OS_PRn_ACCESS_RO    read only permission
150                           (n=0-7)
151 
152  *---------------------------------------------------------------------------*/
153 u32     OS_GetIPermissionsForProtectionRegion(void);
154 
155 
156 //================================================================================
157 //               Data Cache for each protection region
158 //================================================================================
159 /*---------------------------------------------------------------------------*
160   Name:         OS_EnableDCacheForProtectionRegion
161                 OS_DisableDCacheForProtectionRegion
162 
163   Description:  enable/disable data cache for each protection region
164 
165   Arguments:    flags :  Each bit shows the region to be enable/disable.
166                            bit0(LSB) ... protection region 0
167                            bit1      ... protection region 1
168                              :                  :
169                            bit7      ... protection region 7
170 
171   Returns:      None
172  *---------------------------------------------------------------------------*/
173 void    OS_EnableDCacheForProtectionRegion(u32 flags);
174 void    OS_DisableDCacheForProtectionRegion(u32 flags);
175 
176 /*---------------------------------------------------------------------------*
177   Name:         OS_GetDCacheEnableFlagsForProtectionRegion
178 
179   Description:  get data cache status for each protection region
180 
181   Arguments:    None
182 
183   Returns:      data cache status.
184                 bit for each protection region, means 1... enable, 0... disable
185 
186                            bit0(LSB) ... protection region 0
187                            bit1      ... protection region 1
188                              :                  :
189                            bit7      ... protection region 7
190 
191  *---------------------------------------------------------------------------*/
192 u32     OS_GetDCacheEnableFlagsForProtectionRegion(void);
193 
194 /*---------------------------------------------------------------------------*
195   Name:         OS_SetDPermissionsForProtectionRegion
196 
197   Description:  set data access limitation for each protection region
198 
199   Arguments:    setMask : set mask bit, which is or-ed by mask.
200                 flags   : flag bit, which is or-ed by access limitation.
201 
202                    (mask)
203                           OS_PRn_ACCESS_MASK (n=0-7)
204 
205                    (access limitation)
206                           OS_PRn_ACCESS_NA    no permission to access
207                           OS_PRn_ACCESS_RW    read and write permission
208                           OS_PRn_ACCESS_RO    read only permission
209                           (n=0-7)
210 
211                 ex.
212                     In case to set region3 readonly and region6 no permission, call
213                     OS_SetDPermissionsForProtectionRegion(
214                              OS_PR3_ACCESS_MASK | OS_PR6_ACCESS_MASK,
215                              OS_PR3_ACCESS_RO | OS_PR6_ACCESS_NA );
216 
217   Returns:      None
218  *---------------------------------------------------------------------------*/
219 void    OS_SetDPermissionsForProtectionRegion(u32 setMask, u32 flags);
220 
221 /*---------------------------------------------------------------------------*
222   Name:         OS_GetDPermissionsForProtectionRegion
223 
224   Description:  get data access limitation for each protection region
225 
226   Arguments:    None
227 
228   Returns:      Return value & OS_PRn_ACCESS_MASK (n=0-7) shows access limitation.
229 
230                    (access limitation)
231                           OS_PRn_ACCESS_NA    no permission to access
232                           OS_PRn_ACCESS_RW    read and write permission
233                           OS_PRn_ACCESS_RO    read only permission
234                           (n=0-7)
235 
236  *---------------------------------------------------------------------------*/
237 u32     OS_GetDPermissionsForProtectionRegion(void);
238 
239 
240 //================================================================================
241 //               Write buffer for each protection region
242 //================================================================================
243 /*---------------------------------------------------------------------------*
244   Name:         OS_EnableWriteBufferForProtectionRegion
245                 OS_DisableWriteBufferForProtectionRegion
246 
247   Description:  enable/disable write buffer for each protection region
248 
249   Arguments:    flags :  Each bit shows the region to be enable/disable.
250                            bit0(LSB) ... protection region 0
251                            bit1      ... protection region 1
252                              :                  :
253                            bit7      ... protection region 7
254 
255   Returns:      None
256  *---------------------------------------------------------------------------*/
257 void    OS_EnableWriteBufferForProtectionRegion(u32 flags);
258 void    OS_DisableWriteBufferForProtectionRegion(u32 flags);
259 
260 /*---------------------------------------------------------------------------*
261   Name:         OS_GetWriteBufferEnableFlagsForProtectionRegion
262 
263   Description:  get write buffer status for each protection region
264 
265   Arguments:    None
266 
267   Returns:      write buffer status.
268                 bit for each protection region, means 1... enable, 0... disable
269 
270                            bit0(LSB) ... protection region 0
271                            bit1      ... protection region 1
272                              :                  :
273                            bit7      ... protection region 7
274 
275  *---------------------------------------------------------------------------*/
276 u32     OS_GetWriteBufferEnableFlagsForProtectionRegion(u32 flags);
277 
278 
279 
280 //================================================================================
281 //          protection region
282 //================================================================================
283 
284 typedef enum
285 {
286     OSi_PR_BASE_MASK_4KB = 0xfffff000,
287     OSi_PR_BASE_MASK_8KB = 0xffffe000,
288     OSi_PR_BASE_MASK_16KB = 0xffffc000,
289     OSi_PR_BASE_MASK_32KB = 0xffff8000,
290     OSi_PR_BASE_MASK_64KB = 0xffff0000,
291     OSi_PR_BASE_MASK_128KB = 0xfffe0000,
292     OSi_PR_BASE_MASK_256KB = 0xfffc0000,
293     OSi_PR_BASE_MASK_512KB = 0xfff80000,
294     OSi_PR_BASE_MASK_1MB = 0xfff00000,
295     OSi_PR_BASE_MASK_2MB = 0xffe00000,
296     OSi_PR_BASE_MASK_4MB = 0xffc00000,
297     OSi_PR_BASE_MASK_8MB = 0xff800000,
298     OSi_PR_BASE_MASK_16MB = 0xff000000,
299     OSi_PR_BASE_MASK_32MB = 0xfe000000,
300     OSi_PR_BASE_MASK_64MB = 0xfc000000,
301     OSi_PR_BASE_MASK_128MB = 0xf8000000,
302     OSi_PR_BASE_MASK_256MB = 0xf0000000,
303     OSi_PR_BASE_MASK_512MB = 0xe0000000,
304     OSi_PR_BASE_MASK_1GB = 0xc0000000,
305     OSi_PR_BASE_MASK_2GB = 0x80000000,
306     OSi_PR_BASE_MASK_4GB = 0x00000000
307 }
308 OSiProtectionRegionBaseMask;
309 
310 #define OS_PR_SIZE_4KB        HW_C6_PR_4KB
311 #define OS_PR_SIZE_8KB        HW_C6_PR_8KB
312 #define OS_PR_SIZE_16KB       HW_C6_PR_16KB
313 #define OS_PR_SIZE_32KB       HW_C6_PR_32KB
314 #define OS_PR_SIZE_64KB       HW_C6_PR_64KB
315 #define OS_PR_SIZE_128KB      HW_C6_PR_128KB
316 #define OS_PR_SIZE_256KB      HW_C6_PR_256KB
317 #define OS_PR_SIZE_512KB      HW_C6_PR_512KB
318 #define OS_PR_SIZE_1MB        HW_C6_PR_1MB
319 #define OS_PR_SIZE_2MB        HW_C6_PR_2MB
320 #define OS_PR_SIZE_4MB        HW_C6_PR_4MB
321 #define OS_PR_SIZE_8MB        HW_C6_PR_8MB
322 #define OS_PR_SIZE_16MB       HW_C6_PR_16MB
323 #define OS_PR_SIZE_32MB       HW_C6_PR_32MB
324 #define OS_PR_SIZE_64MB       HW_C6_PR_64MB
325 #define OS_PR_SIZE_128MB      HW_C6_PR_128MB
326 #define OS_PR_SIZE_256MB      HW_C6_PR_256MB
327 #define OS_PR_SIZE_512MB      HW_C6_PR_512MB
328 #define OS_PR_SIZE_1GB        HW_C6_PR_1GB
329 #define OS_PR_SIZE_2GB        HW_C6_PR_2GB
330 #define OS_PR_SIZE_4GB        HW_C6_PR_4GB
331 
332 // convert OS_PR_SIZE_xxx to number.
333 // OS_PR_SIZE_4KB = 0, OS_PR_SIZE_8KB = 1, ...
334 #define OSi_PRDEF_TO_VAL(prDef) (((prDef)-0x16) >> 1)
335 
336 // convert number to OS_PR_SIZE_xxx
337 // 0 = OS_PR_SIZE_4KB, 1 = OS_PR_SIZE_8KB, ...
338 #define OSi_VAL_TO_PRDEF(val)   (((val)<<1) + 0x16)
339 
340 /*---------------------------------------------------------------------------*
341   Name:         OSi_CalcPRParam
342 
343   Description:  set up parameter of protection region
344 
345   Arguments:    address  :   base address
346                 size     :   HW_C6_PR_xxx (xxx=4KB, ,... 4GB)
347                 mask     ;   OSi_PR_BASE_MASK_xxx (xxx=4KB, ... 4GB)
348 
349   Returns:      parameter to set protection region
350  *---------------------------------------------------------------------------*/
OSi_CalcPRParam(u32 address,u32 size,u32 mask)351 static inline u32 OSi_CalcPRParam(u32 address, u32 size, u32 mask)
352 {
353 #ifdef SDK_DEBUG
354     SDK_ASSERT((address & mask) == address);
355 #endif
356     return ((address & mask) | size);
357 }
358 
359 /*---------------------------------------------------------------------------*
360   Name:         OS_SetProtectionRegionParam
361 
362   Description:  set up parameter of protection region
363 
364   Arguments:    regionNo :   region number
365                 param    :   parameter ( created from base address and size )
366 
367   Returns:      None
368  *---------------------------------------------------------------------------*/
369 void    OS_SetProtectionRegionParam( int regionNo, u32 param );
370 
371 /*---------------------------------------------------------------------------*
372   Name:         OS_SetProtectionRegion*
373 
374   Description:  set up parameter of each protection region
375 
376   Arguments:    param    :   parameter ( created from base address and size )
377 
378   Returns:      None
379  *---------------------------------------------------------------------------*/
380 void    OS_SetProtectionRegion0(u32 param);
381 void    OS_SetProtectionRegion1(u32 param);
382 void    OS_SetProtectionRegion2(u32 param);
383 void    OS_SetProtectionRegion3(u32 param);
384 void    OS_SetProtectionRegion4(u32 param);
385 void    OS_SetProtectionRegion5(u32 param);
386 void    OS_SetProtectionRegion6(u32 param);
387 void    OS_SetProtectionRegion7(u32 param);
388 
389 /*---------------------------------------------------------------------------*
390   Name:         OS_GetProtectionRegionParam
391 
392   Description:  get parameter of protection region
393 
394   Arguments:    regionNo :   region number
395 
396   Returns:      parameter of protection region ( created from base address and size )
397  *---------------------------------------------------------------------------*/
398 u32     OS_GetProtectionRegionParam( int regionNo );
399 
400 /*---------------------------------------------------------------------------*
401   Name:         OS_GetProtectionRegion*
402 
403   Description:  get parameter of each protection region
404 
405   Arguments:    None
406 
407   Returns:      parameter of protection region ( created from base address and size )
408  *---------------------------------------------------------------------------*/
409 u32     OS_GetProtectionRegion0(void);
410 u32     OS_GetProtectionRegion1(void);
411 u32     OS_GetProtectionRegion2(void);
412 u32     OS_GetProtectionRegion3(void);
413 u32     OS_GetProtectionRegion4(void);
414 u32     OS_GetProtectionRegion5(void);
415 u32     OS_GetProtectionRegion6(void);
416 u32     OS_GetProtectionRegion7(void);
417 
418 /*---------------------------------------------------------------------------*
419   Name:         OS_SetProtectionRegion
420 
421   Description:  set up parameter of protection region
422 
423   Arguments:    regionNo :   region number
424                 address  :   base address
425                 sizeStr  :   string that stands for size
426 
427                regionNo must be immidiate number, from 0 to 7.
428                address can be valiable
429                sizeStr is 4KB or 8KB or 16KB or ... 4GB
430 
431                ex. OSi_SetProtectionRegion( 3, 0x2000000, 4KB );
432 
433   Returns:      None
434  *---------------------------------------------------------------------------*/
435 #define OS_SetProtectionRegion( regionNo, address, sizeStr )  \
436   OS_SetProtectionRegionParam( (regionNo), \
437 							   OSi_CalcPRParam( (address), HW_C6_PR_##sizeStr, OSi_PR_BASE_MASK_##sizeStr ) \
438                               | HW_C6_PR_ENABLE )
439 
440 /*---------------------------------------------------------------------------*
441   Name:         OS_SetProtectionRegionEx
442 
443   Description:  set up parameter of protection region
444 
445   Arguments:    regionNo :   region number
446                 address  :   base address
447                 sizeDef  :   size value.
448 
449                 regionNo must be immidiate number, from 0 to 7.
450                 sizeStr is OS_PR_SIZE_xxx  (xxx=4KB, 8KB, 16KB, ..., 4GB )
451 
452                 ex. OSi_SetProtectionRegion( 3, 0x2000000, OS_PR_SIZE_4KB );
453 
454   Returns:      None
455  *---------------------------------------------------------------------------*/
456 void OS_SetProtectionRegionEx( int regionNo, u32 address, u32 sizeDef );
457 
458 /*---------------------------------------------------------------------------*
459   Name:         OS_GetProtectionRegionAddress
460 
461   Description:  get parameter of protection region
462 
463   Arguments:    regionNo :   region number
464 
465   Returns:      base address of protection region.
466  *---------------------------------------------------------------------------*/
467 #define OS_GetProtectionRegionAddress( regionNo )  ( OS_GetProtectionRegionParam((regionNo)) & HW_C6_PR_BASE_MASK )
468 
469 /*---------------------------------------------------------------------------*
470   Name:         OS_GetProtectionRegionSize
471 
472   Description:  get parameter of protection region
473 
474   Arguments:    regionNo :   region number
475 
476   Returns:      size parameter of protection region.
477                 parameter is HW_C6_PR_SIZE_xxx, xxx is 4KB or 8KB or ... 4GB
478  *---------------------------------------------------------------------------*/
479 #define OS_GetProtectionRegionSize( regionNo )  ( OS_GetProtectionRegionParam((regionNo)) & HW_C6_PR_SIZE_MASK )
480 
481 /*---------------------------------------------------------------------------*
482   Name:         OS_DumpProtectionRegion
483 
484   Description:  dump protection region list (for DEBUG)
485 
486   Arguments:    None
487 
488   Returns:      None
489  *---------------------------------------------------------------------------*/
490 #ifndef SDK_FINALROM
491 void OS_DumpProtectionRegion( void );
492 #else
493 #define OS_DumpProtectionRegion()  ((void)0)
494 #endif
495 
496 #ifdef __cplusplus
497 } /* extern "C" */
498 #endif
499 
500 /* NITRO_OS_PROTECTIONREGION_H_ */
501 #endif
502