prom_init.c
字号:
* * at 14Mb or more we vmlinux, then a gap and initrd. In some rare cases, initrd * might end up beeing before the kernel though. We assume this won't override * the final kernel at 0, we have no provision to handle that in this version, * but it should hopefully never happen. * * alloc_top is set to the top of RMO, eventually shrink down if the TCEs overlap * alloc_bottom is set to the top of kernel/initrd * * from there, allocations are done that way : rtas is allocated topmost, and * the device-tree is allocated from the bottom. We try to grow the device-tree * allocation as we progress. If we can't, then we fail, we don't currently have * a facility to restart elsewhere, but that shouldn't be necessary neither * * Note that calls to reserve_mem have to be done explicitely, memory allocated * with either alloc_up or alloc_down isn't automatically reserved. *//* * Allocates memory in the RMO upward from the kernel/initrd * * When align is 0, this is a special case, it means to allocate in place * at the current location of alloc_bottom or fail (that is basically * extending the previous allocation). Used for the device-tree flattening */static unsigned long __init alloc_up(unsigned long size, unsigned long align){ unsigned long offset = reloc_offset(); unsigned long base = _ALIGN_UP(RELOC(alloc_bottom), align); unsigned long addr = 0; prom_debug("alloc_up(%x, %x)\n", size, align); if (RELOC(ram_top) == 0) prom_panic("alloc_up() called with mem not initialized\n"); if (align) base = _ALIGN_UP(RELOC(alloc_bottom), align); else base = RELOC(alloc_bottom); for(; (base + size) <= RELOC(alloc_top); base = _ALIGN_UP(base + 0x100000, align)) { prom_debug(" trying: 0x%x\n\r", base); addr = (unsigned long)prom_claim(base, size, 0); if ((int)addr != PROM_ERROR) break; addr = 0; if (align == 0) break; } if (addr == 0) return 0; RELOC(alloc_bottom) = addr; prom_debug(" -> %x\n", addr); prom_debug(" alloc_bottom : %x\n", RELOC(alloc_bottom)); prom_debug(" alloc_top : %x\n", RELOC(alloc_top)); prom_debug(" alloc_top_hi : %x\n", RELOC(alloc_top_high)); prom_debug(" rmo_top : %x\n", RELOC(rmo_top)); prom_debug(" ram_top : %x\n", RELOC(ram_top)); return addr;}/* * Allocates memory downard, either from top of RMO, or if highmem * is set, from the top of RAM. Note that this one doesn't handle * failures. In does claim memory if highmem is not set. */static unsigned long __init alloc_down(unsigned long size, unsigned long align, int highmem){ unsigned long offset = reloc_offset(); unsigned long base, addr = 0; prom_debug("alloc_down(%x, %x, %s)\n", size, align, highmem ? RELOC("(high)") : RELOC("(low)")); if (RELOC(ram_top) == 0) prom_panic("alloc_down() called with mem not initialized\n"); if (highmem) { /* Carve out storage for the TCE table. */ addr = _ALIGN_DOWN(RELOC(alloc_top_high) - size, align); if (addr <= RELOC(alloc_bottom)) return 0; else { /* Will we bump into the RMO ? If yes, check out that we * didn't overlap existing allocations there, if we did, * we are dead, we must be the first in town ! */ if (addr < RELOC(rmo_top)) { /* Good, we are first */ if (RELOC(alloc_top) == RELOC(rmo_top)) RELOC(alloc_top) = RELOC(rmo_top) = addr; else return 0; } RELOC(alloc_top_high) = addr; } goto bail; } base = _ALIGN_DOWN(RELOC(alloc_top) - size, align); for(; base > RELOC(alloc_bottom); base = _ALIGN_DOWN(base - 0x100000, align)) { prom_debug(" trying: 0x%x\n\r", base); addr = (unsigned long)prom_claim(base, size, 0); if ((int)addr != PROM_ERROR) break; addr = 0; } if (addr == 0) return 0; RELOC(alloc_top) = addr; bail: prom_debug(" -> %x\n", addr); prom_debug(" alloc_bottom : %x\n", RELOC(alloc_bottom)); prom_debug(" alloc_top : %x\n", RELOC(alloc_top)); prom_debug(" alloc_top_hi : %x\n", RELOC(alloc_top_high)); prom_debug(" rmo_top : %x\n", RELOC(rmo_top)); prom_debug(" ram_top : %x\n", RELOC(ram_top)); return addr;}/* * Parse a "reg" cell */static unsigned long __init prom_next_cell(int s, cell_t **cellp){ cell_t *p = *cellp; unsigned long r = 0; /* Ignore more than 2 cells */ while (s > 2) { p++; s--; } while (s) { r <<= 32; r |= *(p++); s--; } *cellp = p; return r;}/* * Very dumb function for adding to the memory reserve list, but * we don't need anything smarter at this point * * XXX Eventually check for collisions. They should NEVER happen * if problems seem to show up, it would be a good start to track * them down. */static void reserve_mem(unsigned long base, unsigned long size){ unsigned long offset = reloc_offset(); unsigned long top = base + size; unsigned long cnt = RELOC(mem_reserve_cnt); if (size == 0) return; /* We need to always keep one empty entry so that we * have our terminator with "size" set to 0 since we are * dumb and just copy this entire array to the boot params */ base = _ALIGN_DOWN(base, PAGE_SIZE); top = _ALIGN_UP(top, PAGE_SIZE); size = top - base; if (cnt >= (MEM_RESERVE_MAP_SIZE - 1)) prom_panic("Memory reserve map exhausted !\n"); RELOC(mem_reserve_map)[cnt].base = base; RELOC(mem_reserve_map)[cnt].size = size; RELOC(mem_reserve_cnt) = cnt + 1;}/* * Initialize memory allocation mecanism, parse "memory" nodes and * obtain that way the top of memory and RMO to setup out local allocator */static void __init prom_init_mem(void){ phandle node; char *path, type[64]; unsigned int plen; cell_t *p, *endp; unsigned long offset = reloc_offset(); struct prom_t *_prom = PTRRELOC(&prom); /* * We iterate the memory nodes to find * 1) top of RMO (first node) * 2) top of memory */ prom_debug("root_addr_cells: %x\n", (long)_prom->root_addr_cells); prom_debug("root_size_cells: %x\n", (long)_prom->root_size_cells); prom_debug("scanning memory:\n"); path = RELOC(prom_scratch); for (node = 0; prom_next_node(&node); ) { type[0] = 0; prom_getprop(node, "device_type", type, sizeof(type)); if (strcmp(type, RELOC("memory"))) continue; plen = prom_getprop(node, "reg", RELOC(regbuf), sizeof(regbuf)); if (plen > sizeof(regbuf)) { prom_printf("memory node too large for buffer !\n"); plen = sizeof(regbuf); } p = RELOC(regbuf); endp = p + (plen / sizeof(cell_t));#ifdef DEBUG_PROM memset(path, 0, PROM_SCRATCH_SIZE); call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1); prom_debug(" node %s :\n", path);#endif /* DEBUG_PROM */ while ((endp - p) >= (_prom->root_addr_cells + _prom->root_size_cells)) { unsigned long base, size; base = prom_next_cell(_prom->root_addr_cells, &p); size = prom_next_cell(_prom->root_size_cells, &p); if (size == 0) continue; prom_debug(" %x %x\n", base, size); if (base == 0) RELOC(rmo_top) = size; if ((base + size) > RELOC(ram_top)) RELOC(ram_top) = base + size; } } /* Setup our top/bottom alloc points, that is top of RMO or top of * segment 0 when running non-LPAR */ if ( RELOC(of_platform) == PLATFORM_PSERIES_LPAR ) RELOC(alloc_top) = RELOC(rmo_top); else RELOC(alloc_top) = min(0x40000000ul, RELOC(ram_top)); RELOC(alloc_bottom) = PAGE_ALIGN(RELOC(klimit) - offset + 0x4000); RELOC(alloc_top_high) = RELOC(ram_top); /* Check if we have an initrd after the kernel, if we do move our bottom * point to after it */ if (RELOC(prom_initrd_start)) { if ((RELOC(prom_initrd_start) + RELOC(prom_initrd_end)) > RELOC(alloc_bottom)) RELOC(alloc_bottom) = PAGE_ALIGN(RELOC(prom_initrd_end)); } prom_printf("memory layout at init:\n"); prom_printf(" alloc_bottom : %x\n", RELOC(alloc_bottom)); prom_printf(" alloc_top : %x\n", RELOC(alloc_top)); prom_printf(" alloc_top_hi : %x\n", RELOC(alloc_top_high)); prom_printf(" rmo_top : %x\n", RELOC(rmo_top)); prom_printf(" ram_top : %x\n", RELOC(ram_top));}/* * Allocate room for and instanciate RTAS */static void __init prom_instantiate_rtas(void){ unsigned long offset = reloc_offset(); struct prom_t *_prom = PTRRELOC(&prom); phandle prom_rtas; u64 base, entry = 0; u32 size; prom_debug("prom_instantiate_rtas: start...\n"); prom_rtas = call_prom("finddevice", 1, 1, ADDR("/rtas")); if (prom_rtas != (phandle) -1) { prom_getprop(prom_rtas, "rtas-size", &size, sizeof(size)); if (size != 0) { base = alloc_down(size, PAGE_SIZE, 0); if (base == 0) { prom_printf("RTAS allocation failed !\n"); return; } prom_printf("instantiating rtas at 0x%x", base); prom_rtas = call_prom("open", 1, 1, ADDR("/rtas")); prom_printf("..."); if (call_prom("call-method", 3, 2, ADDR("instantiate-rtas"), prom_rtas, base) != PROM_ERROR) { entry = (long)_prom->args.rets[1]; } if (entry == 0) { prom_printf(" failed\n"); return; } prom_printf(" done\n"); reserve_mem(base, size); } prom_setprop(_prom->chosen, "linux,rtas-base", &base, sizeof(base)); prom_setprop(_prom->chosen, "linux,rtas-entry", &entry, sizeof(entry)); prom_setprop(_prom->chosen, "linux,rtas-size", &size, sizeof(size)); prom_debug("rtas base = 0x%x\n", base); prom_debug("rtas entry = 0x%x\n", entry); prom_debug("rtas size = 0x%x\n", (long)size); } prom_debug("prom_instantiate_rtas: end...\n");}/* * Allocate room for and initialize TCE tables */static void __init prom_initialize_tce_table(void){ phandle node; ihandle phb_node; unsigned long offset = reloc_offset(); char compatible[64], type[64], model[64]; char *path = RELOC(prom_scratch); u64 base, vbase, align; u32 minalign, minsize; u64 tce_entry, *tce_entryp; u64 local_alloc_top, local_alloc_bottom; u64 i; if (RELOC(ppc64_iommu_off)) return; prom_debug("starting prom_initialize_tce_table\n"); /* Cache current top of allocs so we reserve a single block */ local_alloc_top = RELOC(alloc_top_high); local_alloc_bottom = local_alloc_top; /* Search all nodes looking for PHBs. */ for (node = 0; prom_next_node(&node); ) { compatible[0] = 0; type[0] = 0; model[0] = 0; prom_getprop(node, "compatible", compatible, sizeof(compatible)); prom_getprop(node, "device_type", type, sizeof(type)); prom_getprop(node, "model", model, sizeof(model)); if ((type[0] == 0) || (strstr(type, RELOC("pci")) == NULL)) continue; /* Keep the old logic in tack to avoid regression. */ if (compatible[0] != 0) { if ((strstr(compatible, RELOC("python")) == NULL) && (strstr(compatible, RELOC("Speedwagon")) == NULL) && (strstr(compatible, RELOC("Winnipeg")) == NULL)) continue; } else if (model[0] != 0) { if ((strstr(model, RELOC("ython")) == NULL) && (strstr(model, RELOC("peedwagon")) == NULL) && (strstr(model, RELOC("innipeg")) == NULL)) continue; } if (prom_getprop(node, "tce-table-minalign", &minalign, sizeof(minalign)) == PROM_ERROR) minalign = 0; if (prom_getprop(node, "tce-table-minsize", &minsize, sizeof(minsize)) == PROM_ERROR) minsize = 4UL << 20; /* * Even though we read what OF wants, we just set the table * size to 4 MB. This is enough to map 2GB of PCI DMA space. * By doing this, we avoid the pitfalls of trying to DMA to * MMIO space and the DMA alias hole. * * On POWER4, firmware sets the TCE region by assuming * each TCE table is 8MB. Using this memory for anything * else will impact performance, so we always allocate 8MB. * Anton */ if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p)) minsize = 8UL << 20; else minsize = 4UL << 20; /* Align to the greater of the align or size */ align = max(minalign, minsize); base = alloc_down(minsize, align, 1); if (base == 0) prom_panic("ERROR, cannot find space for TCE table.\n"); if (base < local_alloc_bottom) local_alloc_bottom = base; vbase = (unsigned long)abs_to_virt(base); /* Save away the TCE table attributes for later use. */ prom_setprop(node, "linux,tce-base", &vbase, sizeof(vbase)); prom_setprop(node, "linux,tce-size", &minsize, sizeof(minsize)); prom_setprop(node, "linux,has-tce-table", NULL, 0); /* It seems OF doesn't null-terminate the path :-( */ memset(path, 0, sizeof(path)); /* Call OF to setup the TCE hardware */ if (call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) { prom_printf("package-to-path failed\n"); } prom_debug("TCE table: %s\n", path); prom_debug("\tnode = 0x%x\n", node); prom_debug("\tbase = 0x%x\n", vbase); prom_debug("\tsize = 0x%x\n", minsize); /* Initialize the table to have a one-to-one mapping * over the allocated size. */ tce_entryp = (unsigned long *)base;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -