/* * jazzdma.c * * Mips Jazz DMA controller support * (C) 1995 Andreas Busse * * NOTE: Some of the argument checking could be removed when * things have settled down. Also, instead of returning 0xffffffff * on failure of vdma_alloc() one could leave page #0 unused * and return the more usual NULL pointer as logical address. * */ #include #include #include #include #include #include #include #include #include static unsigned long vdma_pagetable_start = 0; static unsigned long vdma_pagetable_end = 0; /* * Debug stuff */ #define vdma_debug ((CONF_DEBUG_VDMA) ? debuglvl : 0) static int debuglvl = 3; /* * Local prototypes */ static void vdma_pgtbl_init(void); /* * Initialize the Jazz R4030 dma controller */ unsigned long vdma_init(unsigned long memory_start, unsigned long memory_end) { /* * Allocate 32k of memory for DMA page tables. * This needs to be page aligned and should be * uncached to avoid cache flushing after every * update. */ vdma_pagetable_start = KSEG1ADDR((memory_start + 4095) & ~4095); vdma_pagetable_end = vdma_pagetable_start + VDMA_PGTBL_SIZE; /* * Clear the R4030 translation table */ vdma_pgtbl_init(); r4030_write_reg32(JAZZ_R4030_TRSTBL_BASE,PHYSADDR(vdma_pagetable_start)); r4030_write_reg32(JAZZ_R4030_TRSTBL_LIM,VDMA_PGTBL_SIZE); r4030_write_reg32(JAZZ_R4030_TRSTBL_INV,0); printk("VDMA: R4030 DMA pagetables initialized.\n"); return KSEG0ADDR(vdma_pagetable_end); } /* * Allocate DMA pagetables using a simple first-fit algorithm */ unsigned long vdma_alloc(unsigned long paddr, unsigned long size) { VDMA_PGTBL_ENTRY *entry = (VDMA_PGTBL_ENTRY *)vdma_pagetable_start; int first; int last; int pages; unsigned int frame; unsigned long laddr; int i; /* check arguments */ if (paddr > 0x1fffffff) { if (vdma_debug) printk("vdma_alloc: Invalid physical address: %08lx\n",paddr); return VDMA_ERROR; /* invalid physical address */ } if (size > 0x400000 || size == 0) { if (vdma_debug) printk("vdma_alloc: Invalid size: %08lx\n",size); return VDMA_ERROR; /* invalid physical address */ } /* find free chunk */ pages = (size + 4095) >> 12; /* no. of pages to allocate */ first = 0; while (1) { while (entry[first].owner != VDMA_PAGE_EMPTY && first < VDMA_PGTBL_ENTRIES) first++; if (first+pages > VDMA_PGTBL_ENTRIES) /* nothing free */ return VDMA_ERROR; last = first+1; while (entry[last].owner == VDMA_PAGE_EMPTY && last-first < pages) last++; if (last-first == pages) break; /* found */ } /* mark pages as allocated */ laddr = (first << 12) + (paddr & (VDMA_PAGESIZE-1)); frame = paddr & ~(VDMA_PAGESIZE-1); for (i=first; i 1) printk("vdma_alloc: Allocated %d pages starting from %08lx\n", pages,laddr); if (vdma_debug > 2) { printk("LADDR: "); for (i=first; i> 12; if (pgtbl[i].owner != laddr) { printk("vdma_free: trying to free other's dma pages, laddr=%8lx\n", laddr); return -1; } while (pgtbl[i].owner == laddr && i < VDMA_PGTBL_ENTRIES) { pgtbl[i].owner = VDMA_PAGE_EMPTY; i++; } if (vdma_debug > 1) printk("vdma_free: freed %ld pages starting from %08lx\n", i-(laddr>>12),laddr); return 0; } /* * Map certain page(s) to another physical address. * Caller must have allocated the page(s) before. */ int vdma_remap(unsigned long laddr, unsigned long paddr, unsigned long size) { VDMA_PGTBL_ENTRY *pgtbl = (VDMA_PGTBL_ENTRY *)vdma_pagetable_start; int first; int pages; if (laddr > 0xffffff) { if (vdma_debug) printk("vdma_map: Invalid logical address: %08lx\n",laddr); return -EINVAL; /* invalid logical address */ } if (paddr > 0x1fffffff) { if (vdma_debug) printk("vdma_map: Invalid physical address: %08lx\n",paddr); return -EINVAL; /* invalid physical address */ } pages = (((paddr & (VDMA_PAGESIZE-1)) + size) >> 12) + 1; first = laddr >> 12; if (vdma_debug) printk("vdma_remap: first=%x, pages=%x\n",first,pages); if (first+pages > VDMA_PGTBL_ENTRIES) { if (vdma_debug) printk("vdma_alloc: Invalid size: %08lx\n",size); return -EINVAL; } paddr &= ~(VDMA_PAGESIZE-1); while (pages > 0 && first < VDMA_PGTBL_ENTRIES) { if (pgtbl[first].owner != laddr) { if (vdma_debug) printk("Trying to remap other's pages.\n"); return -EPERM; /* not owner */ } pgtbl[first].frame = paddr; paddr += VDMA_PAGESIZE; first++; pages--; } /* update translation table */ r4030_write_reg32(JAZZ_R4030_TRSTBL_INV,0); if (vdma_debug > 2) { int i; pages = (((paddr & (VDMA_PAGESIZE-1)) + size) >> 12) + 1; first = laddr >> 12; printk("LADDR: "); for (i=first; i> 12].frame + (laddr & (VDMA_PAGESIZE-1)); } /* * Initialize the pagetable with a one-to-one mapping of * the first 16 Mbytes of main memory and declare all * entries to be unused. Using this method will at least * allow some early device driver operations to work. */ static void vdma_pgtbl_init(void) { int i; unsigned long paddr = 0; VDMA_PGTBL_ENTRY *pgtbl = (VDMA_PGTBL_ENTRY *)vdma_pagetable_start; for (i=0; i