pci-epc-mem.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCI Endpoint *Controller* Address Space Management
  4. *
  5. * Copyright (C) 2017 Texas Instruments
  6. * Author: Kishon Vijay Abraham I <[email protected]>
  7. */
  8. #include <linux/io.h>
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/pci-epc.h>
  12. /**
  13. * pci_epc_mem_get_order() - determine the allocation order of a memory size
  14. * @mem: address space of the endpoint controller
  15. * @size: the size for which to get the order
  16. *
  17. * Reimplement get_order() for mem->page_size since the generic get_order
  18. * always gets order with a constant PAGE_SIZE.
  19. */
  20. static int pci_epc_mem_get_order(struct pci_epc_mem *mem, size_t size)
  21. {
  22. int order;
  23. unsigned int page_shift = ilog2(mem->window.page_size);
  24. size--;
  25. size >>= page_shift;
  26. #if BITS_PER_LONG == 32
  27. order = fls(size);
  28. #else
  29. order = fls64(size);
  30. #endif
  31. return order;
  32. }
  33. /**
  34. * pci_epc_multi_mem_init() - initialize the pci_epc_mem structure
  35. * @epc: the EPC device that invoked pci_epc_mem_init
  36. * @windows: pointer to windows supported by the device
  37. * @num_windows: number of windows device supports
  38. *
  39. * Invoke to initialize the pci_epc_mem structure used by the
  40. * endpoint functions to allocate mapped PCI address.
  41. */
  42. int pci_epc_multi_mem_init(struct pci_epc *epc,
  43. struct pci_epc_mem_window *windows,
  44. unsigned int num_windows)
  45. {
  46. struct pci_epc_mem *mem = NULL;
  47. unsigned long *bitmap = NULL;
  48. unsigned int page_shift;
  49. size_t page_size;
  50. int bitmap_size;
  51. int pages;
  52. int ret;
  53. int i;
  54. epc->num_windows = 0;
  55. if (!windows || !num_windows)
  56. return -EINVAL;
  57. epc->windows = kcalloc(num_windows, sizeof(*epc->windows), GFP_KERNEL);
  58. if (!epc->windows)
  59. return -ENOMEM;
  60. for (i = 0; i < num_windows; i++) {
  61. page_size = windows[i].page_size;
  62. if (page_size < PAGE_SIZE)
  63. page_size = PAGE_SIZE;
  64. page_shift = ilog2(page_size);
  65. pages = windows[i].size >> page_shift;
  66. bitmap_size = BITS_TO_LONGS(pages) * sizeof(long);
  67. mem = kzalloc(sizeof(*mem), GFP_KERNEL);
  68. if (!mem) {
  69. ret = -ENOMEM;
  70. i--;
  71. goto err_mem;
  72. }
  73. bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  74. if (!bitmap) {
  75. ret = -ENOMEM;
  76. kfree(mem);
  77. i--;
  78. goto err_mem;
  79. }
  80. mem->window.phys_base = windows[i].phys_base;
  81. mem->window.size = windows[i].size;
  82. mem->window.page_size = page_size;
  83. mem->bitmap = bitmap;
  84. mem->pages = pages;
  85. mutex_init(&mem->lock);
  86. epc->windows[i] = mem;
  87. }
  88. epc->mem = epc->windows[0];
  89. epc->num_windows = num_windows;
  90. return 0;
  91. err_mem:
  92. for (; i >= 0; i--) {
  93. mem = epc->windows[i];
  94. kfree(mem->bitmap);
  95. kfree(mem);
  96. }
  97. kfree(epc->windows);
  98. return ret;
  99. }
  100. EXPORT_SYMBOL_GPL(pci_epc_multi_mem_init);
  101. int pci_epc_mem_init(struct pci_epc *epc, phys_addr_t base,
  102. size_t size, size_t page_size)
  103. {
  104. struct pci_epc_mem_window mem_window;
  105. mem_window.phys_base = base;
  106. mem_window.size = size;
  107. mem_window.page_size = page_size;
  108. return pci_epc_multi_mem_init(epc, &mem_window, 1);
  109. }
  110. EXPORT_SYMBOL_GPL(pci_epc_mem_init);
  111. /**
  112. * pci_epc_mem_exit() - cleanup the pci_epc_mem structure
  113. * @epc: the EPC device that invoked pci_epc_mem_exit
  114. *
  115. * Invoke to cleanup the pci_epc_mem structure allocated in
  116. * pci_epc_mem_init().
  117. */
  118. void pci_epc_mem_exit(struct pci_epc *epc)
  119. {
  120. struct pci_epc_mem *mem;
  121. int i;
  122. if (!epc->num_windows)
  123. return;
  124. for (i = 0; i < epc->num_windows; i++) {
  125. mem = epc->windows[i];
  126. kfree(mem->bitmap);
  127. kfree(mem);
  128. }
  129. kfree(epc->windows);
  130. epc->windows = NULL;
  131. epc->mem = NULL;
  132. epc->num_windows = 0;
  133. }
  134. EXPORT_SYMBOL_GPL(pci_epc_mem_exit);
  135. /**
  136. * pci_epc_mem_alloc_addr() - allocate memory address from EPC addr space
  137. * @epc: the EPC device on which memory has to be allocated
  138. * @phys_addr: populate the allocated physical address here
  139. * @size: the size of the address space that has to be allocated
  140. *
  141. * Invoke to allocate memory address from the EPC address space. This
  142. * is usually done to map the remote RC address into the local system.
  143. */
  144. void __iomem *pci_epc_mem_alloc_addr(struct pci_epc *epc,
  145. phys_addr_t *phys_addr, size_t size)
  146. {
  147. void __iomem *virt_addr = NULL;
  148. struct pci_epc_mem *mem;
  149. unsigned int page_shift;
  150. size_t align_size;
  151. int pageno;
  152. int order;
  153. int i;
  154. for (i = 0; i < epc->num_windows; i++) {
  155. mem = epc->windows[i];
  156. mutex_lock(&mem->lock);
  157. align_size = ALIGN(size, mem->window.page_size);
  158. order = pci_epc_mem_get_order(mem, align_size);
  159. pageno = bitmap_find_free_region(mem->bitmap, mem->pages,
  160. order);
  161. if (pageno >= 0) {
  162. page_shift = ilog2(mem->window.page_size);
  163. *phys_addr = mem->window.phys_base +
  164. ((phys_addr_t)pageno << page_shift);
  165. virt_addr = ioremap(*phys_addr, align_size);
  166. if (!virt_addr) {
  167. bitmap_release_region(mem->bitmap,
  168. pageno, order);
  169. mutex_unlock(&mem->lock);
  170. continue;
  171. }
  172. mutex_unlock(&mem->lock);
  173. return virt_addr;
  174. }
  175. mutex_unlock(&mem->lock);
  176. }
  177. return virt_addr;
  178. }
  179. EXPORT_SYMBOL_GPL(pci_epc_mem_alloc_addr);
  180. static struct pci_epc_mem *pci_epc_get_matching_window(struct pci_epc *epc,
  181. phys_addr_t phys_addr)
  182. {
  183. struct pci_epc_mem *mem;
  184. int i;
  185. for (i = 0; i < epc->num_windows; i++) {
  186. mem = epc->windows[i];
  187. if (phys_addr >= mem->window.phys_base &&
  188. phys_addr < (mem->window.phys_base + mem->window.size))
  189. return mem;
  190. }
  191. return NULL;
  192. }
  193. /**
  194. * pci_epc_mem_free_addr() - free the allocated memory address
  195. * @epc: the EPC device on which memory was allocated
  196. * @phys_addr: the allocated physical address
  197. * @virt_addr: virtual address of the allocated mem space
  198. * @size: the size of the allocated address space
  199. *
  200. * Invoke to free the memory allocated using pci_epc_mem_alloc_addr.
  201. */
  202. void pci_epc_mem_free_addr(struct pci_epc *epc, phys_addr_t phys_addr,
  203. void __iomem *virt_addr, size_t size)
  204. {
  205. struct pci_epc_mem *mem;
  206. unsigned int page_shift;
  207. size_t page_size;
  208. int pageno;
  209. int order;
  210. mem = pci_epc_get_matching_window(epc, phys_addr);
  211. if (!mem) {
  212. pr_err("failed to get matching window\n");
  213. return;
  214. }
  215. page_size = mem->window.page_size;
  216. page_shift = ilog2(page_size);
  217. iounmap(virt_addr);
  218. pageno = (phys_addr - mem->window.phys_base) >> page_shift;
  219. size = ALIGN(size, page_size);
  220. order = pci_epc_mem_get_order(mem, size);
  221. mutex_lock(&mem->lock);
  222. bitmap_release_region(mem->bitmap, pageno, order);
  223. mutex_unlock(&mem->lock);
  224. }
  225. EXPORT_SYMBOL_GPL(pci_epc_mem_free_addr);
  226. MODULE_DESCRIPTION("PCI EPC Address Space Management");
  227. MODULE_AUTHOR("Kishon Vijay Abraham I <[email protected]>");
  228. MODULE_LICENSE("GPL v2");