chipram.c 3.2 KB

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