pci-epc-mem.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * PCI Endpoint *Controller* Address Space Management
  3. *
  4. * Copyright (C) 2017 Texas Instruments
  5. * Author: Kishon Vijay Abraham I <kishon@ti.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 of
  9. * the License as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/io.h>
  20. #include <linux/module.h>
  21. #include <linux/slab.h>
  22. #include <linux/pci-epc.h>
  23. /**
  24. * pci_epc_mem_get_order() - determine the allocation order of a memory size
  25. * @mem: address space of the endpoint controller
  26. * @size: the size for which to get the order
  27. *
  28. * Reimplement get_order() for mem->page_size since the generic get_order
  29. * always gets order with a constant PAGE_SIZE.
  30. */
  31. static int pci_epc_mem_get_order(struct pci_epc_mem *mem, size_t size)
  32. {
  33. int order;
  34. unsigned int page_shift = ilog2(mem->page_size);
  35. size--;
  36. size >>= page_shift;
  37. #if BITS_PER_LONG == 32
  38. order = fls(size);
  39. #else
  40. order = fls64(size);
  41. #endif
  42. return order;
  43. }
  44. /**
  45. * __pci_epc_mem_init() - initialize the pci_epc_mem structure
  46. * @epc: the EPC device that invoked pci_epc_mem_init
  47. * @phys_base: the physical address of the base
  48. * @size: the size of the address space
  49. * @page_size: size of each page
  50. *
  51. * Invoke to initialize the pci_epc_mem structure used by the
  52. * endpoint functions to allocate mapped PCI address.
  53. */
  54. int __pci_epc_mem_init(struct pci_epc *epc, phys_addr_t phys_base, size_t size,
  55. size_t page_size)
  56. {
  57. int ret;
  58. struct pci_epc_mem *mem;
  59. unsigned long *bitmap;
  60. unsigned int page_shift;
  61. int pages;
  62. int bitmap_size;
  63. if (page_size < PAGE_SIZE)
  64. page_size = PAGE_SIZE;
  65. page_shift = ilog2(page_size);
  66. pages = size >> page_shift;
  67. bitmap_size = BITS_TO_LONGS(pages) * sizeof(long);
  68. mem = kzalloc(sizeof(*mem), GFP_KERNEL);
  69. if (!mem) {
  70. ret = -ENOMEM;
  71. goto err;
  72. }
  73. bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  74. if (!bitmap) {
  75. ret = -ENOMEM;
  76. goto err_mem;
  77. }
  78. mem->bitmap = bitmap;
  79. mem->phys_base = phys_base;
  80. mem->page_size = page_size;
  81. mem->pages = pages;
  82. mem->size = size;
  83. mutex_init(&mem->lock);
  84. epc->mem = mem;
  85. return 0;
  86. err_mem:
  87. kfree(mem);
  88. err:
  89. return ret;
  90. }
  91. EXPORT_SYMBOL_GPL(__pci_epc_mem_init);
  92. /**
  93. * pci_epc_mem_exit() - cleanup the pci_epc_mem structure
  94. * @epc: the EPC device that invoked pci_epc_mem_exit
  95. *
  96. * Invoke to cleanup the pci_epc_mem structure allocated in
  97. * pci_epc_mem_init().
  98. */
  99. void pci_epc_mem_exit(struct pci_epc *epc)
  100. {
  101. struct pci_epc_mem *mem = epc->mem;
  102. epc->mem = NULL;
  103. kfree(mem->bitmap);
  104. kfree(mem);
  105. }
  106. EXPORT_SYMBOL_GPL(pci_epc_mem_exit);
  107. /**
  108. * pci_epc_mem_alloc_addr() - allocate memory address from EPC addr space
  109. * @epc: the EPC device on which memory has to be allocated
  110. * @phys_addr: populate the allocated physical address here
  111. * @size: the size of the address space that has to be allocated
  112. *
  113. * Invoke to allocate memory address from the EPC address space. This
  114. * is usually done to map the remote RC address into the local system.
  115. */
  116. void __iomem *pci_epc_mem_alloc_addr(struct pci_epc *epc,
  117. phys_addr_t *phys_addr, size_t size)
  118. {
  119. int pageno;
  120. void __iomem *virt_addr = NULL;
  121. struct pci_epc_mem *mem = epc->mem;
  122. unsigned int page_shift = ilog2(mem->page_size);
  123. int order;
  124. size = ALIGN(size, mem->page_size);
  125. order = pci_epc_mem_get_order(mem, size);
  126. mutex_lock(&mem->lock);
  127. pageno = bitmap_find_free_region(mem->bitmap, mem->pages, order);
  128. if (pageno < 0)
  129. goto ret;
  130. *phys_addr = mem->phys_base + (pageno << page_shift);
  131. virt_addr = ioremap(*phys_addr, size);
  132. if (!virt_addr)
  133. bitmap_release_region(mem->bitmap, pageno, order);
  134. ret:
  135. mutex_unlock(&mem->lock);
  136. return virt_addr;
  137. }
  138. EXPORT_SYMBOL_GPL(pci_epc_mem_alloc_addr);
  139. /**
  140. * pci_epc_mem_free_addr() - free the allocated memory address
  141. * @epc: the EPC device on which memory was allocated
  142. * @phys_addr: the allocated physical address
  143. * @virt_addr: virtual address of the allocated mem space
  144. * @size: the size of the allocated address space
  145. *
  146. * Invoke to free the memory allocated using pci_epc_mem_alloc_addr.
  147. */
  148. void pci_epc_mem_free_addr(struct pci_epc *epc, phys_addr_t phys_addr,
  149. void __iomem *virt_addr, size_t size)
  150. {
  151. int pageno;
  152. struct pci_epc_mem *mem = epc->mem;
  153. unsigned int page_shift = ilog2(mem->page_size);
  154. int order;
  155. iounmap(virt_addr);
  156. pageno = (phys_addr - mem->phys_base) >> page_shift;
  157. size = ALIGN(size, mem->page_size);
  158. order = pci_epc_mem_get_order(mem, size);
  159. mutex_lock(&mem->lock);
  160. bitmap_release_region(mem->bitmap, pageno, order);
  161. mutex_unlock(&mem->lock);
  162. }
  163. EXPORT_SYMBOL_GPL(pci_epc_mem_free_addr);
  164. MODULE_DESCRIPTION("PCI EPC Address Space Management");
  165. MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
  166. MODULE_LICENSE("GPL v2");