1845a180fSFrançois Revol /* 24e44040dSFrançois Revol * Copyright 2007, François Revol, revol@free.fr. 34e44040dSFrançois Revol * Distributed under the terms of the MIT License. 44e44040dSFrançois Revol * 5845a180fSFrançois Revol * Copyright 2003-2005, Axel Dörfler, axeld@pinc-software.de. 6845a180fSFrançois Revol * Distributed under the terms of the MIT License. 7845a180fSFrançois Revol * 8845a180fSFrançois Revol * Copyright 2001, Travis Geiselbrecht. All rights reserved. 9845a180fSFrançois Revol * Distributed under the terms of the NewOS License. 10845a180fSFrançois Revol */ 11845a180fSFrançois Revol 12845a180fSFrançois Revol 13845a180fSFrançois Revol #include <KernelExport.h> 14845a180fSFrançois Revol 15845a180fSFrançois Revol #include <arch_platform.h> 16845a180fSFrançois Revol #include <arch_thread.h> 17845a180fSFrançois Revol #include <arch/cpu.h> 18845a180fSFrançois Revol #include <boot/kernel_args.h> 19845a180fSFrançois Revol 204e44040dSFrançois Revol extern struct m68k_cpu_ops cpu_ops_030; 214e44040dSFrançois Revol extern struct m68k_cpu_ops cpu_ops_040; 224e44040dSFrançois Revol extern struct m68k_cpu_ops cpu_ops_060; 234e44040dSFrançois Revol 244e44040dSFrançois Revol struct m68k_cpu_ops cpu_ops; 254e44040dSFrançois Revol 26f0c5a3daSFrançois Revol int arch_cpu_type; 27f0c5a3daSFrançois Revol int arch_fpu_type; 28f0c5a3daSFrançois Revol int arch_mmu_type; 29f0c5a3daSFrançois Revol int arch_platform; 30f0c5a3daSFrançois Revol 31845a180fSFrançois Revol status_t 32845a180fSFrançois Revol arch_cpu_preboot_init_percpu(kernel_args *args, int curr_cpu) 33845a180fSFrançois Revol { 34845a180fSFrançois Revol // enable FPU 35829748d8SFrançois Revol //ppc:set_msr(get_msr() | MSR_FP_AVAILABLE); 36845a180fSFrançois Revol 37845a180fSFrançois Revol // The current thread must be NULL for all CPUs till we have threads. 38845a180fSFrançois Revol // Some boot code relies on this. 39845a180fSFrançois Revol arch_thread_set_current_thread(NULL); 40845a180fSFrançois Revol 41845a180fSFrançois Revol return B_OK; 42845a180fSFrançois Revol } 43845a180fSFrançois Revol 44845a180fSFrançois Revol 45845a180fSFrançois Revol status_t 461fd024beSFrançois Revol arch_cpu_init_percpu(kernel_args *args, int curr_cpu) 471fd024beSFrançois Revol { 481fd024beSFrançois Revol //detect_cpu(curr_cpu); 491fd024beSFrançois Revol 501fd024beSFrançois Revol // we only support one anyway... 511fd024beSFrançois Revol return 0; 521fd024beSFrançois Revol } 531fd024beSFrançois Revol 541fd024beSFrançois Revol 551fd024beSFrançois Revol status_t 56845a180fSFrançois Revol arch_cpu_init(kernel_args *args) 57845a180fSFrançois Revol { 58a3dc7ef0SFrançois Revol arch_cpu_type = args->arch_args.cpu_type; 59a3dc7ef0SFrançois Revol arch_fpu_type = args->arch_args.fpu_type; 60a3dc7ef0SFrançois Revol arch_mmu_type = args->arch_args.mmu_type; 61a3dc7ef0SFrançois Revol arch_platform = args->arch_args.platform; 62f0c5a3daSFrançois Revol arch_platform = args->arch_args.machine; 634e44040dSFrançois Revol 64a3dc7ef0SFrançois Revol switch (arch_cpu_type) { 65a3dc7ef0SFrançois Revol case 68020: 66a3dc7ef0SFrançois Revol case 68030: 6736ee9f5cSFrançois Revol memcpy(&cpu_ops, &cpu_ops_030, sizeof(cpu_ops)); 684e44040dSFrançois Revol break; 69*b061bf24SFrançois Revol 70a3dc7ef0SFrançois Revol case 68040: 71*b061bf24SFrançois Revol memcpy(&cpu_ops, &cpu_ops_040, sizeof(cpu_ops)); 724e44040dSFrançois Revol break; 73*b061bf24SFrançois Revol 744e44040dSFrançois Revol #ifdef SUPPORTS_060 75a3dc7ef0SFrançois Revol case 68060: 76*b061bf24SFrançois Revol memcpy(&cpu_ops, &cpu_ops_060, sizeof(cpu_ops)); 774e44040dSFrançois Revol break; 784e44040dSFrançois Revol #endif 794e44040dSFrançois Revol default: 80a3dc7ef0SFrançois Revol panic("unknown cpu_type %d\n", arch_cpu_type); 814e44040dSFrançois Revol } 82a3dc7ef0SFrançois Revol 83845a180fSFrançois Revol return B_OK; 84845a180fSFrançois Revol } 85845a180fSFrançois Revol 86845a180fSFrançois Revol 87845a180fSFrançois Revol status_t 88845a180fSFrançois Revol arch_cpu_init_post_vm(kernel_args *args) 89845a180fSFrançois Revol { 90845a180fSFrançois Revol return B_OK; 91845a180fSFrançois Revol } 92845a180fSFrançois Revol 93845a180fSFrançois Revol status_t 94845a180fSFrançois Revol arch_cpu_init_post_modules(kernel_args *args) 95845a180fSFrançois Revol { 96845a180fSFrançois Revol return B_OK; 97845a180fSFrançois Revol } 98845a180fSFrançois Revol 99845a180fSFrançois Revol 100845a180fSFrançois Revol void 101845a180fSFrançois Revol arch_cpu_sync_icache(void *address, size_t len) 102845a180fSFrançois Revol { 103a3dc7ef0SFrançois Revol cpu_ops.flush_icache((addr_t)address, len); 104845a180fSFrançois Revol } 105845a180fSFrançois Revol 106845a180fSFrançois Revol 107845a180fSFrançois Revol void 108807cf76dSFrançois Revol arch_cpu_memory_read_barrier(void) 109807cf76dSFrançois Revol { 110807cf76dSFrançois Revol asm volatile ("nop;" : : : "memory"); 111*b061bf24SFrançois Revol #warning M68k: check arch_cpu_memory_read_barrier (FNOP ?) 112807cf76dSFrançois Revol } 113807cf76dSFrançois Revol 114807cf76dSFrançois Revol 115807cf76dSFrançois Revol void 116807cf76dSFrançois Revol arch_cpu_memory_write_barrier(void) 117807cf76dSFrançois Revol { 118807cf76dSFrançois Revol asm volatile ("nop;" : : : "memory"); 119*b061bf24SFrançois Revol #warning M68k: check arch_cpu_memory_write_barrier (FNOP ?) 120807cf76dSFrançois Revol } 121807cf76dSFrançois Revol 122807cf76dSFrançois Revol 123807cf76dSFrançois Revol void 124845a180fSFrançois Revol arch_cpu_invalidate_TLB_range(addr_t start, addr_t end) 125845a180fSFrançois Revol { 1264237dbd0SFrançois Revol int32 num_pages = end / B_PAGE_SIZE - start / B_PAGE_SIZE; 1274e44040dSFrançois Revol cpu_ops.flush_insn_pipeline(); 1284237dbd0SFrançois Revol while (num_pages-- >= 0) { 1294e44040dSFrançois Revol cpu_ops.flush_atc_addr(start); 1304e44040dSFrançois Revol cpu_ops.flush_insn_pipeline(); 131845a180fSFrançois Revol start += B_PAGE_SIZE; 132845a180fSFrançois Revol } 1334e44040dSFrançois Revol cpu_ops.flush_insn_pipeline(); 134845a180fSFrançois Revol } 135845a180fSFrançois Revol 136845a180fSFrançois Revol 137845a180fSFrançois Revol void 138845a180fSFrançois Revol arch_cpu_invalidate_TLB_list(addr_t pages[], int num_pages) 139845a180fSFrançois Revol { 140845a180fSFrançois Revol int i; 141845a180fSFrançois Revol 1424e44040dSFrançois Revol cpu_ops.flush_insn_pipeline(); 143845a180fSFrançois Revol for (i = 0; i < num_pages; i++) { 1444e44040dSFrançois Revol cpu_ops.flush_atc_addr(pages[i]); 1454e44040dSFrançois Revol cpu_ops.flush_insn_pipeline(); 146845a180fSFrançois Revol } 1474e44040dSFrançois Revol cpu_ops.flush_insn_pipeline(); 148845a180fSFrançois Revol } 149845a180fSFrançois Revol 150845a180fSFrançois Revol 151845a180fSFrançois Revol void 152845a180fSFrançois Revol arch_cpu_global_TLB_invalidate(void) 153845a180fSFrançois Revol { 1544e44040dSFrançois Revol cpu_ops.flush_insn_pipeline(); 1554e44040dSFrançois Revol cpu_ops.flush_atc_all(); 1564e44040dSFrançois Revol cpu_ops.flush_insn_pipeline(); 157845a180fSFrançois Revol } 158845a180fSFrançois Revol 159845a180fSFrançois Revol 160845a180fSFrançois Revol void 161845a180fSFrançois Revol arch_cpu_user_TLB_invalidate(void) 162845a180fSFrançois Revol { 1634e44040dSFrançois Revol cpu_ops.flush_insn_pipeline(); 1644e44040dSFrançois Revol cpu_ops.flush_atc_user(); 1654e44040dSFrançois Revol cpu_ops.flush_insn_pipeline(); 166845a180fSFrançois Revol } 167845a180fSFrançois Revol 168845a180fSFrançois Revol 169845a180fSFrançois Revol status_t 170845a180fSFrançois Revol arch_cpu_user_memcpy(void *to, const void *from, size_t size, 171845a180fSFrançois Revol addr_t *faultHandler) 172845a180fSFrançois Revol { 173845a180fSFrançois Revol char *tmp = (char *)to; 174845a180fSFrançois Revol char *s = (char *)from; 175845a180fSFrançois Revol addr_t oldFaultHandler = *faultHandler; 176845a180fSFrançois Revol 177845a180fSFrançois Revol if (m68k_set_fault_handler(faultHandler, (addr_t)&&error)) 178845a180fSFrançois Revol goto error; 179845a180fSFrançois Revol 180845a180fSFrançois Revol while (size--) 181845a180fSFrançois Revol *tmp++ = *s++; 182845a180fSFrançois Revol 183845a180fSFrançois Revol *faultHandler = oldFaultHandler; 184845a180fSFrançois Revol return 0; 185845a180fSFrançois Revol 186845a180fSFrançois Revol error: 187845a180fSFrançois Revol *faultHandler = oldFaultHandler; 188845a180fSFrançois Revol return B_BAD_ADDRESS; 189845a180fSFrançois Revol } 190845a180fSFrançois Revol 191845a180fSFrançois Revol 192845a180fSFrançois Revol /** \brief Copies at most (\a size - 1) characters from the string in \a from to 193845a180fSFrançois Revol * the string in \a to, NULL-terminating the result. 194845a180fSFrançois Revol * 195845a180fSFrançois Revol * \param to Pointer to the destination C-string. 196845a180fSFrançois Revol * \param from Pointer to the source C-string. 197845a180fSFrançois Revol * \param size Size in bytes of the string buffer pointed to by \a to. 198845a180fSFrançois Revol * 199845a180fSFrançois Revol * \return strlen(\a from). 200845a180fSFrançois Revol */ 201845a180fSFrançois Revol 202845a180fSFrançois Revol ssize_t 203845a180fSFrançois Revol arch_cpu_user_strlcpy(char *to, const char *from, size_t size, addr_t *faultHandler) 204845a180fSFrançois Revol { 205845a180fSFrançois Revol int from_length = 0; 206845a180fSFrançois Revol addr_t oldFaultHandler = *faultHandler; 207845a180fSFrançois Revol 208845a180fSFrançois Revol if (m68k_set_fault_handler(faultHandler, (addr_t)&&error)) 209845a180fSFrançois Revol goto error; 210845a180fSFrançois Revol 211845a180fSFrançois Revol if (size > 0) { 212845a180fSFrançois Revol to[--size] = '\0'; 213845a180fSFrançois Revol // copy 214845a180fSFrançois Revol for ( ; size; size--, from_length++, to++, from++) { 215845a180fSFrançois Revol if ((*to = *from) == '\0') 216845a180fSFrançois Revol break; 217845a180fSFrançois Revol } 218845a180fSFrançois Revol } 219845a180fSFrançois Revol // count any leftover from chars 220845a180fSFrançois Revol while (*from++ != '\0') 221845a180fSFrançois Revol from_length++; 222845a180fSFrançois Revol 223845a180fSFrançois Revol *faultHandler = oldFaultHandler; 224845a180fSFrançois Revol return from_length; 225845a180fSFrançois Revol 226845a180fSFrançois Revol error: 227845a180fSFrançois Revol *faultHandler = oldFaultHandler; 228845a180fSFrançois Revol return B_BAD_ADDRESS; 229845a180fSFrançois Revol } 230845a180fSFrançois Revol 231845a180fSFrançois Revol 232845a180fSFrançois Revol status_t 233845a180fSFrançois Revol arch_cpu_user_memset(void *s, char c, size_t count, addr_t *faultHandler) 234845a180fSFrançois Revol { 235845a180fSFrançois Revol char *xs = (char *)s; 236845a180fSFrançois Revol addr_t oldFaultHandler = *faultHandler; 237845a180fSFrançois Revol 238845a180fSFrançois Revol if (m68k_set_fault_handler(faultHandler, (addr_t)&&error)) 239845a180fSFrançois Revol goto error; 240845a180fSFrançois Revol 241845a180fSFrançois Revol while (count--) 242845a180fSFrançois Revol *xs++ = c; 243845a180fSFrançois Revol 244845a180fSFrançois Revol *faultHandler = oldFaultHandler; 245845a180fSFrançois Revol return 0; 246845a180fSFrançois Revol 247845a180fSFrançois Revol error: 248845a180fSFrançois Revol *faultHandler = oldFaultHandler; 249845a180fSFrançois Revol return B_BAD_ADDRESS; 250845a180fSFrançois Revol } 251845a180fSFrançois Revol 252845a180fSFrançois Revol 253845a180fSFrançois Revol status_t 254845a180fSFrançois Revol arch_cpu_shutdown(bool reboot) 255845a180fSFrançois Revol { 256845a180fSFrançois Revol M68KPlatform::Default()->ShutDown(reboot); 257845a180fSFrançois Revol return B_ERROR; 258845a180fSFrançois Revol } 259845a180fSFrançois Revol 260845a180fSFrançois Revol 261845a180fSFrançois Revol void 262845a180fSFrançois Revol arch_cpu_idle(void) 263845a180fSFrançois Revol { 2644e44040dSFrançois Revol if (cpu_ops.idle) 2654e44040dSFrançois Revol cpu_ops.idle(); 266ac1a8a0cSFrançois Revol #warning M68K: use LPSTOP ? 267ac1a8a0cSFrançois Revol //asm volatile ("lpstop"); 268845a180fSFrançois Revol } 269845a180fSFrançois Revol 270845a180fSFrançois Revol 271845a180fSFrançois Revol // The purpose of this function is to trick the compiler. When setting the 272845a180fSFrançois Revol // page_handler to a label that is obviously (to the compiler) never used, 273845a180fSFrançois Revol // it may reorganize the control flow, so that the labeled part is optimized 274845a180fSFrançois Revol // away. 275845a180fSFrançois Revol // By invoking the function like this 276845a180fSFrançois Revol // 277845a180fSFrançois Revol // if (m68k_set_fault_handler(faultHandler, (addr_t)&&error)) 278845a180fSFrançois Revol // goto error; 279845a180fSFrançois Revol // 280845a180fSFrançois Revol // the compiler has to keep the labeled code, since it can't guess the return 281845a180fSFrançois Revol // value of this (non-inlinable) function. At least in my tests it worked that 282845a180fSFrançois Revol // way, and I hope it will continue to work like this in the future. 283845a180fSFrançois Revol // 284845a180fSFrançois Revol bool 285845a180fSFrançois Revol m68k_set_fault_handler(addr_t *handlerLocation, addr_t handler) 286845a180fSFrançois Revol { 287845a180fSFrançois Revol *handlerLocation = handler; 288845a180fSFrançois Revol return false; 289845a180fSFrançois Revol } 290