uOFW
Reverse engineered PSP kernel 6.60.
Loading...
Searching...
No Matches
memory.h
1/* Copyright (C) 2011, 2012, 2013 The uOFW team
2 See the file COPYING for copying permission.
3*/
4
5#ifndef COMMON_INCLUDED
6# error "Only include common_imp.h or common_header.h!"
7#endif
8
9/* Segment base addresses and sizes */
10#define KU0_BASE 0x00000000 /* cached - user/supervisor/kernel */
11#define KU1_BASE 0x40000000 /* uncached - user/supervisor/kernel */
12
13#define K0_BASE 0x80000000 /* cached - kernel */
14#define K0_SIZE 0x20000000 /* 512 MB */
15#define K1_BASE 0xA0000000 /* uncached - kernel */
16#define K1_SIZE 0x20000000 /* 512 MB */
17#define K2_BASE 0xC0000000 /* cached - supervisor/kernel */
18#define K2_SIZE 0x20000000 /* 512 MB */
19#define K3_BASE 0xE0000000 /* cached - kernel */
20#define K3_SIZE 0x20000000 /* 512 MB */
21
22/* Scratchpad segment base address and size */
23#define SCE_SCRATCHPAD_ADDR 0x00010000 /* Physical memory */
24#define SCE_SCRATCHPAD_ADDR_KU0 0x00010000 /* KU segment 0 (cached) */
25#define SCE_SCRATCHPAD_ADDR_KU1 0x40010000 /* KU segment 1 (uncached) */
26#define SCE_SCRATCHPAD_ADDR_K0 0x80010000 /* K0 segment (cached) */
27#define SCE_SCRATCHPAD_SIZE 0x00004000 /* 16 KB */
28
29#define REBOOT_BASE_ADDR_K0 0x88600000 /* K0 segment (cached) */
30
31/* Userspace memory base address and size */
32#define SCE_USERSPACE_ADDR_KU0 0x08800000 /* KU segment 0 (cached) */
33#define SCE_USERSPACE_ADDR_KU1 0x48800000 /* KU segment 1 (uncached) */
34#define SCE_USERSPACE_ADDR_K0 0x88800000 /* K0 segment (cached) */
35#define SCE_USERSPACE_ADDR_K1 0xA8800000 /* K1 segment (uncached) */
36#define SCE_USERSPACE_SIZE 0x01800000 /* 24 MB */
37
38#define SCE_USERSPACE_GAME_ADDR_K0 0x88900000 /* K0 segment (chached) */
39
40#define UCACHED(ptr) (void *)((u32)(void *)(ptr) & 0x1FFFFFFF) /* KU0 - cached. */
41#define KCACHED(ptr) (void *)(K0_BASE | ((u32)(void *)(ptr) & 0x1FFFFFFF)) /* K0 - cached */
42#define KUNCACHED(ptr) (void *)(K1_BASE | ((u32)(void *)(ptr) & 0x1FFFFFFF)) /* K1 - uncached */
43#define UUNCACHED(ptr) (void *)(KU1_BASE | ((u32)(void *)(ptr) & 0x1FFFFFFF)) /* KU1 - uncached */
44
45/* Alignment */
46#define UPALIGN256(v) (((v) + 0xFF) & 0xFFFFFF00)
47#define UPALIGN64(v) (((v) + 0x3F) & 0xFFFFFFC0)
48#define UPALIGN16(v) (((v) + 0xF) & 0xFFFFFFF0)
49#define UPALIGN8(v) (((v) + 0x7) & 0xFFFFFFF8)
50#define UPALIGN4(v) (((v) + 0x3) & 0xFFFFFFFC)
51
52#define ISALIGN4(v) (((u32)(v) & 0x03) == 0)
53
54/* Clear memory partitioned in 1-Byte blocks. */
55static inline void pspClearMemory8(void *ptr, int size) {
56 int i;
57 for (i = 0; i < size; i++)
58 ((u8 *)ptr)[i] = 0;
59}
60
61/* Clear memory partitioned in 2-Byte blocks. */
62static inline void pspClearMemory16(void *ptr, int size) {
63 int i;
64 for (i = 0; i < (int)(size / sizeof(u16)); i++)
65 ((u16 *)ptr)[i] = 0;
66}
67
68/* Clear memory partitioned in 4-Byte blocks. */
69static inline void pspClearMemory32(void *ptr, int size) {
70 int i;
71 for (i = 0; i < (int)(size / sizeof(u32)); i++)
72 ((u32 *)ptr)[i] = 0;
73}
74
75// TODO: Remove size handling in above's clear functions.
76// Replace instances of above functions with this one.
77static inline void pspClearMemory(void *ptr, int size) {
78 if (size % 4 == 0)
79 pspClearMemory32(ptr, size / 4);
80 else if (size % 2 == 0)
81 pspClearMemory16(ptr, size / 2);
82 else
83 pspClearMemory8(ptr, size);
84}
85
86static inline void memsetInline(void *s, int c, SceSize size)
87{
88 u32 i;
89 for (i = 0; i < size; i++)
90 {
91 *(u8 *)(s + i) = (u8)c;
92 }
93}
94
95static inline void memcpyInline(void *dest, const void *src, SceSize size)
96{
97 u32 i;
98 for (i = 0; i < size; i++)
99 {
100 *(u8 *)(dest + i) = *(u8 *)(src + i);
101 }
102}
103
104/* If we believe in the sysmem NIDs, 04g+ seem to have a "L2" cache
105 * we can send commands to through this address */
106#define L2_CACHE_CMD (vu32*)0xA7F00000
107
108static inline void pspL2CacheWriteback0(void *ptr, u8 align) {
109 *L2_CACHE_CMD = 0xA0000000 | ((u32)ptr & 0x07FFFFC0) | align;
110 *L2_CACHE_CMD;
111}
112
113static inline void pspL2CacheWriteback1(void *ptr, u8 align) {
114 *L2_CACHE_CMD = 0xA0000000 | 0x08000000 | ((u32)ptr & 0x07FFFFC0) | align;
115 *L2_CACHE_CMD;
116}
117
118static inline void pspL2CacheWriteback10(void *ptr, u8 align) {
119 *L2_CACHE_CMD = 0xA0000000 | 0x08000000 | ((u32)ptr & 0x07FFFFC0) | align;
120 *L2_CACHE_CMD = 0xA0000000 | ((u32)ptr & 0x07FFFFC0) | align;
121 *L2_CACHE_CMD;
122}
123