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