blob: 9005fa07f2e213ec9897504fa6731c41b2c751c3 [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
Geert Uytterhoevenb4f6f452011-04-24 22:55:20 +020050 pr_debug("amiga_chip_alloc: allocate %lu bytes\n", size);
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020051 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
52 if (!res)
53 return NULL;
54 res->name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020056 if (allocate_resource(&chipram_res, res, size, 0, UINT_MAX, PAGE_SIZE,
57 NULL, NULL) < 0) {
58 kfree(res);
59 return NULL;
60 }
61 chipavail -= size;
Geert Uytterhoevenb4f6f452011-04-24 22:55:20 +020062 pr_debug("amiga_chip_alloc: returning %pR\n", res);
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020063 return (void *)ZTWO_VADDR(res->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
Adrian Bunk8b169fa2008-02-04 22:30:25 -080065EXPORT_SYMBOL(amiga_chip_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020068 /*
69 * Warning:
70 * amiga_chip_alloc_res is meant only for drivers that need to
71 * allocate Chip RAM before kmalloc() is functional. As a consequence,
72 * those drivers must not free that Chip RAM afterwards.
73 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75void * __init amiga_chip_alloc_res(unsigned long size, struct resource *res)
76{
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020077 unsigned long start;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020079 /* round up */
80 size = PAGE_ALIGN(size);
81 /* dmesg into chipmem prefers memory at the safe end */
82 start = CHIP_PHYSADDR + chipavail - size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Geert Uytterhoevenb4f6f452011-04-24 22:55:20 +020084 pr_debug("amiga_chip_alloc_res: allocate %lu bytes\n", size);
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020085 if (allocate_resource(&chipram_res, res, size, start, UINT_MAX,
86 PAGE_SIZE, NULL, NULL) < 0) {
Geert Uytterhoevenb4f6f452011-04-24 22:55:20 +020087 pr_err("amiga_chip_alloc_res: first alloc failed!\n");
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020088 if (allocate_resource(&chipram_res, res, size, 0, UINT_MAX,
89 PAGE_SIZE, NULL, NULL) < 0)
90 return NULL;
91 }
92 chipavail -= size;
Geert Uytterhoevenb4f6f452011-04-24 22:55:20 +020093 pr_debug("amiga_chip_alloc_res: returning %pR\n", res);
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020094 return (void *)ZTWO_VADDR(res->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
97void amiga_chip_free(void *ptr)
98{
Geert Uytterhoeven5be32462011-05-21 20:46:39 +020099 unsigned long start = ZTWO_PADDR(ptr);
100 struct resource **p, *res;
101 unsigned long size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Geert Uytterhoeven5be32462011-05-21 20:46:39 +0200103 for (p = &chipram_res.child; (res = *p); p = &res->sibling) {
104 if (res->start != start)
105 continue;
106 *p = res->sibling;
107 size = res->end-start;
Geert Uytterhoevenb4f6f452011-04-24 22:55:20 +0200108 pr_debug("amiga_chip_free: free %lu bytes at %p\n", size, ptr);
Geert Uytterhoeven5be32462011-05-21 20:46:39 +0200109 chipavail += size;
110 kfree(res);
111 return;
112 }
Geert Uytterhoevenb4f6f452011-04-24 22:55:20 +0200113 pr_err("amiga_chip_free: trying to free nonexistent region at %p\n",
Geert Uytterhoeven5be32462011-05-21 20:46:39 +0200114 ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
Adrian Bunk8b169fa2008-02-04 22:30:25 -0800116EXPORT_SYMBOL(amiga_chip_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118
119unsigned long amiga_chip_avail(void)
120{
Geert Uytterhoevenb4f6f452011-04-24 22:55:20 +0200121 pr_debug("amiga_chip_avail : %lu bytes\n", chipavail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return chipavail;
123}
Adrian Bunk8b169fa2008-02-04 22:30:25 -0800124EXPORT_SYMBOL(amiga_chip_avail);
125