]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | ** linux/amiga/chipram.c | |
4 | ** | |
5 | ** Modified 03-May-94 by Geert Uytterhoeven <geert@linux-m68k.org> | |
6 | ** - 64-bit aligned allocations for full AGA compatibility | |
7 | ** | |
8 | ** Rewritten 15/9/2000 by Geert to use resource management | |
9 | */ | |
10 | ||
1da177e4 LT |
11 | #include <linux/types.h> |
12 | #include <linux/kernel.h> | |
27ac792c | 13 | #include <linux/mm.h> |
1da177e4 LT |
14 | #include <linux/init.h> |
15 | #include <linux/ioport.h> | |
16 | #include <linux/slab.h> | |
17 | #include <linux/string.h> | |
8b169fa2 AB |
18 | #include <linux/module.h> |
19 | ||
cab49bc9 | 20 | #include <asm/atomic.h> |
1da177e4 LT |
21 | #include <asm/page.h> |
22 | #include <asm/amigahw.h> | |
23 | ||
24 | unsigned long amiga_chip_size; | |
8b169fa2 | 25 | EXPORT_SYMBOL(amiga_chip_size); |
1da177e4 LT |
26 | |
27 | static struct resource chipram_res = { | |
5be32463 | 28 | .name = "Chip RAM", .start = CHIP_PHYSADDR |
1da177e4 | 29 | }; |
cab49bc9 | 30 | static atomic_t chipavail; |
1da177e4 LT |
31 | |
32 | ||
33 | void __init amiga_chip_init(void) | |
34 | { | |
5be32463 GU |
35 | if (!AMIGAHW_PRESENT(CHIP_RAM)) |
36 | return; | |
1da177e4 | 37 | |
1dad6c7b | 38 | chipram_res.end = CHIP_PHYSADDR + amiga_chip_size - 1; |
5be32463 | 39 | request_resource(&iomem_resource, &chipram_res); |
1da177e4 | 40 | |
cab49bc9 | 41 | atomic_set(&chipavail, amiga_chip_size); |
1da177e4 LT |
42 | } |
43 | ||
44 | ||
45 | void *amiga_chip_alloc(unsigned long size, const char *name) | |
46 | { | |
5be32463 | 47 | struct resource *res; |
3a17bfa4 | 48 | void *p; |
1da177e4 | 49 | |
5be32463 GU |
50 | res = kzalloc(sizeof(struct resource), GFP_KERNEL); |
51 | if (!res) | |
52 | return NULL; | |
5be32463 | 53 | |
3a17bfa4 GU |
54 | res->name = name; |
55 | p = amiga_chip_alloc_res(size, res); | |
56 | if (!p) { | |
5be32463 GU |
57 | kfree(res); |
58 | return NULL; | |
59 | } | |
3a17bfa4 GU |
60 | |
61 | return p; | |
1da177e4 | 62 | } |
8b169fa2 | 63 | EXPORT_SYMBOL(amiga_chip_alloc); |
1da177e4 LT |
64 | |
65 | ||
5be32463 GU |
66 | /* |
67 | * Warning: | |
68 | * amiga_chip_alloc_res is meant only for drivers that need to | |
69 | * allocate Chip RAM before kmalloc() is functional. As a consequence, | |
70 | * those drivers must not free that Chip RAM afterwards. | |
71 | */ | |
1da177e4 | 72 | |
3a17bfa4 | 73 | void *amiga_chip_alloc_res(unsigned long size, struct resource *res) |
1da177e4 | 74 | { |
3a17bfa4 | 75 | int error; |
1da177e4 | 76 | |
5be32463 GU |
77 | /* round up */ |
78 | size = PAGE_ALIGN(size); | |
1da177e4 | 79 | |
b4f6f453 | 80 | pr_debug("amiga_chip_alloc_res: allocate %lu bytes\n", size); |
3a17bfa4 GU |
81 | error = allocate_resource(&chipram_res, res, size, 0, UINT_MAX, |
82 | PAGE_SIZE, NULL, NULL); | |
83 | if (error < 0) { | |
84 | pr_err("amiga_chip_alloc_res: allocate_resource() failed %d!\n", | |
85 | error); | |
86 | return NULL; | |
5be32463 | 87 | } |
3a17bfa4 | 88 | |
cab49bc9 | 89 | atomic_sub(size, &chipavail); |
b4f6f453 | 90 | pr_debug("amiga_chip_alloc_res: returning %pR\n", res); |
6112ea08 | 91 | return ZTWO_VADDR(res->start); |
1da177e4 LT |
92 | } |
93 | ||
94 | void amiga_chip_free(void *ptr) | |
95 | { | |
5be32463 | 96 | unsigned long start = ZTWO_PADDR(ptr); |
b7785e95 | 97 | struct resource *res; |
5be32463 GU |
98 | unsigned long size; |
99 | ||
b7785e95 GU |
100 | res = lookup_resource(&chipram_res, start); |
101 | if (!res) { | |
102 | pr_err("amiga_chip_free: trying to free nonexistent region at " | |
103 | "%p\n", ptr); | |
5be32463 GU |
104 | return; |
105 | } | |
b7785e95 GU |
106 | |
107 | size = resource_size(res); | |
108 | pr_debug("amiga_chip_free: free %lu bytes at %p\n", size, ptr); | |
109 | atomic_add(size, &chipavail); | |
110 | release_resource(res); | |
111 | kfree(res); | |
1da177e4 | 112 | } |
8b169fa2 | 113 | EXPORT_SYMBOL(amiga_chip_free); |
1da177e4 LT |
114 | |
115 | ||
116 | unsigned long amiga_chip_avail(void) | |
117 | { | |
cab49bc9 GU |
118 | unsigned long n = atomic_read(&chipavail); |
119 | ||
120 | pr_debug("amiga_chip_avail : %lu bytes\n", n); | |
121 | return n; | |
1da177e4 | 122 | } |
8b169fa2 AB |
123 | EXPORT_SYMBOL(amiga_chip_avail); |
124 |