/*
 * arch/arm/mm/mm-armv.c
 *
 * Common routines for ARM v3 and v4 architectures.
 *
 * Copyright (C) 1998 Russell King
 *
 * Do not compile this file directly!
 */

#ifndef MAPPING
#error MAPPING not defined - do not compile this file individually
#endif

static const struct mapping {
	unsigned long virtual;
	unsigned long physical;
	unsigned long length;
	int domain:4,
	    prot_read:1,
	    prot_write:1;
} mapping[] __initdata = {
	MAPPING
};

#define SIZEOFMAP (sizeof(mapping) / sizeof(mapping[0]))

__initfunc(unsigned long setup_io_pagetables(unsigned long start_mem))
{
	const struct mapping *mp;
	int i;

	for (i = 0, mp = mapping; i < SIZEOFMAP; i++, mp++) {
		unsigned long virtual, physical, length;
		int prot;

		virtual = mp->virtual;
		physical = mp->physical;
		length = mp->length;
		prot = (mp->prot_read ? L_PTE_USER : 0) | (mp->prot_write ? L_PTE_WRITE : 0)
			| L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY;

		while ((virtual & 1048575 || physical & 1048575) && length >= PAGE_SIZE) {
			alloc_init_page(&start_mem, virtual, physical, mp->domain, prot);
			length -= PAGE_SIZE;
			virtual += PAGE_SIZE;
			physical += PAGE_SIZE;
		}

		prot = (mp->prot_read ? PMD_SECT_AP_READ : 0) |
		       (mp->prot_write ? PMD_SECT_AP_WRITE : 0);

		while (length >= 1048576) {
			alloc_init_section(&start_mem, virtual, physical, mp->domain, prot);
			length -= 1048576;
			virtual += 1048576;
			physical += 1048576;
		}

		prot = (mp->prot_read ? L_PTE_USER : 0) | (mp->prot_write ? L_PTE_WRITE : 0)
			| L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY;

		while (length >= PAGE_SIZE) {
			alloc_init_page(&start_mem, virtual, physical, mp->domain, prot);
			length -= PAGE_SIZE;
			virtual += PAGE_SIZE;
			physical += PAGE_SIZE;
		}
	}

	return start_mem;
}

/*
 * In order to soft-boot, we need to insert a 1:1 mapping in place of
 * the user-mode pages.  This will then ensure that we have predictable
 * results when turning the mmu off
 */
void setup_mm_for_reboot(char mode)
{
	pgd_t *pgd;
	pmd_t pmd;
	int i;

	if (current->mm && current->mm->pgd)
		pgd = current->mm->pgd;
	else
		pgd = init_mm.pgd;

	for (i = 0; i < USER_PTRS_PER_PGD; i++) {
		pmd_val(pmd) = (i << PAGE_SHIFT) |
			PMD_SECT_AP_WRITE | PMD_SECT_AP_READ |
			PMD_TYPE_SECT;
		set_pmd(pmd_offset(pgd + i, i << PGDIR_SHIFT), pmd);
	}
}
