pci-epc-mem.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. epc->mem = mem;
  84. return 0;
  85. err_mem:
  86. kfree(mem);
  87. err:
  88. return ret;
  89. }
  90. EXPORT_SYMBOL_GPL(__pci_epc_mem_init);
  91. /**
  92. * pci_epc_mem_exit() - cleanup the pci_epc_mem structure
  93. * @epc: the EPC device that invoked pci_epc_mem_exit
  94. *
  95. * Invoke to cleanup the pci_epc_mem structure allocated in
  96. * pci_epc_mem_init().
  97. */
  98. void pci_epc_mem_exit(struct pci_epc *epc)
  99. {
  100. struct pci_epc_mem *mem = epc->mem;
  101. epc->mem = NULL;
  102. kfree(mem->bitmap);
  103. kfree(mem);
  104. }
  105. EXPORT_SYMBOL_GPL(pci_epc_mem_exit);
  106. /**
  107. * pci_epc_mem_alloc_addr() - allocate memory address from EPC addr space
  108. * @epc: the EPC device on which memory has to be allocated
  109. * @phys_addr: populate the allocated physical address here
  110. * @size: the size of the address space that has to be allocated
  111. *
  112. * Invoke to allocate memory address from the EPC address space. This
  113. * is usually done to map the remote RC address into the local system.
  114. */
  115. void __iomem *pci_epc_mem_alloc_addr(struct pci_epc *epc,
  116. phys_addr_t *phys_addr, size_t size)
  117. {
  118. int pageno;
  119. void __iomem *virt_addr;
  120. struct pci_epc_mem *mem = epc->mem;
  121. unsigned int page_shift = ilog2(mem->page_size);
  122. int order;
  123. size = ALIGN(size, mem->page_size);
  124. order = pci_epc_mem_get_order(mem, size);
  125. pageno = bitmap_find_free_region(mem->bitmap, mem->pages, order);
  126. if (pageno < 0)
  127. return NULL;
  128. *phys_addr = mem->phys_base + (pageno << page_shift);
  129. virt_addr = ioremap(*phys_addr, size);
  130. if (!virt_addr)
  131. bitmap_release_region(mem->bitmap, pageno, order);
  132. return virt_addr;
  133. }
  134. EXPORT_SYMBOL_GPL(pci_epc_mem_alloc_addr);
  135. /**
  136. * pci_epc_mem_free_addr() - free the allocated memory address
  137. * @epc: the EPC device on which memory was allocated
  138. * @phys_addr: the allocated physical address
  139. * @virt_addr: virtual address of the allocated mem space
  140. * @size: the size of the allocated address space
  141. *
  142. * Invoke to free the memory allocated using pci_epc_mem_alloc_addr.
  143. */
  144. void pci_epc_mem_free_addr(struct pci_epc *epc, phys_addr_t phys_addr,
  145. void __iomem *virt_addr, size_t size)
  146. {
  147. int pageno;
  148. struct pci_epc_mem *mem = epc->mem;
  149. unsigned int page_shift = ilog2(mem->page_size);
  150. int order;
  151. iounmap(virt_addr);
  152. pageno = (phys_addr - mem->phys_base) >> page_shift;
  153. size = ALIGN(size, mem->page_size);
  154. order = pci_epc_mem_get_order(mem, size);
  155. bitmap_release_region(mem->bitmap, pageno, order);
  156. }
  157. EXPORT_SYMBOL_GPL(pci_epc_mem_free_addr);
  158. MODULE_DESCRIPTION("PCI EPC Address Space Management");
  159. MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
  160. MODULE_LICENSE("GPL v2");