1433d6423SLionel Sambuc #include <minix/cpufeature.h> 2433d6423SLionel Sambuc 3433d6423SLionel Sambuc #include <minix/type.h> 4433d6423SLionel Sambuc #include <assert.h> 5433d6423SLionel Sambuc #include "kernel/kernel.h" 6433d6423SLionel Sambuc #include "arch_proto.h" 7433d6423SLionel Sambuc #include <machine/cpu.h> 8433d6423SLionel Sambuc #include <arm/armreg.h> 9433d6423SLionel Sambuc 10433d6423SLionel Sambuc #include <string.h> 11433d6423SLionel Sambuc #include <minix/type.h> 12433d6423SLionel Sambuc 13433d6423SLionel Sambuc /* These are set/computed in kernel.lds. */ 14433d6423SLionel Sambuc extern char _kern_vir_base, _kern_phys_base, _kern_size; 15433d6423SLionel Sambuc 16433d6423SLionel Sambuc /* Retrieve the absolute values to something we can use. */ 17433d6423SLionel Sambuc static phys_bytes kern_vir_start = (phys_bytes) &_kern_vir_base; 18433d6423SLionel Sambuc static phys_bytes kern_phys_start = (phys_bytes) &_kern_phys_base; 19433d6423SLionel Sambuc static phys_bytes kern_kernlen = (phys_bytes) &_kern_size; 20433d6423SLionel Sambuc 21433d6423SLionel Sambuc /* page directory we can use to map things */ 22433d6423SLionel Sambuc static u32_t pagedir[4096] __aligned(16384); 23433d6423SLionel Sambuc 24433d6423SLionel Sambuc void print_memmap(kinfo_t *cbi) 25433d6423SLionel Sambuc { 26433d6423SLionel Sambuc int m; 27433d6423SLionel Sambuc assert(cbi->mmap_size < MAXMEMMAP); 28433d6423SLionel Sambuc for(m = 0; m < cbi->mmap_size; m++) { 29433d6423SLionel Sambuc phys_bytes addr = cbi->memmap[m].mm_base_addr, endit = cbi->memmap[m].mm_base_addr + cbi->memmap[m].mm_length; 30433d6423SLionel Sambuc printf("%08lx-%08lx ",addr, endit); 31433d6423SLionel Sambuc } 32433d6423SLionel Sambuc printf("\nsize %08lx\n", cbi->mmap_size); 33433d6423SLionel Sambuc } 34433d6423SLionel Sambuc 35433d6423SLionel Sambuc void cut_memmap(kinfo_t *cbi, phys_bytes start, phys_bytes end) 36433d6423SLionel Sambuc { 37433d6423SLionel Sambuc int m; 38433d6423SLionel Sambuc phys_bytes o; 39433d6423SLionel Sambuc 40433d6423SLionel Sambuc if((o=start % ARM_PAGE_SIZE)) 41433d6423SLionel Sambuc start -= o; 42433d6423SLionel Sambuc if((o=end % ARM_PAGE_SIZE)) 43433d6423SLionel Sambuc end += ARM_PAGE_SIZE - o; 44433d6423SLionel Sambuc 45433d6423SLionel Sambuc assert(kernel_may_alloc); 46433d6423SLionel Sambuc 47433d6423SLionel Sambuc for(m = 0; m < cbi->mmap_size; m++) { 48433d6423SLionel Sambuc phys_bytes substart = start, subend = end; 49433d6423SLionel Sambuc phys_bytes memaddr = cbi->memmap[m].mm_base_addr, 50433d6423SLionel Sambuc memend = cbi->memmap[m].mm_base_addr + cbi->memmap[m].mm_length; 51433d6423SLionel Sambuc 52433d6423SLionel Sambuc /* adjust cut range to be a subset of the free memory */ 53433d6423SLionel Sambuc if(substart < memaddr) substart = memaddr; 54433d6423SLionel Sambuc if(subend > memend) subend = memend; 55433d6423SLionel Sambuc if(substart >= subend) continue; 56433d6423SLionel Sambuc 57433d6423SLionel Sambuc /* if there is any overlap, forget this one and add 58433d6423SLionel Sambuc * 1-2 subranges back 59433d6423SLionel Sambuc */ 60433d6423SLionel Sambuc cbi->memmap[m].mm_base_addr = cbi->memmap[m].mm_length = 0; 61433d6423SLionel Sambuc if(substart > memaddr) 62433d6423SLionel Sambuc add_memmap(cbi, memaddr, substart-memaddr); 63433d6423SLionel Sambuc if(subend < memend) 64433d6423SLionel Sambuc add_memmap(cbi, subend, memend-subend); 65433d6423SLionel Sambuc } 66433d6423SLionel Sambuc } 67433d6423SLionel Sambuc 68433d6423SLionel Sambuc void add_memmap(kinfo_t *cbi, u64_t addr, u64_t len) 69433d6423SLionel Sambuc { 70433d6423SLionel Sambuc int m; 71433d6423SLionel Sambuc #define LIMIT 0xFFFFF000 72433d6423SLionel Sambuc /* Truncate available memory at 4GB as the rest of minix 73433d6423SLionel Sambuc * currently can't deal with any bigger. 74433d6423SLionel Sambuc */ 75433d6423SLionel Sambuc if(addr > LIMIT) { 76433d6423SLionel Sambuc return; 77433d6423SLionel Sambuc } 78433d6423SLionel Sambuc 79433d6423SLionel Sambuc if(addr + len > LIMIT) { 80433d6423SLionel Sambuc len -= (addr + len - LIMIT); 81433d6423SLionel Sambuc } 82433d6423SLionel Sambuc 83433d6423SLionel Sambuc assert(cbi->mmap_size < MAXMEMMAP); 84433d6423SLionel Sambuc if(len == 0) { 85433d6423SLionel Sambuc return; 86433d6423SLionel Sambuc } 87433d6423SLionel Sambuc addr = roundup(addr, ARM_PAGE_SIZE); 88433d6423SLionel Sambuc len = rounddown(len, ARM_PAGE_SIZE); 89433d6423SLionel Sambuc 90433d6423SLionel Sambuc assert(kernel_may_alloc); 91433d6423SLionel Sambuc 92433d6423SLionel Sambuc for(m = 0; m < MAXMEMMAP; m++) { 93433d6423SLionel Sambuc phys_bytes highmark; 94433d6423SLionel Sambuc if(cbi->memmap[m].mm_length) { 95433d6423SLionel Sambuc continue; 96433d6423SLionel Sambuc } 97433d6423SLionel Sambuc cbi->memmap[m].mm_base_addr = addr; 98433d6423SLionel Sambuc cbi->memmap[m].mm_length = len; 99433d6423SLionel Sambuc cbi->memmap[m].type = MULTIBOOT_MEMORY_AVAILABLE; 100433d6423SLionel Sambuc if(m >= cbi->mmap_size) { 101433d6423SLionel Sambuc cbi->mmap_size = m+1; 102433d6423SLionel Sambuc } 103433d6423SLionel Sambuc highmark = addr + len; 104433d6423SLionel Sambuc if(highmark > cbi->mem_high_phys) { 105433d6423SLionel Sambuc cbi->mem_high_phys = highmark; 106433d6423SLionel Sambuc } 107433d6423SLionel Sambuc return; 108433d6423SLionel Sambuc } 109433d6423SLionel Sambuc 110433d6423SLionel Sambuc panic("no available memmap slot"); 111433d6423SLionel Sambuc } 112433d6423SLionel Sambuc 113433d6423SLionel Sambuc u32_t *alloc_pagetable(phys_bytes *ph) 114433d6423SLionel Sambuc { 115433d6423SLionel Sambuc u32_t *ret; 116433d6423SLionel Sambuc #define PG_PAGETABLES 24 117433d6423SLionel Sambuc static u32_t pagetables[PG_PAGETABLES][256] __aligned(1024); 118433d6423SLionel Sambuc static int pt_inuse = 0; 119433d6423SLionel Sambuc if(pt_inuse >= PG_PAGETABLES) { 120433d6423SLionel Sambuc panic("no more pagetables"); 121433d6423SLionel Sambuc } 122433d6423SLionel Sambuc assert(sizeof(pagetables[pt_inuse]) == 1024); 123433d6423SLionel Sambuc ret = pagetables[pt_inuse++]; 124433d6423SLionel Sambuc *ph = vir2phys(ret); 125433d6423SLionel Sambuc return ret; 126433d6423SLionel Sambuc } 127433d6423SLionel Sambuc 128433d6423SLionel Sambuc #define PAGE_KB (ARM_PAGE_SIZE / 1024) 129433d6423SLionel Sambuc 130433d6423SLionel Sambuc phys_bytes pg_alloc_page(kinfo_t *cbi) 131433d6423SLionel Sambuc { 132433d6423SLionel Sambuc int m; 133433d6423SLionel Sambuc multiboot_memory_map_t *mmap; 134433d6423SLionel Sambuc 135433d6423SLionel Sambuc assert(kernel_may_alloc); 136433d6423SLionel Sambuc 137433d6423SLionel Sambuc for(m = 0; m < cbi->mmap_size; m++) { 138433d6423SLionel Sambuc mmap = &cbi->memmap[m]; 139433d6423SLionel Sambuc if(!mmap->mm_length) { 140433d6423SLionel Sambuc continue; 141433d6423SLionel Sambuc } 142433d6423SLionel Sambuc assert(mmap->mm_length > 0); 143433d6423SLionel Sambuc assert(!(mmap->mm_length % ARM_PAGE_SIZE)); 144433d6423SLionel Sambuc assert(!(mmap->mm_base_addr % ARM_PAGE_SIZE)); 145433d6423SLionel Sambuc 146433d6423SLionel Sambuc u32_t addr = mmap->mm_base_addr; 147433d6423SLionel Sambuc mmap->mm_base_addr += ARM_PAGE_SIZE; 148433d6423SLionel Sambuc mmap->mm_length -= ARM_PAGE_SIZE; 149433d6423SLionel Sambuc 150433d6423SLionel Sambuc cbi->kernel_allocated_bytes_dynamic += ARM_PAGE_SIZE; 151433d6423SLionel Sambuc 152433d6423SLionel Sambuc return addr; 153433d6423SLionel Sambuc } 154433d6423SLionel Sambuc 155433d6423SLionel Sambuc panic("can't find free memory"); 156433d6423SLionel Sambuc } 157433d6423SLionel Sambuc 158433d6423SLionel Sambuc void pg_identity(kinfo_t *cbi) 159433d6423SLionel Sambuc { 160*0a6a1f1dSLionel Sambuc uint32_t i; 161433d6423SLionel Sambuc phys_bytes phys; 162433d6423SLionel Sambuc 163433d6423SLionel Sambuc /* We map memory that does not correspond to physical memory 164433d6423SLionel Sambuc * as non-cacheable. Make sure we know what it is. 165433d6423SLionel Sambuc */ 166433d6423SLionel Sambuc assert(cbi->mem_high_phys); 167433d6423SLionel Sambuc 168433d6423SLionel Sambuc /* Set up an identity mapping page directory */ 169433d6423SLionel Sambuc for(i = 0; i < ARM_VM_DIR_ENTRIES; i++) { 170433d6423SLionel Sambuc u32_t flags = ARM_VM_SECTION 171433d6423SLionel Sambuc | ARM_VM_SECTION_USER 172433d6423SLionel Sambuc | ARM_VM_SECTION_DOMAIN; 173433d6423SLionel Sambuc 174433d6423SLionel Sambuc phys = i * ARM_SECTION_SIZE; 175433d6423SLionel Sambuc /* mark mormal memory as cacheable. TODO: fix hard coded values */ 176433d6423SLionel Sambuc if (phys >= PHYS_MEM_BEGIN && phys <= PHYS_MEM_END) { 177433d6423SLionel Sambuc pagedir[i] = phys | flags | ARM_VM_SECTION_CACHED; 178433d6423SLionel Sambuc } else { 179433d6423SLionel Sambuc pagedir[i] = phys | flags | ARM_VM_SECTION_DEVICE; 180433d6423SLionel Sambuc } 181433d6423SLionel Sambuc } 182433d6423SLionel Sambuc } 183433d6423SLionel Sambuc 184433d6423SLionel Sambuc int pg_mapkernel(void) 185433d6423SLionel Sambuc { 186433d6423SLionel Sambuc int pde; 187433d6423SLionel Sambuc u32_t mapped = 0, kern_phys = kern_phys_start; 188433d6423SLionel Sambuc 189433d6423SLionel Sambuc assert(!(kern_vir_start % ARM_SECTION_SIZE)); 190433d6423SLionel Sambuc assert(!(kern_phys_start % ARM_SECTION_SIZE)); 191433d6423SLionel Sambuc pde = kern_vir_start / ARM_SECTION_SIZE; /* start pde */ 192433d6423SLionel Sambuc while(mapped < kern_kernlen) { 193433d6423SLionel Sambuc pagedir[pde] = (kern_phys & ARM_VM_SECTION_MASK) 194433d6423SLionel Sambuc | ARM_VM_SECTION 195433d6423SLionel Sambuc | ARM_VM_SECTION_SUPER 196433d6423SLionel Sambuc | ARM_VM_SECTION_DOMAIN 197433d6423SLionel Sambuc | ARM_VM_SECTION_CACHED; 198433d6423SLionel Sambuc mapped += ARM_SECTION_SIZE; 199433d6423SLionel Sambuc kern_phys += ARM_SECTION_SIZE; 200433d6423SLionel Sambuc pde++; 201433d6423SLionel Sambuc } 202433d6423SLionel Sambuc return pde; /* free pde */ 203433d6423SLionel Sambuc } 204433d6423SLionel Sambuc 205433d6423SLionel Sambuc void vm_enable_paging(void) 206433d6423SLionel Sambuc { 207433d6423SLionel Sambuc u32_t sctlr; 208433d6423SLionel Sambuc u32_t actlr; 209433d6423SLionel Sambuc 210433d6423SLionel Sambuc write_ttbcr(0); 211433d6423SLionel Sambuc 212433d6423SLionel Sambuc /* Set all Domains to Client */ 213433d6423SLionel Sambuc write_dacr(0x55555555); 214433d6423SLionel Sambuc 215433d6423SLionel Sambuc sctlr = read_sctlr(); 216433d6423SLionel Sambuc 217433d6423SLionel Sambuc /* Enable MMU */ 218433d6423SLionel Sambuc sctlr |= CPU_CONTROL_MMU_ENABLE; 219433d6423SLionel Sambuc 220433d6423SLionel Sambuc /* TRE set to zero (default reset value): TEX[2:0] are used, plus C and B bits.*/ 221433d6423SLionel Sambuc sctlr &= ~CPU_CONTROL_TR_ENABLE; 222433d6423SLionel Sambuc 223433d6423SLionel Sambuc /* AFE set to zero (default reset value): not using simplified model. */ 224433d6423SLionel Sambuc sctlr &= ~CPU_CONTROL_AF_ENABLE; 225433d6423SLionel Sambuc 226433d6423SLionel Sambuc /* Enable instruction ,data cache and branch prediction */ 227433d6423SLionel Sambuc sctlr |= CPU_CONTROL_DC_ENABLE; 228433d6423SLionel Sambuc sctlr |= CPU_CONTROL_IC_ENABLE; 229433d6423SLionel Sambuc sctlr |= CPU_CONTROL_BPRD_ENABLE; 230433d6423SLionel Sambuc 231433d6423SLionel Sambuc /* Enable barriers */ 232433d6423SLionel Sambuc sctlr |= CPU_CONTROL_32BD_ENABLE; 233433d6423SLionel Sambuc 234433d6423SLionel Sambuc /* Enable L2 cache (cortex-a8) */ 235433d6423SLionel Sambuc #define CORTEX_A8_L2EN (0x02) 236433d6423SLionel Sambuc actlr = read_actlr(); 237433d6423SLionel Sambuc actlr |= CORTEX_A8_L2EN; 238433d6423SLionel Sambuc write_actlr(actlr); 239433d6423SLionel Sambuc 240433d6423SLionel Sambuc write_sctlr(sctlr); 241433d6423SLionel Sambuc } 242433d6423SLionel Sambuc 243433d6423SLionel Sambuc phys_bytes pg_load() 244433d6423SLionel Sambuc { 245433d6423SLionel Sambuc phys_bytes phpagedir = vir2phys(pagedir); 246433d6423SLionel Sambuc write_ttbr0(phpagedir); 247433d6423SLionel Sambuc return phpagedir; 248433d6423SLionel Sambuc } 249433d6423SLionel Sambuc 250433d6423SLionel Sambuc void pg_clear(void) 251433d6423SLionel Sambuc { 252433d6423SLionel Sambuc memset(pagedir, 0, sizeof(pagedir)); 253433d6423SLionel Sambuc } 254433d6423SLionel Sambuc 255433d6423SLionel Sambuc phys_bytes pg_rounddown(phys_bytes b) 256433d6423SLionel Sambuc { 257433d6423SLionel Sambuc phys_bytes o; 258433d6423SLionel Sambuc if(!(o = b % ARM_PAGE_SIZE)) { 259433d6423SLionel Sambuc return b; 260433d6423SLionel Sambuc } 261433d6423SLionel Sambuc return b - o; 262433d6423SLionel Sambuc } 263433d6423SLionel Sambuc 264433d6423SLionel Sambuc void pg_map(phys_bytes phys, vir_bytes vaddr, vir_bytes vaddr_end, 265433d6423SLionel Sambuc kinfo_t *cbi) 266433d6423SLionel Sambuc { 267433d6423SLionel Sambuc static int mapped_pde = -1; 268433d6423SLionel Sambuc static u32_t *pt = NULL; 269433d6423SLionel Sambuc int pde, pte; 270433d6423SLionel Sambuc 271433d6423SLionel Sambuc assert(kernel_may_alloc); 272433d6423SLionel Sambuc 273433d6423SLionel Sambuc if(phys == PG_ALLOCATEME) { 274433d6423SLionel Sambuc assert(!(vaddr % ARM_PAGE_SIZE)); 275433d6423SLionel Sambuc } else { 276433d6423SLionel Sambuc assert((vaddr % ARM_PAGE_SIZE) == (phys % ARM_PAGE_SIZE)); 277433d6423SLionel Sambuc vaddr = pg_rounddown(vaddr); 278433d6423SLionel Sambuc phys = pg_rounddown(phys); 279433d6423SLionel Sambuc } 280433d6423SLionel Sambuc assert(vaddr < kern_vir_start); 281433d6423SLionel Sambuc 282433d6423SLionel Sambuc while(vaddr < vaddr_end) { 283433d6423SLionel Sambuc phys_bytes source = phys; 284433d6423SLionel Sambuc assert(!(vaddr % ARM_PAGE_SIZE)); 285433d6423SLionel Sambuc if(phys == PG_ALLOCATEME) { 286433d6423SLionel Sambuc source = pg_alloc_page(cbi); 287433d6423SLionel Sambuc } else { 288433d6423SLionel Sambuc assert(!(phys % ARM_PAGE_SIZE)); 289433d6423SLionel Sambuc } 290433d6423SLionel Sambuc assert(!(source % ARM_PAGE_SIZE)); 291433d6423SLionel Sambuc pde = ARM_VM_PDE(vaddr); 292433d6423SLionel Sambuc pte = ARM_VM_PTE(vaddr); 293433d6423SLionel Sambuc if(mapped_pde < pde) { 294433d6423SLionel Sambuc phys_bytes ph; 295433d6423SLionel Sambuc pt = alloc_pagetable(&ph); 296433d6423SLionel Sambuc pagedir[pde] = (ph & ARM_VM_PDE_MASK) 297433d6423SLionel Sambuc | ARM_VM_PAGEDIR 298433d6423SLionel Sambuc | ARM_VM_PDE_DOMAIN; 299433d6423SLionel Sambuc mapped_pde = pde; 300433d6423SLionel Sambuc } 301433d6423SLionel Sambuc assert(pt); 302433d6423SLionel Sambuc pt[pte] = (source & ARM_VM_PTE_MASK) 303433d6423SLionel Sambuc | ARM_VM_PAGETABLE 304433d6423SLionel Sambuc | ARM_VM_PTE_CACHED 305433d6423SLionel Sambuc | ARM_VM_PTE_USER; 306433d6423SLionel Sambuc vaddr += ARM_PAGE_SIZE; 307433d6423SLionel Sambuc if(phys != PG_ALLOCATEME) { 308433d6423SLionel Sambuc phys += ARM_PAGE_SIZE; 309433d6423SLionel Sambuc } 310433d6423SLionel Sambuc } 311433d6423SLionel Sambuc } 312433d6423SLionel Sambuc 313433d6423SLionel Sambuc void pg_info(reg_t *pagedir_ph, u32_t **pagedir_v) 314433d6423SLionel Sambuc { 315433d6423SLionel Sambuc *pagedir_ph = vir2phys(pagedir); 316433d6423SLionel Sambuc *pagedir_v = pagedir; 317433d6423SLionel Sambuc } 318