pci-ioda-tce.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * TCE helpers for IODA PCI/PCIe on PowerNV platforms
  4. *
  5. * Copyright 2018 IBM Corp.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/iommu.h>
  14. #include <asm/iommu.h>
  15. #include <asm/tce.h>
  16. #include "pci.h"
  17. void pnv_pci_setup_iommu_table(struct iommu_table *tbl,
  18. void *tce_mem, u64 tce_size,
  19. u64 dma_offset, unsigned int page_shift)
  20. {
  21. tbl->it_blocksize = 16;
  22. tbl->it_base = (unsigned long)tce_mem;
  23. tbl->it_page_shift = page_shift;
  24. tbl->it_offset = dma_offset >> tbl->it_page_shift;
  25. tbl->it_index = 0;
  26. tbl->it_size = tce_size >> 3;
  27. tbl->it_busno = 0;
  28. tbl->it_type = TCE_PCI;
  29. }
  30. static __be64 *pnv_alloc_tce_level(int nid, unsigned int shift)
  31. {
  32. struct page *tce_mem = NULL;
  33. __be64 *addr;
  34. tce_mem = alloc_pages_node(nid, GFP_ATOMIC | __GFP_NOWARN,
  35. shift - PAGE_SHIFT);
  36. if (!tce_mem) {
  37. pr_err("Failed to allocate a TCE memory, level shift=%d\n",
  38. shift);
  39. return NULL;
  40. }
  41. addr = page_address(tce_mem);
  42. memset(addr, 0, 1UL << shift);
  43. return addr;
  44. }
  45. static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
  46. unsigned long size, unsigned int levels);
  47. static __be64 *pnv_tce(struct iommu_table *tbl, bool user, long idx, bool alloc)
  48. {
  49. __be64 *tmp = user ? tbl->it_userspace : (__be64 *) tbl->it_base;
  50. int level = tbl->it_indirect_levels;
  51. const long shift = ilog2(tbl->it_level_size);
  52. unsigned long mask = (tbl->it_level_size - 1) << (level * shift);
  53. while (level) {
  54. int n = (idx & mask) >> (level * shift);
  55. unsigned long oldtce, tce = be64_to_cpu(READ_ONCE(tmp[n]));
  56. if (!tce) {
  57. __be64 *tmp2;
  58. if (!alloc)
  59. return NULL;
  60. tmp2 = pnv_alloc_tce_level(tbl->it_nid,
  61. ilog2(tbl->it_level_size) + 3);
  62. if (!tmp2)
  63. return NULL;
  64. tce = __pa(tmp2) | TCE_PCI_READ | TCE_PCI_WRITE;
  65. oldtce = be64_to_cpu(cmpxchg(&tmp[n], 0,
  66. cpu_to_be64(tce)));
  67. if (oldtce) {
  68. pnv_pci_ioda2_table_do_free_pages(tmp2,
  69. ilog2(tbl->it_level_size) + 3, 1);
  70. tce = oldtce;
  71. }
  72. }
  73. tmp = __va(tce & ~(TCE_PCI_READ | TCE_PCI_WRITE));
  74. idx &= ~mask;
  75. mask >>= shift;
  76. --level;
  77. }
  78. return tmp + idx;
  79. }
  80. int pnv_tce_build(struct iommu_table *tbl, long index, long npages,
  81. unsigned long uaddr, enum dma_data_direction direction,
  82. unsigned long attrs)
  83. {
  84. u64 proto_tce = iommu_direction_to_tce_perm(direction);
  85. u64 rpn = __pa(uaddr) >> tbl->it_page_shift;
  86. long i;
  87. if (proto_tce & TCE_PCI_WRITE)
  88. proto_tce |= TCE_PCI_READ;
  89. for (i = 0; i < npages; i++) {
  90. unsigned long newtce = proto_tce |
  91. ((rpn + i) << tbl->it_page_shift);
  92. unsigned long idx = index - tbl->it_offset + i;
  93. *(pnv_tce(tbl, false, idx, true)) = cpu_to_be64(newtce);
  94. }
  95. return 0;
  96. }
  97. #ifdef CONFIG_IOMMU_API
  98. int pnv_tce_xchg(struct iommu_table *tbl, long index,
  99. unsigned long *hpa, enum dma_data_direction *direction,
  100. bool alloc)
  101. {
  102. u64 proto_tce = iommu_direction_to_tce_perm(*direction);
  103. unsigned long newtce = *hpa | proto_tce, oldtce;
  104. unsigned long idx = index - tbl->it_offset;
  105. __be64 *ptce = NULL;
  106. BUG_ON(*hpa & ~IOMMU_PAGE_MASK(tbl));
  107. if (*direction == DMA_NONE) {
  108. ptce = pnv_tce(tbl, false, idx, false);
  109. if (!ptce) {
  110. *hpa = 0;
  111. return 0;
  112. }
  113. }
  114. if (!ptce) {
  115. ptce = pnv_tce(tbl, false, idx, alloc);
  116. if (!ptce)
  117. return alloc ? H_HARDWARE : H_TOO_HARD;
  118. }
  119. if (newtce & TCE_PCI_WRITE)
  120. newtce |= TCE_PCI_READ;
  121. oldtce = be64_to_cpu(xchg(ptce, cpu_to_be64(newtce)));
  122. *hpa = oldtce & ~(TCE_PCI_READ | TCE_PCI_WRITE);
  123. *direction = iommu_tce_direction(oldtce);
  124. return 0;
  125. }
  126. __be64 *pnv_tce_useraddrptr(struct iommu_table *tbl, long index, bool alloc)
  127. {
  128. if (WARN_ON_ONCE(!tbl->it_userspace))
  129. return NULL;
  130. return pnv_tce(tbl, true, index - tbl->it_offset, alloc);
  131. }
  132. #endif
  133. void pnv_tce_free(struct iommu_table *tbl, long index, long npages)
  134. {
  135. long i;
  136. for (i = 0; i < npages; i++) {
  137. unsigned long idx = index - tbl->it_offset + i;
  138. __be64 *ptce = pnv_tce(tbl, false, idx, false);
  139. if (ptce)
  140. *ptce = cpu_to_be64(0);
  141. else
  142. /* Skip the rest of the level */
  143. i |= tbl->it_level_size - 1;
  144. }
  145. }
  146. unsigned long pnv_tce_get(struct iommu_table *tbl, long index)
  147. {
  148. __be64 *ptce = pnv_tce(tbl, false, index - tbl->it_offset, false);
  149. if (!ptce)
  150. return 0;
  151. return be64_to_cpu(*ptce);
  152. }
  153. static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
  154. unsigned long size, unsigned int levels)
  155. {
  156. const unsigned long addr_ul = (unsigned long) addr &
  157. ~(TCE_PCI_READ | TCE_PCI_WRITE);
  158. if (levels) {
  159. long i;
  160. u64 *tmp = (u64 *) addr_ul;
  161. for (i = 0; i < size; ++i) {
  162. unsigned long hpa = be64_to_cpu(tmp[i]);
  163. if (!(hpa & (TCE_PCI_READ | TCE_PCI_WRITE)))
  164. continue;
  165. pnv_pci_ioda2_table_do_free_pages(__va(hpa), size,
  166. levels - 1);
  167. }
  168. }
  169. free_pages(addr_ul, get_order(size << 3));
  170. }
  171. void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl)
  172. {
  173. const unsigned long size = tbl->it_indirect_levels ?
  174. tbl->it_level_size : tbl->it_size;
  175. if (!tbl->it_size)
  176. return;
  177. pnv_pci_ioda2_table_do_free_pages((__be64 *)tbl->it_base, size,
  178. tbl->it_indirect_levels);
  179. if (tbl->it_userspace) {
  180. pnv_pci_ioda2_table_do_free_pages(tbl->it_userspace, size,
  181. tbl->it_indirect_levels);
  182. }
  183. }
  184. static __be64 *pnv_pci_ioda2_table_do_alloc_pages(int nid, unsigned int shift,
  185. unsigned int levels, unsigned long limit,
  186. unsigned long *current_offset, unsigned long *total_allocated)
  187. {
  188. __be64 *addr, *tmp;
  189. unsigned long allocated = 1UL << shift;
  190. unsigned int entries = 1UL << (shift - 3);
  191. long i;
  192. addr = pnv_alloc_tce_level(nid, shift);
  193. *total_allocated += allocated;
  194. --levels;
  195. if (!levels) {
  196. *current_offset += allocated;
  197. return addr;
  198. }
  199. for (i = 0; i < entries; ++i) {
  200. tmp = pnv_pci_ioda2_table_do_alloc_pages(nid, shift,
  201. levels, limit, current_offset, total_allocated);
  202. if (!tmp)
  203. break;
  204. addr[i] = cpu_to_be64(__pa(tmp) |
  205. TCE_PCI_READ | TCE_PCI_WRITE);
  206. if (*current_offset >= limit)
  207. break;
  208. }
  209. return addr;
  210. }
  211. long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
  212. __u32 page_shift, __u64 window_size, __u32 levels,
  213. bool alloc_userspace_copy, struct iommu_table *tbl)
  214. {
  215. void *addr, *uas = NULL;
  216. unsigned long offset = 0, level_shift, total_allocated = 0;
  217. unsigned long total_allocated_uas = 0;
  218. const unsigned int window_shift = ilog2(window_size);
  219. unsigned int entries_shift = window_shift - page_shift;
  220. unsigned int table_shift = max_t(unsigned int, entries_shift + 3,
  221. PAGE_SHIFT);
  222. const unsigned long tce_table_size = 1UL << table_shift;
  223. if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS))
  224. return -EINVAL;
  225. if (!is_power_of_2(window_size))
  226. return -EINVAL;
  227. /* Adjust direct table size from window_size and levels */
  228. entries_shift = (entries_shift + levels - 1) / levels;
  229. level_shift = entries_shift + 3;
  230. level_shift = max_t(unsigned int, level_shift, PAGE_SHIFT);
  231. if ((level_shift - 3) * levels + page_shift >= 55)
  232. return -EINVAL;
  233. /* Allocate TCE table */
  234. addr = pnv_pci_ioda2_table_do_alloc_pages(nid, level_shift,
  235. 1, tce_table_size, &offset, &total_allocated);
  236. /* addr==NULL means that the first level allocation failed */
  237. if (!addr)
  238. return -ENOMEM;
  239. /*
  240. * First level was allocated but some lower level failed as
  241. * we did not allocate as much as we wanted,
  242. * release partially allocated table.
  243. */
  244. if (levels == 1 && offset < tce_table_size)
  245. goto free_tces_exit;
  246. /* Allocate userspace view of the TCE table */
  247. if (alloc_userspace_copy) {
  248. offset = 0;
  249. uas = pnv_pci_ioda2_table_do_alloc_pages(nid, level_shift,
  250. 1, tce_table_size, &offset,
  251. &total_allocated_uas);
  252. if (!uas)
  253. goto free_tces_exit;
  254. if (levels == 1 && (offset < tce_table_size ||
  255. total_allocated_uas != total_allocated))
  256. goto free_uas_exit;
  257. }
  258. /* Setup linux iommu table */
  259. pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, bus_offset,
  260. page_shift);
  261. tbl->it_level_size = 1ULL << (level_shift - 3);
  262. tbl->it_indirect_levels = levels - 1;
  263. tbl->it_userspace = uas;
  264. tbl->it_nid = nid;
  265. pr_debug("Created TCE table: ws=%08llx ts=%lx @%08llx base=%lx uas=%p levels=%d/%d\n",
  266. window_size, tce_table_size, bus_offset, tbl->it_base,
  267. tbl->it_userspace, 1, levels);
  268. return 0;
  269. free_uas_exit:
  270. pnv_pci_ioda2_table_do_free_pages(uas,
  271. 1ULL << (level_shift - 3), levels - 1);
  272. free_tces_exit:
  273. pnv_pci_ioda2_table_do_free_pages(addr,
  274. 1ULL << (level_shift - 3), levels - 1);
  275. return -ENOMEM;
  276. }
  277. static void pnv_iommu_table_group_link_free(struct rcu_head *head)
  278. {
  279. struct iommu_table_group_link *tgl = container_of(head,
  280. struct iommu_table_group_link, rcu);
  281. kfree(tgl);
  282. }
  283. void pnv_pci_unlink_table_and_group(struct iommu_table *tbl,
  284. struct iommu_table_group *table_group)
  285. {
  286. long i;
  287. bool found;
  288. struct iommu_table_group_link *tgl;
  289. if (!tbl || !table_group)
  290. return;
  291. /* Remove link to a group from table's list of attached groups */
  292. found = false;
  293. list_for_each_entry_rcu(tgl, &tbl->it_group_list, next) {
  294. if (tgl->table_group == table_group) {
  295. list_del_rcu(&tgl->next);
  296. call_rcu(&tgl->rcu, pnv_iommu_table_group_link_free);
  297. found = true;
  298. break;
  299. }
  300. }
  301. if (WARN_ON(!found))
  302. return;
  303. /* Clean a pointer to iommu_table in iommu_table_group::tables[] */
  304. found = false;
  305. for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i) {
  306. if (table_group->tables[i] == tbl) {
  307. table_group->tables[i] = NULL;
  308. found = true;
  309. break;
  310. }
  311. }
  312. WARN_ON(!found);
  313. }
  314. long pnv_pci_link_table_and_group(int node, int num,
  315. struct iommu_table *tbl,
  316. struct iommu_table_group *table_group)
  317. {
  318. struct iommu_table_group_link *tgl = NULL;
  319. if (WARN_ON(!tbl || !table_group))
  320. return -EINVAL;
  321. tgl = kzalloc_node(sizeof(struct iommu_table_group_link), GFP_KERNEL,
  322. node);
  323. if (!tgl)
  324. return -ENOMEM;
  325. tgl->table_group = table_group;
  326. list_add_rcu(&tgl->next, &tbl->it_group_list);
  327. table_group->tables[num] = tbl;
  328. return 0;
  329. }