blob: ba03cec3f711e024422fe916a407b4cf56556f76 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/types.h>
11#include <linux/kernel.h>
Andrea Righi27ac7922008-07-23 21:28:13 -070012#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/init.h>
14#include <linux/ioport.h>
15#include <linux/slab.h>
16#include <linux/string.h>
Adrian Bunk8b169fa2008-02-04 22:30:25 -080017#include <linux/module.h>
18
Geert Uytterhoevencab49bc92011-04-24 23:40:51 +020019#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/page.h>
21#include <asm/amigahw.h>
22
23unsigned long amiga_chip_size;
Adrian Bunk8b169fa2008-02-04 22:30:25 -080024EXPORT_SYMBOL(amiga_chip_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26static struct resource chipram_res = {
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020027 .name = "Chip RAM", .start = CHIP_PHYSADDR
Linus Torvalds1da177e2005-04-16 15:20:36 -070028};
Geert Uytterhoevencab49bc92011-04-24 23:40:51 +020029static atomic_t chipavail;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31
32void __init amiga_chip_init(void)
33{
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020034 if (!AMIGAHW_PRESENT(CHIP_RAM))
35 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Geert Uytterhoeven1dad6c72011-05-22 11:09:02 +020037 chipram_res.end = CHIP_PHYSADDR + amiga_chip_size - 1;
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020038 request_resource(&iomem_resource, &chipram_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Geert Uytterhoevencab49bc92011-04-24 23:40:51 +020040 atomic_set(&chipavail, amiga_chip_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041}
42
43
44void *amiga_chip_alloc(unsigned long size, const char *name)
45{
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020046 struct resource *res;
Geert Uytterhoeven3a17bfa2011-04-24 23:19:05 +020047 void *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020049 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
50 if (!res)
51 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Geert Uytterhoeven3a17bfa2011-04-24 23:19:05 +020053 res->name = name;
54 p = amiga_chip_alloc_res(size, res);
55 if (!p) {
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020056 kfree(res);
57 return NULL;
58 }
Geert Uytterhoeven3a17bfa2011-04-24 23:19:05 +020059
60 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
Adrian Bunk8b169fa2008-02-04 22:30:25 -080062EXPORT_SYMBOL(amiga_chip_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020065 /*
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 Torvalds1da177e2005-04-16 15:20:36 -070071
Geert Uytterhoeven3a17bfa2011-04-24 23:19:05 +020072void *amiga_chip_alloc_res(unsigned long size, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Geert Uytterhoeven3a17bfa2011-04-24 23:19:05 +020074 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020076 /* round up */
77 size = PAGE_ALIGN(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Geert Uytterhoevenb4f6f452011-04-24 22:55:20 +020079 pr_debug("amiga_chip_alloc_res: allocate %lu bytes\n", size);
Geert Uytterhoeven3a17bfa2011-04-24 23:19:05 +020080 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 Uytterhoeven5be32462011-05-21 20:46:39 +020086 }
Geert Uytterhoeven3a17bfa2011-04-24 23:19:05 +020087
Geert Uytterhoevencab49bc92011-04-24 23:40:51 +020088 atomic_sub(size, &chipavail);
Geert Uytterhoevenb4f6f452011-04-24 22:55:20 +020089 pr_debug("amiga_chip_alloc_res: returning %pR\n", res);
Geert Uytterhoeven6112ea02011-01-09 11:03:43 +010090 return ZTWO_VADDR(res->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93void amiga_chip_free(void *ptr)
94{
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020095 unsigned long start = ZTWO_PADDR(ptr);
Geert Uytterhoevenb7785e92011-05-07 20:56:00 +020096 struct resource *res;
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020097 unsigned long size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Geert Uytterhoevenb7785e92011-05-07 20:56:00 +020099 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 Uytterhoeven5be32462011-05-21 20:46:39 +0200103 return;
104 }
Geert Uytterhoevenb7785e92011-05-07 20:56:00 +0200105
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 Torvalds1da177e2005-04-16 15:20:36 -0700111}
Adrian Bunk8b169fa2008-02-04 22:30:25 -0800112EXPORT_SYMBOL(amiga_chip_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114
115unsigned long amiga_chip_avail(void)
116{
Geert Uytterhoevencab49bc92011-04-24 23:40:51 +0200117 unsigned long n = atomic_read(&chipavail);
118
119 pr_debug("amiga_chip_avail : %lu bytes\n", n);
120 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
Adrian Bunk8b169fa2008-02-04 22:30:25 -0800122EXPORT_SYMBOL(amiga_chip_avail);
123