chipram.c 3.1 KB

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