chipram.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/atomic.h>
  18. #include <asm/page.h>
  19. #include <asm/amigahw.h>
  20. unsigned long amiga_chip_size;
  21. EXPORT_SYMBOL(amiga_chip_size);
  22. static struct resource chipram_res = {
  23. .name = "Chip RAM", .start = CHIP_PHYSADDR
  24. };
  25. static atomic_t chipavail;
  26. void __init amiga_chip_init(void)
  27. {
  28. if (!AMIGAHW_PRESENT(CHIP_RAM))
  29. return;
  30. chipram_res.end = CHIP_PHYSADDR + amiga_chip_size - 1;
  31. request_resource(&iomem_resource, &chipram_res);
  32. atomic_set(&chipavail, amiga_chip_size);
  33. }
  34. void *amiga_chip_alloc(unsigned long size, const char *name)
  35. {
  36. struct resource *res;
  37. void *p;
  38. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  39. if (!res)
  40. return NULL;
  41. res->name = name;
  42. p = amiga_chip_alloc_res(size, res);
  43. if (!p) {
  44. kfree(res);
  45. return NULL;
  46. }
  47. return p;
  48. }
  49. EXPORT_SYMBOL(amiga_chip_alloc);
  50. /*
  51. * Warning:
  52. * amiga_chip_alloc_res is meant only for drivers that need to
  53. * allocate Chip RAM before kmalloc() is functional. As a consequence,
  54. * those drivers must not free that Chip RAM afterwards.
  55. */
  56. void *amiga_chip_alloc_res(unsigned long size, struct resource *res)
  57. {
  58. int error;
  59. /* round up */
  60. size = PAGE_ALIGN(size);
  61. pr_debug("amiga_chip_alloc_res: allocate %lu bytes\n", size);
  62. error = allocate_resource(&chipram_res, res, size, 0, UINT_MAX,
  63. PAGE_SIZE, NULL, NULL);
  64. if (error < 0) {
  65. pr_err("amiga_chip_alloc_res: allocate_resource() failed %d!\n",
  66. error);
  67. return NULL;
  68. }
  69. atomic_sub(size, &chipavail);
  70. pr_debug("amiga_chip_alloc_res: returning %pR\n", res);
  71. return ZTWO_VADDR(res->start);
  72. }
  73. void amiga_chip_free(void *ptr)
  74. {
  75. unsigned long start = ZTWO_PADDR(ptr);
  76. struct resource *res;
  77. unsigned long size;
  78. res = lookup_resource(&chipram_res, start);
  79. if (!res) {
  80. pr_err("amiga_chip_free: trying to free nonexistent region at "
  81. "%p\n", ptr);
  82. return;
  83. }
  84. size = resource_size(res);
  85. pr_debug("amiga_chip_free: free %lu bytes at %p\n", size, ptr);
  86. atomic_add(size, &chipavail);
  87. release_resource(res);
  88. kfree(res);
  89. }
  90. EXPORT_SYMBOL(amiga_chip_free);
  91. unsigned long amiga_chip_avail(void)
  92. {
  93. unsigned long n = atomic_read(&chipavail);
  94. pr_debug("amiga_chip_avail : %lu bytes\n", n);
  95. return n;
  96. }
  97. EXPORT_SYMBOL(amiga_chip_avail);