chipram.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. ** linux/amiga/chipram.c
  4. **
  5. ** Modified 03-May-94 by Geert Uytterhoeven <geert@linux-m68k.org>
  6. ** - 64-bit aligned allocations for full AGA compatibility
  7. **
  8. ** Rewritten 15/9/2000 by Geert to use resource management
  9. */
  10. #include <linux/types.h>
  11. #include <linux/kernel.h>
  12. #include <linux/mm.h>
  13. #include <linux/init.h>
  14. #include <linux/ioport.h>
  15. #include <linux/slab.h>
  16. #include <linux/string.h>
  17. #include <linux/module.h>
  18. #include <asm/atomic.h>
  19. #include <asm/page.h>
  20. #include <asm/amigahw.h>
  21. unsigned long amiga_chip_size;
  22. EXPORT_SYMBOL(amiga_chip_size);
  23. static struct resource chipram_res = {
  24. .name = "Chip RAM", .start = CHIP_PHYSADDR
  25. };
  26. static atomic_t chipavail;
  27. void __init amiga_chip_init(void)
  28. {
  29. if (!AMIGAHW_PRESENT(CHIP_RAM))
  30. return;
  31. chipram_res.end = CHIP_PHYSADDR + amiga_chip_size - 1;
  32. request_resource(&iomem_resource, &chipram_res);
  33. atomic_set(&chipavail, amiga_chip_size);
  34. }
  35. void *amiga_chip_alloc(unsigned long size, const char *name)
  36. {
  37. struct resource *res;
  38. void *p;
  39. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  40. if (!res)
  41. return NULL;
  42. res->name = name;
  43. p = amiga_chip_alloc_res(size, res);
  44. if (!p) {
  45. kfree(res);
  46. return NULL;
  47. }
  48. return p;
  49. }
  50. EXPORT_SYMBOL(amiga_chip_alloc);
  51. /*
  52. * Warning:
  53. * amiga_chip_alloc_res is meant only for drivers that need to
  54. * allocate Chip RAM before kmalloc() is functional. As a consequence,
  55. * those drivers must not free that Chip RAM afterwards.
  56. */
  57. void *amiga_chip_alloc_res(unsigned long size, struct resource *res)
  58. {
  59. int error;
  60. /* round up */
  61. size = PAGE_ALIGN(size);
  62. pr_debug("amiga_chip_alloc_res: allocate %lu bytes\n", size);
  63. error = allocate_resource(&chipram_res, res, size, 0, UINT_MAX,
  64. PAGE_SIZE, NULL, NULL);
  65. if (error < 0) {
  66. pr_err("amiga_chip_alloc_res: allocate_resource() failed %d!\n",
  67. error);
  68. return NULL;
  69. }
  70. atomic_sub(size, &chipavail);
  71. pr_debug("amiga_chip_alloc_res: returning %pR\n", res);
  72. return ZTWO_VADDR(res->start);
  73. }
  74. void amiga_chip_free(void *ptr)
  75. {
  76. unsigned long start = ZTWO_PADDR(ptr);
  77. struct resource *res;
  78. unsigned long size;
  79. res = lookup_resource(&chipram_res, start);
  80. if (!res) {
  81. pr_err("amiga_chip_free: trying to free nonexistent region at "
  82. "%p\n", ptr);
  83. return;
  84. }
  85. size = resource_size(res);
  86. pr_debug("amiga_chip_free: free %lu bytes at %p\n", size, ptr);
  87. atomic_add(size, &chipavail);
  88. release_resource(res);
  89. kfree(res);
  90. }
  91. EXPORT_SYMBOL(amiga_chip_free);
  92. unsigned long amiga_chip_avail(void)
  93. {
  94. unsigned long n = atomic_read(&chipavail);
  95. pr_debug("amiga_chip_avail : %lu bytes\n", n);
  96. return n;
  97. }
  98. EXPORT_SYMBOL(amiga_chip_avail);