1 // SPDX-License-Identifier: GPL-2.0+
3 * TCE helpers for IODA PCI/PCIe on PowerNV platforms
5 * Copyright 2018 IBM Corp.
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.
13 #include <linux/kernel.h>
14 #include <linux/iommu.h>
16 #include <asm/iommu.h>
20 void pnv_pci_setup_iommu_table(struct iommu_table *tbl,
21 void *tce_mem, u64 tce_size,
22 u64 dma_offset, unsigned int page_shift)
24 tbl->it_blocksize = 16;
25 tbl->it_base = (unsigned long)tce_mem;
26 tbl->it_page_shift = page_shift;
27 tbl->it_offset = dma_offset >> tbl->it_page_shift;
29 tbl->it_size = tce_size >> 3;
31 tbl->it_type = TCE_PCI;
34 static __be64 *pnv_alloc_tce_level(int nid, unsigned int shift)
36 struct page *tce_mem = NULL;
39 tce_mem = alloc_pages_node(nid, GFP_KERNEL, shift - PAGE_SHIFT);
41 pr_err("Failed to allocate a TCE memory, level shift=%d\n",
45 addr = page_address(tce_mem);
46 memset(addr, 0, 1UL << shift);
51 static __be64 *pnv_tce(struct iommu_table *tbl, bool user, long idx, bool alloc)
53 __be64 *tmp = user ? tbl->it_userspace : (__be64 *) tbl->it_base;
54 int level = tbl->it_indirect_levels;
55 const long shift = ilog2(tbl->it_level_size);
56 unsigned long mask = (tbl->it_level_size - 1) << (level * shift);
59 int n = (idx & mask) >> (level * shift);
68 tmp2 = pnv_alloc_tce_level(tbl->it_nid,
69 ilog2(tbl->it_level_size) + 3);
73 tmp[n] = cpu_to_be64(__pa(tmp2) |
74 TCE_PCI_READ | TCE_PCI_WRITE);
76 tce = be64_to_cpu(tmp[n]);
78 tmp = __va(tce & ~(TCE_PCI_READ | TCE_PCI_WRITE));
87 int pnv_tce_build(struct iommu_table *tbl, long index, long npages,
88 unsigned long uaddr, enum dma_data_direction direction,
91 u64 proto_tce = iommu_direction_to_tce_perm(direction);
92 u64 rpn = __pa(uaddr) >> tbl->it_page_shift;
95 if (proto_tce & TCE_PCI_WRITE)
96 proto_tce |= TCE_PCI_READ;
98 for (i = 0; i < npages; i++) {
99 unsigned long newtce = proto_tce |
100 ((rpn + i) << tbl->it_page_shift);
101 unsigned long idx = index - tbl->it_offset + i;
103 *(pnv_tce(tbl, false, idx, true)) = cpu_to_be64(newtce);
109 #ifdef CONFIG_IOMMU_API
110 int pnv_tce_xchg(struct iommu_table *tbl, long index,
111 unsigned long *hpa, enum dma_data_direction *direction,
114 u64 proto_tce = iommu_direction_to_tce_perm(*direction);
115 unsigned long newtce = *hpa | proto_tce, oldtce;
116 unsigned long idx = index - tbl->it_offset;
119 BUG_ON(*hpa & ~IOMMU_PAGE_MASK(tbl));
121 if (*direction == DMA_NONE) {
122 ptce = pnv_tce(tbl, false, idx, false);
130 ptce = pnv_tce(tbl, false, idx, alloc);
132 return alloc ? H_HARDWARE : H_TOO_HARD;
135 if (newtce & TCE_PCI_WRITE)
136 newtce |= TCE_PCI_READ;
138 oldtce = be64_to_cpu(xchg(ptce, cpu_to_be64(newtce)));
139 *hpa = oldtce & ~(TCE_PCI_READ | TCE_PCI_WRITE);
140 *direction = iommu_tce_direction(oldtce);
145 __be64 *pnv_tce_useraddrptr(struct iommu_table *tbl, long index, bool alloc)
147 if (WARN_ON_ONCE(!tbl->it_userspace))
150 return pnv_tce(tbl, true, index - tbl->it_offset, alloc);
154 void pnv_tce_free(struct iommu_table *tbl, long index, long npages)
158 for (i = 0; i < npages; i++) {
159 unsigned long idx = index - tbl->it_offset + i;
160 __be64 *ptce = pnv_tce(tbl, false, idx, false);
163 *ptce = cpu_to_be64(0);
167 unsigned long pnv_tce_get(struct iommu_table *tbl, long index)
169 __be64 *ptce = pnv_tce(tbl, false, index - tbl->it_offset, false);
174 return be64_to_cpu(*ptce);
177 static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
178 unsigned long size, unsigned int levels)
180 const unsigned long addr_ul = (unsigned long) addr &
181 ~(TCE_PCI_READ | TCE_PCI_WRITE);
185 u64 *tmp = (u64 *) addr_ul;
187 for (i = 0; i < size; ++i) {
188 unsigned long hpa = be64_to_cpu(tmp[i]);
190 if (!(hpa & (TCE_PCI_READ | TCE_PCI_WRITE)))
193 pnv_pci_ioda2_table_do_free_pages(__va(hpa), size,
198 free_pages(addr_ul, get_order(size << 3));
201 void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl)
203 const unsigned long size = tbl->it_indirect_levels ?
204 tbl->it_level_size : tbl->it_size;
209 pnv_pci_ioda2_table_do_free_pages((__be64 *)tbl->it_base, size,
210 tbl->it_indirect_levels);
211 if (tbl->it_userspace) {
212 pnv_pci_ioda2_table_do_free_pages(tbl->it_userspace, size,
213 tbl->it_indirect_levels);
217 static __be64 *pnv_pci_ioda2_table_do_alloc_pages(int nid, unsigned int shift,
218 unsigned int levels, unsigned long limit,
219 unsigned long *current_offset, unsigned long *total_allocated)
222 unsigned long allocated = 1UL << shift;
223 unsigned int entries = 1UL << (shift - 3);
226 addr = pnv_alloc_tce_level(nid, shift);
227 *total_allocated += allocated;
231 *current_offset += allocated;
235 for (i = 0; i < entries; ++i) {
236 tmp = pnv_pci_ioda2_table_do_alloc_pages(nid, shift,
237 levels, limit, current_offset, total_allocated);
241 addr[i] = cpu_to_be64(__pa(tmp) |
242 TCE_PCI_READ | TCE_PCI_WRITE);
244 if (*current_offset >= limit)
251 long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
252 __u32 page_shift, __u64 window_size, __u32 levels,
253 bool alloc_userspace_copy, struct iommu_table *tbl)
255 void *addr, *uas = NULL;
256 unsigned long offset = 0, level_shift, total_allocated = 0;
257 unsigned long total_allocated_uas = 0;
258 const unsigned int window_shift = ilog2(window_size);
259 unsigned int entries_shift = window_shift - page_shift;
260 unsigned int table_shift = max_t(unsigned int, entries_shift + 3,
262 const unsigned long tce_table_size = 1UL << table_shift;
263 unsigned int tmplevels = levels;
265 if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS))
268 if (!is_power_of_2(window_size))
271 if (alloc_userspace_copy && (window_size > (1ULL << 32)))
274 /* Adjust direct table size from window_size and levels */
275 entries_shift = (entries_shift + levels - 1) / levels;
276 level_shift = entries_shift + 3;
277 level_shift = max_t(unsigned int, level_shift, PAGE_SHIFT);
279 if ((level_shift - 3) * levels + page_shift >= 55)
282 /* Allocate TCE table */
283 addr = pnv_pci_ioda2_table_do_alloc_pages(nid, level_shift,
284 tmplevels, tce_table_size, &offset, &total_allocated);
286 /* addr==NULL means that the first level allocation failed */
291 * First level was allocated but some lower level failed as
292 * we did not allocate as much as we wanted,
293 * release partially allocated table.
295 if (tmplevels == levels && offset < tce_table_size)
298 /* Allocate userspace view of the TCE table */
299 if (alloc_userspace_copy) {
301 uas = pnv_pci_ioda2_table_do_alloc_pages(nid, level_shift,
302 levels, tce_table_size, &offset,
303 &total_allocated_uas);
306 if (tmplevels == levels && (offset < tce_table_size ||
307 total_allocated_uas != total_allocated))
311 /* Setup linux iommu table */
312 pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, bus_offset,
314 tbl->it_level_size = 1ULL << (level_shift - 3);
315 tbl->it_indirect_levels = levels - 1;
316 tbl->it_allocated_size = total_allocated;
317 tbl->it_userspace = uas;
320 pr_debug("Created TCE table: ws=%08llx ts=%lx @%08llx base=%lx uas=%p levels=%d/%d\n",
321 window_size, tce_table_size, bus_offset, tbl->it_base,
322 tbl->it_userspace, tmplevels, levels);
327 pnv_pci_ioda2_table_do_free_pages(uas,
328 1ULL << (level_shift - 3), levels - 1);
330 pnv_pci_ioda2_table_do_free_pages(addr,
331 1ULL << (level_shift - 3), levels - 1);
336 static void pnv_iommu_table_group_link_free(struct rcu_head *head)
338 struct iommu_table_group_link *tgl = container_of(head,
339 struct iommu_table_group_link, rcu);
344 void pnv_pci_unlink_table_and_group(struct iommu_table *tbl,
345 struct iommu_table_group *table_group)
349 struct iommu_table_group_link *tgl;
351 if (!tbl || !table_group)
354 /* Remove link to a group from table's list of attached groups */
356 list_for_each_entry_rcu(tgl, &tbl->it_group_list, next) {
357 if (tgl->table_group == table_group) {
358 list_del_rcu(&tgl->next);
359 call_rcu(&tgl->rcu, pnv_iommu_table_group_link_free);
367 /* Clean a pointer to iommu_table in iommu_table_group::tables[] */
369 for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i) {
370 if (table_group->tables[i] == tbl) {
371 table_group->tables[i] = NULL;
379 long pnv_pci_link_table_and_group(int node, int num,
380 struct iommu_table *tbl,
381 struct iommu_table_group *table_group)
383 struct iommu_table_group_link *tgl = NULL;
385 if (WARN_ON(!tbl || !table_group))
388 tgl = kzalloc_node(sizeof(struct iommu_table_group_link), GFP_KERNEL,
393 tgl->table_group = table_group;
394 list_add_rcu(&tgl->next, &tbl->it_group_list);
396 table_group->tables[num] = tbl;