/*
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished proprietary information of BroadOn Communications Corp.,
 *  and are protected by federal copyright law. They may not be disclosed
 *  to third parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 *  Description:
 *      This file implements helper routines for MMU setup.
 *
 * $Id: mmu2.c,v 1.2 2008/04/08 18:14:48 cnguyen Exp $
 */

#include "mmu.h"

/*
 * This file contains the code to construct section, coarse page, or
 * fine page translation tables.  There are 4096 descriptors in the
 * primary translation table, indexed by the topmost 12 bits of the
 * virtual address, and they can be a mixture of these three types.
 * 
 * The main difference between these is that a section is a flat space
 * of 1MB.  Because a section is a flat address space we can use the
 * least 12 bits of the virtual address to index into it, so there is
 * no need to have a secondary page table.
 * 
 * For paged areas, however, the memory described by one translation
 * table entry can be allocated as pages, each with its own physical
 * location and permissions, so each translation table entry points to
 * a secondary page table, each entry of which describes a page.
 * 
 * For coarse pages each secondary page table contains up to 256
 * entries, indexed by bits [19:12] of the virtual address.  Coarse
 * page areas can have either large 64K or small 4KB pages.  The
 * memory addressed by a large page table entry is indexed by bits
 * [15:0] of the virtual address, and the memory described by a small
 * page table entry is indexed by bits [11:0] of the virtual adress.
 * 
 * For fine pages the secondary page tables contain up to 1024
 * entries, indexed by bits [19:10] of the virtual address.  Fine page
 * table descriptors can be backed by large or small page table
 * entries, as above.  In addition they can point to tiny page table
 * entries.  A tiny page is indexed by bits [9:0] of the virtual
 * address.
 */

/*
 * Write the first level translation table.  Writes a section
 * descriptor to each of 4096 entries with increasing base address.
 *
 * entry_addr:   the start address of the translation table.
 * start_offset: where to begin writing in the translation table.
 * how_many:     how many descriptors to write.
 * prototype:    descriptor with no address bits.
 */
void
setupSectionTransTable (unsigned int *entry_addr, int start_offset,
		        int how_many, unsigned int prototype)
{
    unsigned int ttb_entry;
    int entry;

    entry_addr += start_offset;

    for (entry = start_offset; entry < TRANS_TABLE_SIZE; entry++) {
        ttb_entry = prototype | (entry << 20);
	*entry_addr = ttb_entry;
	entry_addr++;
    }

    return;
}

/*
 * Write the first level translation table.  Writes a coarse page
 * descriptor to one of 4096 entries with increasing base address.
 *
 * entry_addr:   the start address of the translation table.
 * start_offset: where to begin writing in the translation table.
 * prototype:    descriptor with no address bits.
 * pt_addr:      address of secondary page table.
 */
void
setupCoarseTransTable (unsigned int *entry_addr, int start_offset,
                       unsigned int prototype, unsigned int *pt_addr)
{
    unsigned int ttb_entry;

    entry_addr += start_offset;
    ttb_entry = prototype | ((unsigned int)pt_addr & COARSE_ADDR_MASK);
    *entry_addr = ttb_entry;

    return;
}

/*
 * Write the first level translation table.  Writes a fine page
 * descriptor to oen of 4096 entries with increasing base address.
 *
 * entry_addr:   the start address of the translation table.
 * start_offset: where to begin writing in the translation table.
 * prototype:    descriptor with no address bits.
 * pt_addr:      address of secondary page table.
 */
void
setupFineTransTable (unsigned int *entry_addr, int start_offset,
                     unsigned int prototype, unsigned int *pt_addr)
{
    unsigned int ttb_entry;

    entry_addr += start_offset;
    ttb_entry = prototype | ((unsigned int)pt_addr & FINE_ADDR_MASK);
    *entry_addr = ttb_entry;

    return;
}

/*
 * Write the second level page table.  Writes a large, small or tiny
 * page descriptor to some of the pt entries with increasing base
 * address.
 *
 * entry_addr:   the start address of the page table.
 * start_offset: where to begin writing in the page table.
 * how_many:     how many page table entries to write.
 * prototype:    descriptor with no address bits.
 * c_bit:        cache bit.
 * b_bit:        buffer bit.
 * f_bit:        coarse/fine bit.
 * phys_addr:    physical address of page.
 */
void
setupPageTable (unsigned int *entry_addr, int start_offset,
                int how_many, unsigned int prototype, int c_bit, 
                int b_bit, int f_bit, unsigned char *phys_addr,
                unsigned int access)
{
    unsigned int pt_entry, phys_addr_mask, phys_addr_incr, access_mask;
    int entry, pt_size, pt_max;

    entry_addr += start_offset;
    pt_size = (f_bit == 0) ? COARSE_PT_SIZE : FINE_PT_SIZE;
    pt_max = (start_offset + how_many) < pt_size ? 
      (start_offset + how_many) : pt_size;

    switch (prototype & 0x3) {
    case 1:
        phys_addr_mask = LARGE_PT_MASK;
	phys_addr_incr = LARGE_PT_INCR;
	access_mask    = ((access & 0x3) << 10) | ((access & 0x3) << 8) |
	  ((access & 0x3) << 6) | ((access & 0x3) << 4);
        break;
    case 2:
        phys_addr_mask = SMALL_PT_MASK;
	phys_addr_incr = SMALL_PT_INCR;
	access_mask    = ((access & 0x3) << 10) | ((access & 0x3) << 8) |
          ((access & 0x3) << 6) | ((access & 0x3) << 4);
        break;
    case 3:
        phys_addr_mask = TINY_PT_MASK;
	phys_addr_incr = TINY_PT_INCR;
	access_mask    = ((access & 0x3) << 4);
        break;
    default:
        return;
    }

    for (entry = start_offset; entry < pt_max; entry++) {
        pt_entry = prototype | ((c_bit & 1) << 3) | ((b_bit & 1) << 2) |
	    ((unsigned int)phys_addr & phys_addr_mask) | access_mask;
	*entry_addr = pt_entry;
	phys_addr += phys_addr_incr; /* words? bytes? */
	entry_addr++;
    }

    return;
}

/* eof */

