欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

prom_init.c

底层驱动开发
C
第 1 页 / 共 4 页
字号:
		u32	type;		char	name[8];	/* "PowerPC" */		struct chrpdesc {			u32	real_mode;			u32	real_base;			u32	real_size;			u32	virt_base;			u32	virt_size;			u32	load_base;		} chrpdesc;	} chrpnote;	struct rpanote {		u32	namesz;		u32	descsz;		u32	type;		char	name[24];	/* "IBM,RPA-Client-Config" */		struct rpadesc {			u32	lpar_affinity;			u32	min_rmo_size;			u32	min_rmo_percent;			u32	max_pft_size;			u32	splpar;			u32	min_load;			u32	new_mem_def;			u32	ignore_me;		} rpadesc;	} rpanote;} fake_elf = {	.elfhdr = {		.e_ident = { 0x7f, 'E', 'L', 'F',			     ELFCLASS32, ELFDATA2MSB, EV_CURRENT },		.e_type = ET_EXEC,	/* yeah right */		.e_machine = EM_PPC,		.e_version = EV_CURRENT,		.e_phoff = offsetof(struct fake_elf, phdr),		.e_phentsize = sizeof(Elf32_Phdr),		.e_phnum = 2	},	.phdr = {		[0] = {			.p_type = PT_NOTE,			.p_offset = offsetof(struct fake_elf, chrpnote),			.p_filesz = sizeof(struct chrpnote)		}, [1] = {			.p_type = PT_NOTE,			.p_offset = offsetof(struct fake_elf, rpanote),			.p_filesz = sizeof(struct rpanote)		}	},	.chrpnote = {		.namesz = sizeof("PowerPC"),		.descsz = sizeof(struct chrpdesc),		.type = 0x1275,		.name = "PowerPC",		.chrpdesc = {			.real_mode = ~0U,	/* ~0 means "don't care" */			.real_base = ~0U,			.real_size = ~0U,			.virt_base = ~0U,			.virt_size = ~0U,			.load_base = ~0U		},	},	.rpanote = {		.namesz = sizeof("IBM,RPA-Client-Config"),		.descsz = sizeof(struct rpadesc),		.type = 0x12759999,		.name = "IBM,RPA-Client-Config",		.rpadesc = {			.lpar_affinity = 0,			.min_rmo_size = 64,	/* in megabytes */			.min_rmo_percent = 0,			.max_pft_size = 48,	/* 2^48 bytes max PFT size */			.splpar = 1,			.min_load = ~0U,			.new_mem_def = 0		}	}};static void __init prom_send_capabilities(void){	unsigned long offset = reloc_offset();	ihandle elfloader;	elfloader = call_prom("open", 1, 1, ADDR("/packages/elf-loader"));	if (elfloader == 0) {		prom_printf("couldn't open /packages/elf-loader\n");		return;	}	call_prom("call-method", 3, 1, ADDR("process-elf-header"),			elfloader, ADDR(&fake_elf));	call_prom("close", 1, 0, elfloader);}/* * Memory allocation strategy... our layout is normally: * *  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 (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 (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;		}	}	RELOC(alloc_bottom) = PAGE_ALIGN(RELOC(klimit) - offset + 0x4000);	/* 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_end) > RELOC(alloc_bottom))			RELOC(alloc_bottom) = PAGE_ALIGN(RELOC(prom_initrd_end));	}	/*	 * If prom_memory_limit is set we reduce the upper limits *except* for	 * alloc_top_high. This must be the real top of RAM so we can put	 * TCE's up there.	 */	RELOC(alloc_top_high) = RELOC(ram_top);	if (RELOC(prom_memory_limit)) {		if (RELOC(prom_memory_limit) <= RELOC(alloc_bottom)) {			prom_printf("Ignoring mem=%x <= alloc_bottom.\n",				RELOC(prom_memory_limit));			RELOC(prom_memory_limit) = 0;		} else if (RELOC(prom_memory_limit) >= RELOC(ram_top)) {			prom_printf("Ignoring mem=%x >= ram_top.\n",				RELOC(prom_memory_limit));			RELOC(prom_memory_limit) = 0;		} else {			RELOC(ram_top) = RELOC(prom_memory_limit);			RELOC(rmo_top) = min(RELOC(rmo_top), RELOC(prom_memory_limit));		}	}	/*	 * Setup our top alloc point, 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		/* Some RS64 machines have buggy firmware where claims up at 1GB		 * fails. Cap at 768MB as a workaround. Still plenty of room.		 */		RELOC(alloc_top) = RELOC(rmo_top) = min(0x30000000ul, RELOC(ram_top));	prom_printf("memory layout at init:\n");	prom_printf("  memory_limit : %x (16 MB aligned)\n", RELOC(prom_memory_limit));	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 rtas_node;	ihandle rtas_inst;	u32 base, entry = 0;	u32 size = 0;	prom_debug("prom_instantiate_rtas: start...\n");	rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));	prom_debug("rtas_node: %x\n", rtas_node);	if (!PHANDLE_VALID(rtas_node))		return;	prom_getprop(rtas_node, "rtas-size", &size, sizeof(size));	if (size == 0)		return;	base = alloc_down(size, PAGE_SIZE, 0);	if (base == 0) {		prom_printf("RTAS allocation failed !\n");		return;	}	rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));	if (!IHANDLE_VALID(rtas_inst)) {		prom_printf("opening rtas package failed");		return;	}	prom_printf("instantiating rtas at 0x%x ...", base);	if (call_prom("call-method", 3, 2,		      ADDR("instantiate-rtas"),		      rtas_inst, 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(rtas_node, "linux,rtas-base", &base, sizeof(base));	prom_setprop(rtas_node, "linux,rtas-entry", &entry, sizeof(entry));	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, 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;		/*

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -