#include #include #include #include #ifdef CONFIG_PROC_FS #include #endif #include #include #include #include #include #include #include #include #include #include struct himem_node { __u32 addr; __u32 len; int used; struct himem_node *next; struct himem_node *prev; }; /* Head of Himem List */ static struct himem_node *himem_head = NULL; #define VIA_MAX_HIMEM (256*1024*1024) #define SIZE_IN_KB(x) (x >> 10) static unsigned long dmask = 0; void via_get_himem (void); void via_free_mem (void); #ifdef MODULE_PARM MODULE_PARM (dmask, "l"); MODULE_PARM_DESC (dmask, "debug mask"); #endif #ifndef DEBUG_MASK #define DEBUG_MASK dmask #endif #define via_debug(mask,fmt,arg...) \ { if ((mask & DEBUG_MASK) == mask) via_printk(KERN_INFO,fmt,##arg); } #define via_printk(kern_level,fmt,arg...) \ printk(kern_level "mem1" "[" __FUNCTION__ "]: " fmt,##arg); #define via_info(fmt,arg...) via_printk(KERN_INFO,fmt,##arg); #define via_error(fmt,arg...) via_printk(KERN_ERR,fmt,##arg); struct mem1_buf { unsigned long addr; /* physical address/on-board offset */ void *virt; __u32 offset; __u32 length; }; static struct mem1_buf* stream_buf = NULL; static int num_stream_buf = 0; static int __init mem1_module_init (void) { int size = PAGE_ALIGN(768*576), free, cb; via_info("loading driver (dmask: 0x%lx)\n", dmask); via_get_himem(); /* setup himem bufs */ free = himem_head->len / size; via_info("setting up %d buffers of len : %d KB\n", free, size); stream_buf = kmalloc(free * sizeof(struct mem1_buf), GFP_KERNEL); num_stream_buf = free; if (stream_buf == NULL) { via_error("kmalloc(stream_buf[%d]) failed\n", free); return -ENOMEM; } for (cb = 0; cb < free; cb++) { stream_buf[cb].offset = cb*size; stream_buf[cb].length = size; stream_buf[cb].addr = himem_head->addr + cb*size; via_debug(1, "stream[%d] : off(0x%x) len(%d) addr(0x%p)\n", cb, stream_buf[cb].offset, stream_buf[cb].length, (void *)stream_buf[cb].addr); stream_buf[cb].virt = NULL; } via_info("remapping high memory\n"); for (cb = 0; cb < free; cb++) { stream_buf[cb].virt = ioremap(stream_buf[cb].addr, stream_buf[cb].length); if (stream_buf[cb].virt == NULL) { via_info("ioremap returned NULL for buffer # %d when %d was wanted\naddr/size->virt: 0x%lx/%x->0x%p\n", cb, free, stream_buf[cb].addr, stream_buf[cb].length, stream_buf[cb].virt); break; } via_debug(1, "addr/size->virt: 0x%lx/%x->0x%p\n", stream_buf[cb].addr, stream_buf[cb].length, stream_buf[cb].virt); } return 0; } static void __exit mem1_module_exit (void) { int cb; if (stream_buf != NULL) { via_info("unmapping %d bufs in high memory\n", num_stream_buf); for (cb = 0; cb < num_stream_buf; cb++) { if (stream_buf[cb].virt == NULL) continue; iounmap(stream_buf[cb].virt); via_info("stream_buf[%d].virt: 0x%p\n", cb, stream_buf[cb].virt); stream_buf[cb].virt = NULL; } kfree(stream_buf); } via_free_mem(); via_info("driver unloaded\n"); } /* * Search the high memory to find out how much is present. * * NOTE: * This is done in a non-cooperative way! I.e. we assume *ALL* high memory * is for our exclusive usage (any data in that area used by other resources * will be destroyed!). * * FIXME: This should be fixed using bigphysarea or other means. */ int himem_search (void) { __u32 himem_size; __u8 *tst_addr, twr, trd; if (himem_head != NULL) { via_error("High memory already in use.\n"); return -1; } /* allocate memory */ himem_head = (struct himem_node *) kmalloc (sizeof (struct himem_node), GFP_KERNEL); if (himem_head == NULL) { via_error("kmalloc() failed\n"); return -1; } /* high_memory points to memory beyond area that the kernel uses */ himem_head->addr = (__u32) virt_to_phys ((void *) high_memory); himem_size = 0; for (twr = 1; himem_size < VIA_MAX_HIMEM; twr++) { /* himem_size tested to prevent infinite loop */ tst_addr = ioremap ((__u32) (himem_head->addr + himem_size), PAGE_SIZE); via_debug(2, "himem addr/virt: 0x%x/0x%p\n", himem_head->addr + himem_size, tst_addr); if (tst_addr == NULL) break; writeb (twr, tst_addr); trd = twr - 1; trd = readb (tst_addr); iounmap (tst_addr); if (trd == twr) himem_size += PAGE_SIZE; else break; } himem_head->len = himem_size; himem_head->used = 0; himem_head->next = NULL; himem_head->prev = NULL; if (himem_size == 0) { via_error("no high memory found, use mem= option to kernel\n"); via_free_mem(); return -1; } else { via_info("found %d kB of high memory at 0x%lx (phys)\n", SIZE_IN_KB (himem_size), (unsigned long) himem_head->addr); } return himem_size; } /* * What we do here: * - assume, *all* himemory is for us * - thus, check how much himem is present * - spread all the available himem equally to all boards * * ... we miss a generic himem handler of the kernel here. */ void via_get_himem (void) { if ((himem_search() > 0) && (himem_head != NULL)) { via_info("himem_head->len:%d KB himem_head->addr:0x%x\n", SIZE_IN_KB(himem_head->len), himem_head->addr); } } /* free all memory used by memory handling functions */ void via_free_mem (void) { if (himem_head != NULL) { kfree (himem_head); himem_head = NULL; } } module_init(mem1_module_init); module_exit(mem1_module_exit); MODULE_AUTHOR("David Geldreich "); MODULE_DESCRIPTION("Test highmem usage"); MODULE_LICENSE("GPL");