Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** linux/amiga/chipram.c |
| 3 | ** |
| 4 | ** Modified 03-May-94 by Geert Uytterhoeven <geert@linux-m68k.org> |
| 5 | ** - 64-bit aligned allocations for full AGA compatibility |
| 6 | ** |
| 7 | ** Rewritten 15/9/2000 by Geert to use resource management |
| 8 | */ |
| 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/types.h> |
| 11 | #include <linux/kernel.h> |
Andrea Righi | 27ac792 | 2008-07-23 21:28:13 -0700 | [diff] [blame] | 12 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/init.h> |
| 14 | #include <linux/ioport.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/string.h> |
Adrian Bunk | 8b169fa | 2008-02-04 22:30:25 -0800 | [diff] [blame] | 17 | #include <linux/module.h> |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/page.h> |
| 20 | #include <asm/amigahw.h> |
| 21 | |
| 22 | unsigned long amiga_chip_size; |
Adrian Bunk | 8b169fa | 2008-02-04 22:30:25 -0800 | [diff] [blame] | 23 | EXPORT_SYMBOL(amiga_chip_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | static struct resource chipram_res = { |
Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 26 | .name = "Chip RAM", .start = CHIP_PHYSADDR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | }; |
| 28 | static unsigned long chipavail; |
| 29 | |
| 30 | |
| 31 | void __init amiga_chip_init(void) |
| 32 | { |
Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 33 | if (!AMIGAHW_PRESENT(CHIP_RAM)) |
| 34 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 36 | chipram_res.end = amiga_chip_size-1; |
| 37 | request_resource(&iomem_resource, &chipram_res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 39 | chipavail = amiga_chip_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | |
| 43 | void *amiga_chip_alloc(unsigned long size, const char *name) |
| 44 | { |
Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 45 | struct resource *res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 47 | /* round up */ |
| 48 | size = PAGE_ALIGN(size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Geert Uytterhoeven | b4f6f45 | 2011-04-24 22:55:20 +0200 | [diff] [blame^] | 50 | pr_debug("amiga_chip_alloc: allocate %lu bytes\n", size); |
Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 51 | res = kzalloc(sizeof(struct resource), GFP_KERNEL); |
| 52 | if (!res) |
| 53 | return NULL; |
| 54 | res->name = name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 56 | if (allocate_resource(&chipram_res, res, size, 0, UINT_MAX, PAGE_SIZE, |
| 57 | NULL, NULL) < 0) { |
| 58 | kfree(res); |
| 59 | return NULL; |
| 60 | } |
| 61 | chipavail -= size; |
Geert Uytterhoeven | b4f6f45 | 2011-04-24 22:55:20 +0200 | [diff] [blame^] | 62 | pr_debug("amiga_chip_alloc: returning %pR\n", res); |
Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 63 | return (void *)ZTWO_VADDR(res->start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | } |
Adrian Bunk | 8b169fa | 2008-02-04 22:30:25 -0800 | [diff] [blame] | 65 | EXPORT_SYMBOL(amiga_chip_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
| 67 | |
Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 68 | /* |
| 69 | * Warning: |
| 70 | * amiga_chip_alloc_res is meant only for drivers that need to |
| 71 | * allocate Chip RAM before kmalloc() is functional. As a consequence, |
| 72 | * those drivers must not free that Chip RAM afterwards. |
| 73 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
| 75 | void * __init amiga_chip_alloc_res(unsigned long size, struct resource *res) |
| 76 | { |
Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 77 | unsigned long start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 79 | /* round up */ |
| 80 | size = PAGE_ALIGN(size); |
| 81 | /* dmesg into chipmem prefers memory at the safe end */ |
| 82 | start = CHIP_PHYSADDR + chipavail - size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Geert Uytterhoeven | b4f6f45 | 2011-04-24 22:55:20 +0200 | [diff] [blame^] | 84 | pr_debug("amiga_chip_alloc_res: allocate %lu bytes\n", size); |
Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 85 | if (allocate_resource(&chipram_res, res, size, start, UINT_MAX, |
| 86 | PAGE_SIZE, NULL, NULL) < 0) { |
Geert Uytterhoeven | b4f6f45 | 2011-04-24 22:55:20 +0200 | [diff] [blame^] | 87 | pr_err("amiga_chip_alloc_res: first alloc failed!\n"); |
Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 88 | if (allocate_resource(&chipram_res, res, size, 0, UINT_MAX, |
| 89 | PAGE_SIZE, NULL, NULL) < 0) |
| 90 | return NULL; |
| 91 | } |
| 92 | chipavail -= size; |
Geert Uytterhoeven | b4f6f45 | 2011-04-24 22:55:20 +0200 | [diff] [blame^] | 93 | pr_debug("amiga_chip_alloc_res: returning %pR\n", res); |
Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 94 | return (void *)ZTWO_VADDR(res->start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void amiga_chip_free(void *ptr) |
| 98 | { |
Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 99 | unsigned long start = ZTWO_PADDR(ptr); |
| 100 | struct resource **p, *res; |
| 101 | unsigned long size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 103 | for (p = &chipram_res.child; (res = *p); p = &res->sibling) { |
| 104 | if (res->start != start) |
| 105 | continue; |
| 106 | *p = res->sibling; |
| 107 | size = res->end-start; |
Geert Uytterhoeven | b4f6f45 | 2011-04-24 22:55:20 +0200 | [diff] [blame^] | 108 | pr_debug("amiga_chip_free: free %lu bytes at %p\n", size, ptr); |
Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 109 | chipavail += size; |
| 110 | kfree(res); |
| 111 | return; |
| 112 | } |
Geert Uytterhoeven | b4f6f45 | 2011-04-24 22:55:20 +0200 | [diff] [blame^] | 113 | pr_err("amiga_chip_free: trying to free nonexistent region at %p\n", |
Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 114 | ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
Adrian Bunk | 8b169fa | 2008-02-04 22:30:25 -0800 | [diff] [blame] | 116 | EXPORT_SYMBOL(amiga_chip_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 118 | |
| 119 | unsigned long amiga_chip_avail(void) |
| 120 | { |
Geert Uytterhoeven | b4f6f45 | 2011-04-24 22:55:20 +0200 | [diff] [blame^] | 121 | pr_debug("amiga_chip_avail : %lu bytes\n", chipavail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | return chipavail; |
| 123 | } |
Adrian Bunk | 8b169fa | 2008-02-04 22:30:25 -0800 | [diff] [blame] | 124 | EXPORT_SYMBOL(amiga_chip_avail); |
| 125 | |