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; |
Geert Uytterhoeven | 3a17bfa | 2011-04-24 23:19:05 +0200 | [diff] [blame^] | 46 | void *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 48 | res = kzalloc(sizeof(struct resource), GFP_KERNEL); |
| 49 | if (!res) |
| 50 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Geert Uytterhoeven | 3a17bfa | 2011-04-24 23:19:05 +0200 | [diff] [blame^] | 52 | res->name = name; |
| 53 | p = amiga_chip_alloc_res(size, res); |
| 54 | if (!p) { |
Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 55 | kfree(res); |
| 56 | return NULL; |
| 57 | } |
Geert Uytterhoeven | 3a17bfa | 2011-04-24 23:19:05 +0200 | [diff] [blame^] | 58 | |
| 59 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
Adrian Bunk | 8b169fa | 2008-02-04 22:30:25 -0800 | [diff] [blame] | 61 | EXPORT_SYMBOL(amiga_chip_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
| 63 | |
Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 64 | /* |
| 65 | * Warning: |
| 66 | * amiga_chip_alloc_res is meant only for drivers that need to |
| 67 | * allocate Chip RAM before kmalloc() is functional. As a consequence, |
| 68 | * those drivers must not free that Chip RAM afterwards. |
| 69 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Geert Uytterhoeven | 3a17bfa | 2011-04-24 23:19:05 +0200 | [diff] [blame^] | 71 | void *amiga_chip_alloc_res(unsigned long size, struct resource *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
Geert Uytterhoeven | 3a17bfa | 2011-04-24 23:19:05 +0200 | [diff] [blame^] | 73 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 75 | /* round up */ |
| 76 | size = PAGE_ALIGN(size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Geert Uytterhoeven | b4f6f45 | 2011-04-24 22:55:20 +0200 | [diff] [blame] | 78 | pr_debug("amiga_chip_alloc_res: allocate %lu bytes\n", size); |
Geert Uytterhoeven | 3a17bfa | 2011-04-24 23:19:05 +0200 | [diff] [blame^] | 79 | error = allocate_resource(&chipram_res, res, size, 0, UINT_MAX, |
| 80 | PAGE_SIZE, NULL, NULL); |
| 81 | if (error < 0) { |
| 82 | pr_err("amiga_chip_alloc_res: allocate_resource() failed %d!\n", |
| 83 | error); |
| 84 | return NULL; |
Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 85 | } |
Geert Uytterhoeven | 3a17bfa | 2011-04-24 23:19:05 +0200 | [diff] [blame^] | 86 | |
Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 87 | chipavail -= size; |
Geert Uytterhoeven | b4f6f45 | 2011-04-24 22:55:20 +0200 | [diff] [blame] | 88 | pr_debug("amiga_chip_alloc_res: returning %pR\n", res); |
Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 89 | return (void *)ZTWO_VADDR(res->start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void amiga_chip_free(void *ptr) |
| 93 | { |
Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 94 | unsigned long start = ZTWO_PADDR(ptr); |
| 95 | struct resource **p, *res; |
| 96 | unsigned long size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 98 | for (p = &chipram_res.child; (res = *p); p = &res->sibling) { |
| 99 | if (res->start != start) |
| 100 | continue; |
| 101 | *p = res->sibling; |
| 102 | size = res->end-start; |
Geert Uytterhoeven | b4f6f45 | 2011-04-24 22:55:20 +0200 | [diff] [blame] | 103 | 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] | 104 | chipavail += size; |
| 105 | kfree(res); |
| 106 | return; |
| 107 | } |
Geert Uytterhoeven | b4f6f45 | 2011-04-24 22:55:20 +0200 | [diff] [blame] | 108 | 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] | 109 | ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
Adrian Bunk | 8b169fa | 2008-02-04 22:30:25 -0800 | [diff] [blame] | 111 | EXPORT_SYMBOL(amiga_chip_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | |
| 114 | unsigned long amiga_chip_avail(void) |
| 115 | { |
Geert Uytterhoeven | b4f6f45 | 2011-04-24 22:55:20 +0200 | [diff] [blame] | 116 | pr_debug("amiga_chip_avail : %lu bytes\n", chipavail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | return chipavail; |
| 118 | } |
Adrian Bunk | 8b169fa | 2008-02-04 22:30:25 -0800 | [diff] [blame] | 119 | EXPORT_SYMBOL(amiga_chip_avail); |
| 120 | |