1 // SPDX-License-Identifier: GPL-2.0
3 ** linux/amiga/chipram.c
6 ** - 64-bit aligned allocations for full AGA compatibility
8 ** Rewritten 15/9/2000 by Geert to use resource management
11 #include <linux/types.h>
12 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/ioport.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18 #include <linux/module.h>
20 #include <asm/atomic.h>
22 #include <asm/amigahw.h>
24 unsigned long amiga_chip_size;
25 EXPORT_SYMBOL(amiga_chip_size);
27 static struct resource chipram_res = {
28 .name = "Chip RAM", .start = CHIP_PHYSADDR
30 static atomic_t chipavail;
33 void __init amiga_chip_init(void)
35 if (!AMIGAHW_PRESENT(CHIP_RAM))
38 chipram_res.end = CHIP_PHYSADDR + amiga_chip_size - 1;
39 request_resource(&iomem_resource, &chipram_res);
41 atomic_set(&chipavail, amiga_chip_size);
45 void *amiga_chip_alloc(unsigned long size, const char *name)
50 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
55 p = amiga_chip_alloc_res(size, res);
63 EXPORT_SYMBOL(amiga_chip_alloc);
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.
73 void *amiga_chip_alloc_res(unsigned long size, struct resource *res)
78 size = PAGE_ALIGN(size);
80 pr_debug("amiga_chip_alloc_res: allocate %lu bytes\n", size);
81 error = allocate_resource(&chipram_res, res, size, 0, UINT_MAX,
82 PAGE_SIZE, NULL, NULL);
84 pr_err("amiga_chip_alloc_res: allocate_resource() failed %d!\n",
89 atomic_sub(size, &chipavail);
90 pr_debug("amiga_chip_alloc_res: returning %pR\n", res);
91 return ZTWO_VADDR(res->start);
94 void amiga_chip_free(void *ptr)
96 unsigned long start = ZTWO_PADDR(ptr);
100 res = lookup_resource(&chipram_res, start);
102 pr_err("amiga_chip_free: trying to free nonexistent region at "
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);
113 EXPORT_SYMBOL(amiga_chip_free);
116 unsigned long amiga_chip_avail(void)
118 unsigned long n = atomic_read(&chipavail);
120 pr_debug("amiga_chip_avail : %lu bytes\n", n);
123 EXPORT_SYMBOL(amiga_chip_avail);