| 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 |  | 
| Geert Uytterhoeven | cab49bc9 | 2011-04-24 23:40:51 +0200 | [diff] [blame] | 19 | #include <asm/atomic.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <asm/page.h> | 
|  | 21 | #include <asm/amigahw.h> | 
|  | 22 |  | 
|  | 23 | unsigned long amiga_chip_size; | 
| Adrian Bunk | 8b169fa | 2008-02-04 22:30:25 -0800 | [diff] [blame] | 24 | EXPORT_SYMBOL(amiga_chip_size); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 |  | 
|  | 26 | static struct resource chipram_res = { | 
| Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 27 | .name = "Chip RAM", .start = CHIP_PHYSADDR | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | }; | 
| Geert Uytterhoeven | cab49bc9 | 2011-04-24 23:40:51 +0200 | [diff] [blame] | 29 | static atomic_t chipavail; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 |  | 
|  | 31 |  | 
|  | 32 | void __init amiga_chip_init(void) | 
|  | 33 | { | 
| Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 34 | if (!AMIGAHW_PRESENT(CHIP_RAM)) | 
|  | 35 | return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 |  | 
| Geert Uytterhoeven | 1dad6c7 | 2011-05-22 11:09:02 +0200 | [diff] [blame] | 37 | chipram_res.end = CHIP_PHYSADDR + amiga_chip_size - 1; | 
| Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 38 | request_resource(&iomem_resource, &chipram_res); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 |  | 
| Geert Uytterhoeven | cab49bc9 | 2011-04-24 23:40:51 +0200 | [diff] [blame] | 40 | atomic_set(&chipavail, amiga_chip_size); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | } | 
|  | 42 |  | 
|  | 43 |  | 
|  | 44 | void *amiga_chip_alloc(unsigned long size, const char *name) | 
|  | 45 | { | 
| Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 46 | struct resource *res; | 
| Geert Uytterhoeven | 3a17bfa | 2011-04-24 23:19:05 +0200 | [diff] [blame] | 47 | void *p; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 |  | 
| Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 49 | res = kzalloc(sizeof(struct resource), GFP_KERNEL); | 
|  | 50 | if (!res) | 
|  | 51 | return NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 |  | 
| Geert Uytterhoeven | 3a17bfa | 2011-04-24 23:19:05 +0200 | [diff] [blame] | 53 | res->name = name; | 
|  | 54 | p = amiga_chip_alloc_res(size, res); | 
|  | 55 | if (!p) { | 
| Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 56 | kfree(res); | 
|  | 57 | return NULL; | 
|  | 58 | } | 
| Geert Uytterhoeven | 3a17bfa | 2011-04-24 23:19:05 +0200 | [diff] [blame] | 59 |  | 
|  | 60 | return p; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | } | 
| Adrian Bunk | 8b169fa | 2008-02-04 22:30:25 -0800 | [diff] [blame] | 62 | EXPORT_SYMBOL(amiga_chip_alloc); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 |  | 
|  | 64 |  | 
| Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 65 | /* | 
|  | 66 | *  Warning: | 
|  | 67 | *  amiga_chip_alloc_res is meant only for drivers that need to | 
|  | 68 | *  allocate Chip RAM before kmalloc() is functional. As a consequence, | 
|  | 69 | *  those drivers must not free that Chip RAM afterwards. | 
|  | 70 | */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 |  | 
| Geert Uytterhoeven | 3a17bfa | 2011-04-24 23:19:05 +0200 | [diff] [blame] | 72 | void *amiga_chip_alloc_res(unsigned long size, struct resource *res) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | { | 
| Geert Uytterhoeven | 3a17bfa | 2011-04-24 23:19:05 +0200 | [diff] [blame] | 74 | int error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 |  | 
| Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 76 | /* round up */ | 
|  | 77 | size = PAGE_ALIGN(size); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 |  | 
| Geert Uytterhoeven | b4f6f45 | 2011-04-24 22:55:20 +0200 | [diff] [blame] | 79 | pr_debug("amiga_chip_alloc_res: allocate %lu bytes\n", size); | 
| Geert Uytterhoeven | 3a17bfa | 2011-04-24 23:19:05 +0200 | [diff] [blame] | 80 | error = allocate_resource(&chipram_res, res, size, 0, UINT_MAX, | 
|  | 81 | PAGE_SIZE, NULL, NULL); | 
|  | 82 | if (error < 0) { | 
|  | 83 | pr_err("amiga_chip_alloc_res: allocate_resource() failed %d!\n", | 
|  | 84 | error); | 
|  | 85 | return NULL; | 
| Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 86 | } | 
| Geert Uytterhoeven | 3a17bfa | 2011-04-24 23:19:05 +0200 | [diff] [blame] | 87 |  | 
| Geert Uytterhoeven | cab49bc9 | 2011-04-24 23:40:51 +0200 | [diff] [blame] | 88 | atomic_sub(size, &chipavail); | 
| Geert Uytterhoeven | b4f6f45 | 2011-04-24 22:55:20 +0200 | [diff] [blame] | 89 | pr_debug("amiga_chip_alloc_res: returning %pR\n", res); | 
| Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 90 | return (void *)ZTWO_VADDR(res->start); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } | 
|  | 92 |  | 
|  | 93 | void amiga_chip_free(void *ptr) | 
|  | 94 | { | 
| Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 95 | unsigned long start = ZTWO_PADDR(ptr); | 
| Geert Uytterhoeven | b7785e9 | 2011-05-07 20:56:00 +0200 | [diff] [blame] | 96 | struct resource *res; | 
| Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 97 | unsigned long size; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 |  | 
| Geert Uytterhoeven | b7785e9 | 2011-05-07 20:56:00 +0200 | [diff] [blame] | 99 | res = lookup_resource(&chipram_res, start); | 
|  | 100 | if (!res) { | 
|  | 101 | pr_err("amiga_chip_free: trying to free nonexistent region at " | 
|  | 102 | "%p\n", ptr); | 
| Geert Uytterhoeven | 5be3246 | 2011-05-21 20:46:39 +0200 | [diff] [blame] | 103 | return; | 
|  | 104 | } | 
| Geert Uytterhoeven | b7785e9 | 2011-05-07 20:56:00 +0200 | [diff] [blame] | 105 |  | 
|  | 106 | size = resource_size(res); | 
|  | 107 | pr_debug("amiga_chip_free: free %lu bytes at %p\n", size, ptr); | 
|  | 108 | atomic_add(size, &chipavail); | 
|  | 109 | release_resource(res); | 
|  | 110 | kfree(res); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } | 
| Adrian Bunk | 8b169fa | 2008-02-04 22:30:25 -0800 | [diff] [blame] | 112 | EXPORT_SYMBOL(amiga_chip_free); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 |  | 
|  | 114 |  | 
|  | 115 | unsigned long amiga_chip_avail(void) | 
|  | 116 | { | 
| Geert Uytterhoeven | cab49bc9 | 2011-04-24 23:40:51 +0200 | [diff] [blame] | 117 | unsigned long n = atomic_read(&chipavail); | 
|  | 118 |  | 
|  | 119 | pr_debug("amiga_chip_avail : %lu bytes\n", n); | 
|  | 120 | return n; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } | 
| Adrian Bunk | 8b169fa | 2008-02-04 22:30:25 -0800 | [diff] [blame] | 122 | EXPORT_SYMBOL(amiga_chip_avail); | 
|  | 123 |  |