chipram.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  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. #include <linux/types.h>
  10. #include <linux/kernel.h>
  11. #include <linux/mm.h>
  12. #include <linux/init.h>
  13. #include <linux/ioport.h>
  14. #include <linux/slab.h>
  15. #include <linux/string.h>
  16. #include <linux/module.h>
  17. #include <asm/page.h>
  18. #include <asm/amigahw.h>
  19. unsigned long amiga_chip_size;
  20. EXPORT_SYMBOL(amiga_chip_size);
  21. static struct resource chipram_res = {
  22. .name = "Chip RAM", .start = CHIP_PHYSADDR
  23. };
  24. static unsigned long chipavail;
  25. void __init amiga_chip_init(void)
  26. {
  27. if (!AMIGAHW_PRESENT(CHIP_RAM))
  28. return;
  29. /*
  30. * Remove the first 4 pages where PPC exception handlers will be located
  31. */
  32. amiga_chip_size -= 0x4000;
  33. chipram_res.end = amiga_chip_size-1;
  34. request_resource(&iomem_resource, &chipram_res);
  35. chipavail = amiga_chip_size;
  36. }
  37. void *amiga_chip_alloc(unsigned long size, const char *name)
  38. {
  39. struct resource *res;
  40. /* round up */
  41. size = PAGE_ALIGN(size);
  42. #ifdef DEBUG
  43. printk("amiga_chip_alloc: allocate %ld bytes\n", size);
  44. #endif
  45. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  46. if (!res)
  47. return NULL;
  48. res->name = name;
  49. if (allocate_resource(&chipram_res, res, size, 0, UINT_MAX, PAGE_SIZE, NULL, NULL) < 0) {
  50. kfree(res);
  51. return NULL;
  52. }
  53. chipavail -= size;
  54. #ifdef DEBUG
  55. printk("amiga_chip_alloc: returning %lx\n", res->start);
  56. #endif
  57. return (void *)ZTWO_VADDR(res->start);
  58. }
  59. EXPORT_SYMBOL(amiga_chip_alloc);
  60. /*
  61. * Warning:
  62. * amiga_chip_alloc_res is meant only for drivers that need to allocate
  63. * Chip RAM before kmalloc() is functional. As a consequence, those
  64. * drivers must not free that Chip RAM afterwards.
  65. */
  66. void * __init amiga_chip_alloc_res(unsigned long size, struct resource *res)
  67. {
  68. unsigned long start;
  69. /* round up */
  70. size = PAGE_ALIGN(size);
  71. /* dmesg into chipmem prefers memory at the safe end */
  72. start = CHIP_PHYSADDR + chipavail - size;
  73. #ifdef DEBUG
  74. printk("amiga_chip_alloc_res: allocate %ld bytes\n", size);
  75. #endif
  76. if (allocate_resource(&chipram_res, res, size, start, UINT_MAX, PAGE_SIZE, NULL, NULL) < 0) {
  77. printk("amiga_chip_alloc_res: first alloc failed!\n");
  78. if (allocate_resource(&chipram_res, res, size, 0, UINT_MAX, PAGE_SIZE, NULL, NULL) < 0)
  79. return NULL;
  80. }
  81. chipavail -= size;
  82. #ifdef DEBUG
  83. printk("amiga_chip_alloc_res: returning %lx\n", res->start);
  84. #endif
  85. return (void *)ZTWO_VADDR(res->start);
  86. }
  87. void amiga_chip_free(void *ptr)
  88. {
  89. unsigned long start = ZTWO_PADDR(ptr);
  90. struct resource **p, *res;
  91. unsigned long size;
  92. for (p = &chipram_res.child; (res = *p); p = &res->sibling) {
  93. if (res->start != start)
  94. continue;
  95. *p = res->sibling;
  96. size = res->end-start;
  97. #ifdef DEBUG
  98. printk("amiga_chip_free: free %ld bytes at %p\n", size, ptr);
  99. #endif
  100. chipavail += size;
  101. kfree(res);
  102. return;
  103. }
  104. printk("amiga_chip_free: trying to free nonexistent region at %p\n", ptr);
  105. }
  106. EXPORT_SYMBOL(amiga_chip_free);
  107. unsigned long amiga_chip_avail(void)
  108. {
  109. #ifdef DEBUG
  110. printk("amiga_chip_avail : %ld bytes\n", chipavail);
  111. #endif
  112. return chipavail;
  113. }
  114. EXPORT_SYMBOL(amiga_chip_avail);