blob: e757ffffbc8ae5745948252941d202768f1f8631 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/page.h>
20#include <asm/amigahw.h>
21
22unsigned long amiga_chip_size;
Adrian Bunk8b169fa2008-02-04 22:30:25 -080023EXPORT_SYMBOL(amiga_chip_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25static struct resource chipram_res = {
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020026 .name = "Chip RAM", .start = CHIP_PHYSADDR
Linus Torvalds1da177e2005-04-16 15:20:36 -070027};
28static unsigned long chipavail;
29
30
31void __init amiga_chip_init(void)
32{
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020033 if (!AMIGAHW_PRESENT(CHIP_RAM))
34 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020036 chipram_res.end = amiga_chip_size-1;
37 request_resource(&iomem_resource, &chipram_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020039 chipavail = amiga_chip_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
42
43void *amiga_chip_alloc(unsigned long size, const char *name)
44{
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020045 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020047 /* round up */
48 size = PAGE_ALIGN(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#ifdef DEBUG
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020051 printk("amiga_chip_alloc: allocate %ld bytes\n", size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#endif
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020053 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
54 if (!res)
55 return NULL;
56 res->name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020058 if (allocate_resource(&chipram_res, res, size, 0, UINT_MAX, PAGE_SIZE,
59 NULL, NULL) < 0) {
60 kfree(res);
61 return NULL;
62 }
63 chipavail -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#ifdef DEBUG
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020065 printk("amiga_chip_alloc: returning %lx\n", res->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#endif
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020067 return (void *)ZTWO_VADDR(res->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
Adrian Bunk8b169fa2008-02-04 22:30:25 -080069EXPORT_SYMBOL(amiga_chip_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020072 /*
73 * Warning:
74 * amiga_chip_alloc_res is meant only for drivers that need to
75 * allocate Chip RAM before kmalloc() is functional. As a consequence,
76 * those drivers must not free that Chip RAM afterwards.
77 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79void * __init amiga_chip_alloc_res(unsigned long size, struct resource *res)
80{
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020081 unsigned long start;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020083 /* round up */
84 size = PAGE_ALIGN(size);
85 /* dmesg into chipmem prefers memory at the safe end */
86 start = CHIP_PHYSADDR + chipavail - size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88#ifdef DEBUG
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020089 printk("amiga_chip_alloc_res: allocate %ld bytes\n", size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#endif
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020091 if (allocate_resource(&chipram_res, res, size, start, UINT_MAX,
92 PAGE_SIZE, NULL, NULL) < 0) {
93 printk("amiga_chip_alloc_res: first alloc failed!\n");
94 if (allocate_resource(&chipram_res, res, size, 0, UINT_MAX,
95 PAGE_SIZE, NULL, NULL) < 0)
96 return NULL;
97 }
98 chipavail -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#ifdef DEBUG
Geert Uytterhoeven5be32462011-05-21 20:46:39 +0200100 printk("amiga_chip_alloc_res: returning %lx\n", res->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#endif
Geert Uytterhoeven5be32462011-05-21 20:46:39 +0200102 return (void *)ZTWO_VADDR(res->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
105void amiga_chip_free(void *ptr)
106{
Geert Uytterhoeven5be32462011-05-21 20:46:39 +0200107 unsigned long start = ZTWO_PADDR(ptr);
108 struct resource **p, *res;
109 unsigned long size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Geert Uytterhoeven5be32462011-05-21 20:46:39 +0200111 for (p = &chipram_res.child; (res = *p); p = &res->sibling) {
112 if (res->start != start)
113 continue;
114 *p = res->sibling;
115 size = res->end-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#ifdef DEBUG
Geert Uytterhoeven5be32462011-05-21 20:46:39 +0200117 printk("amiga_chip_free: free %ld bytes at %p\n", size, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#endif
Geert Uytterhoeven5be32462011-05-21 20:46:39 +0200119 chipavail += size;
120 kfree(res);
121 return;
122 }
123 printk("amiga_chip_free: trying to free nonexistent region at %p\n",
124 ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
Adrian Bunk8b169fa2008-02-04 22:30:25 -0800126EXPORT_SYMBOL(amiga_chip_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128
129unsigned long amiga_chip_avail(void)
130{
131#ifdef DEBUG
132 printk("amiga_chip_avail : %ld bytes\n", chipavail);
133#endif
134 return chipavail;
135}
Adrian Bunk8b169fa2008-02-04 22:30:25 -0800136EXPORT_SYMBOL(amiga_chip_avail);
137