fsl_85xx_cache_sram.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright 2009-2010 Freescale Semiconductor, Inc.
  3. *
  4. * Simple memory allocator abstraction for QorIQ (P1/P2) based Cache-SRAM
  5. *
  6. * Author: Vivek Mahajan <vivek.mahajan@freescale.com>
  7. *
  8. * This file is derived from the original work done
  9. * by Sylvain Munaut for the Bestcomm SRAM allocator.
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/export.h>
  27. #include <linux/slab.h>
  28. #include <linux/err.h>
  29. #include <linux/of_platform.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/fsl_85xx_cache_sram.h>
  32. #include "fsl_85xx_cache_ctlr.h"
  33. struct mpc85xx_cache_sram *cache_sram;
  34. void *mpc85xx_cache_sram_alloc(unsigned int size,
  35. phys_addr_t *phys, unsigned int align)
  36. {
  37. unsigned long offset;
  38. unsigned long flags;
  39. if (unlikely(cache_sram == NULL))
  40. return NULL;
  41. if (!size || (size > cache_sram->size) || (align > cache_sram->size)) {
  42. pr_err("%s(): size(=%x) or align(=%x) zero or too big\n",
  43. __func__, size, align);
  44. return NULL;
  45. }
  46. if ((align & (align - 1)) || align <= 1) {
  47. pr_err("%s(): align(=%x) must be power of two and >1\n",
  48. __func__, align);
  49. return NULL;
  50. }
  51. spin_lock_irqsave(&cache_sram->lock, flags);
  52. offset = rh_alloc_align(cache_sram->rh, size, align, NULL);
  53. spin_unlock_irqrestore(&cache_sram->lock, flags);
  54. if (IS_ERR_VALUE(offset))
  55. return NULL;
  56. *phys = cache_sram->base_phys + offset;
  57. return (unsigned char *)cache_sram->base_virt + offset;
  58. }
  59. EXPORT_SYMBOL(mpc85xx_cache_sram_alloc);
  60. void mpc85xx_cache_sram_free(void *ptr)
  61. {
  62. unsigned long flags;
  63. BUG_ON(!ptr);
  64. spin_lock_irqsave(&cache_sram->lock, flags);
  65. rh_free(cache_sram->rh, ptr - cache_sram->base_virt);
  66. spin_unlock_irqrestore(&cache_sram->lock, flags);
  67. }
  68. EXPORT_SYMBOL(mpc85xx_cache_sram_free);
  69. int __init instantiate_cache_sram(struct platform_device *dev,
  70. struct sram_parameters sram_params)
  71. {
  72. int ret = 0;
  73. if (cache_sram) {
  74. dev_err(&dev->dev, "Already initialized cache-sram\n");
  75. return -EBUSY;
  76. }
  77. cache_sram = kzalloc(sizeof(struct mpc85xx_cache_sram), GFP_KERNEL);
  78. if (!cache_sram) {
  79. dev_err(&dev->dev, "Out of memory for cache_sram structure\n");
  80. return -ENOMEM;
  81. }
  82. cache_sram->base_phys = sram_params.sram_offset;
  83. cache_sram->size = sram_params.sram_size;
  84. if (!request_mem_region(cache_sram->base_phys, cache_sram->size,
  85. "fsl_85xx_cache_sram")) {
  86. dev_err(&dev->dev, "%s: request memory failed\n",
  87. dev->dev.of_node->full_name);
  88. ret = -ENXIO;
  89. goto out_free;
  90. }
  91. cache_sram->base_virt = ioremap_prot(cache_sram->base_phys,
  92. cache_sram->size, _PAGE_COHERENT | PAGE_KERNEL);
  93. if (!cache_sram->base_virt) {
  94. dev_err(&dev->dev, "%s: ioremap_prot failed\n",
  95. dev->dev.of_node->full_name);
  96. ret = -ENOMEM;
  97. goto out_release;
  98. }
  99. cache_sram->rh = rh_create(sizeof(unsigned int));
  100. if (IS_ERR(cache_sram->rh)) {
  101. dev_err(&dev->dev, "%s: Unable to create remote heap\n",
  102. dev->dev.of_node->full_name);
  103. ret = PTR_ERR(cache_sram->rh);
  104. goto out_unmap;
  105. }
  106. rh_attach_region(cache_sram->rh, 0, cache_sram->size);
  107. spin_lock_init(&cache_sram->lock);
  108. dev_info(&dev->dev, "[base:0x%llx, size:0x%x] configured and loaded\n",
  109. (unsigned long long)cache_sram->base_phys, cache_sram->size);
  110. return 0;
  111. out_unmap:
  112. iounmap(cache_sram->base_virt);
  113. out_release:
  114. release_mem_region(cache_sram->base_phys, cache_sram->size);
  115. out_free:
  116. kfree(cache_sram);
  117. return ret;
  118. }
  119. void remove_cache_sram(struct platform_device *dev)
  120. {
  121. BUG_ON(!cache_sram);
  122. rh_detach_region(cache_sram->rh, 0, cache_sram->size);
  123. rh_destroy(cache_sram->rh);
  124. iounmap(cache_sram->base_virt);
  125. release_mem_region(cache_sram->base_phys, cache_sram->size);
  126. kfree(cache_sram);
  127. cache_sram = NULL;
  128. dev_info(&dev->dev, "MPC85xx Cache-SRAM driver unloaded\n");
  129. }