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 unsigned long pnv_ioda_parse_tce_sizes(struct pnv_phb *phb)
22 struct pci_controller *hose = phb->hose;
23 struct device_node *dn = hose->dn;
24 unsigned long mask = 0;
28 count = of_property_count_u32_elems(dn, "ibm,supported-tce-sizes");
30 mask = SZ_4K | SZ_64K;
31 /* Add 16M for POWER8 by default */
32 if (cpu_has_feature(CPU_FTR_ARCH_207S) &&
33 !cpu_has_feature(CPU_FTR_ARCH_300))
34 mask |= SZ_16M | SZ_256M;
38 for (i = 0; i < count; i++) {
39 rc = of_property_read_u32_index(dn, "ibm,supported-tce-sizes",
48 void pnv_pci_setup_iommu_table(struct iommu_table *tbl,
49 void *tce_mem, u64 tce_size,
50 u64 dma_offset, unsigned int page_shift)
52 tbl->it_blocksize = 16;
53 tbl->it_base = (unsigned long)tce_mem;
54 tbl->it_page_shift = page_shift;
55 tbl->it_offset = dma_offset >> tbl->it_page_shift;
57 tbl->it_size = tce_size >> 3;
59 tbl->it_type = TCE_PCI;
62 static __be64 *pnv_alloc_tce_level(int nid, unsigned int shift)
64 struct page *tce_mem = NULL;
67 tce_mem = alloc_pages_node(nid, GFP_ATOMIC | __GFP_NOWARN,
70 pr_err("Failed to allocate a TCE memory, level shift=%d\n",
74 addr = page_address(tce_mem);
75 memset(addr, 0, 1UL << shift);
80 static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
81 unsigned long size, unsigned int levels);
83 static __be64 *pnv_tce(struct iommu_table *tbl, bool user, long idx, bool alloc)
85 __be64 *tmp = user ? tbl->it_userspace : (__be64 *) tbl->it_base;
86 int level = tbl->it_indirect_levels;
87 const long shift = ilog2(tbl->it_level_size);
88 unsigned long mask = (tbl->it_level_size - 1) << (level * shift);
91 int n = (idx & mask) >> (level * shift);
92 unsigned long oldtce, tce = be64_to_cpu(READ_ONCE(tmp[n]));
100 tmp2 = pnv_alloc_tce_level(tbl->it_nid,
101 ilog2(tbl->it_level_size) + 3);
105 tce = __pa(tmp2) | TCE_PCI_READ | TCE_PCI_WRITE;
106 oldtce = be64_to_cpu(cmpxchg(&tmp[n], 0,
109 pnv_pci_ioda2_table_do_free_pages(tmp2,
110 ilog2(tbl->it_level_size) + 3, 1);
115 tmp = __va(tce & ~(TCE_PCI_READ | TCE_PCI_WRITE));
124 int pnv_tce_build(struct iommu_table *tbl, long index, long npages,
125 unsigned long uaddr, enum dma_data_direction direction,
128 u64 proto_tce = iommu_direction_to_tce_perm(direction);
129 u64 rpn = __pa(uaddr) >> tbl->it_page_shift;
132 if (proto_tce & TCE_PCI_WRITE)
133 proto_tce |= TCE_PCI_READ;
135 for (i = 0; i < npages; i++) {
136 unsigned long newtce = proto_tce |
137 ((rpn + i) << tbl->it_page_shift);
138 unsigned long idx = index - tbl->it_offset + i;
140 *(pnv_tce(tbl, false, idx, true)) = cpu_to_be64(newtce);
146 #ifdef CONFIG_IOMMU_API
147 int pnv_tce_xchg(struct iommu_table *tbl, long index,
148 unsigned long *hpa, enum dma_data_direction *direction)
150 u64 proto_tce = iommu_direction_to_tce_perm(*direction);
151 unsigned long newtce = *hpa | proto_tce, oldtce;
152 unsigned long idx = index - tbl->it_offset;
155 BUG_ON(*hpa & ~IOMMU_PAGE_MASK(tbl));
157 if (*direction == DMA_NONE) {
158 ptce = pnv_tce(tbl, false, idx, false);
166 ptce = pnv_tce(tbl, false, idx, true);
171 if (newtce & TCE_PCI_WRITE)
172 newtce |= TCE_PCI_READ;
174 oldtce = be64_to_cpu(xchg(ptce, cpu_to_be64(newtce)));
175 *hpa = oldtce & ~(TCE_PCI_READ | TCE_PCI_WRITE);
176 *direction = iommu_tce_direction(oldtce);
181 __be64 *pnv_tce_useraddrptr(struct iommu_table *tbl, long index, bool alloc)
183 if (WARN_ON_ONCE(!tbl->it_userspace))
186 return pnv_tce(tbl, true, index - tbl->it_offset, alloc);
190 void pnv_tce_free(struct iommu_table *tbl, long index, long npages)
194 for (i = 0; i < npages; i++) {
195 unsigned long idx = index - tbl->it_offset + i;
196 __be64 *ptce = pnv_tce(tbl, false, idx, false);
199 *ptce = cpu_to_be64(0);
201 /* Skip the rest of the level */
202 i |= tbl->it_level_size - 1;
206 unsigned long pnv_tce_get(struct iommu_table *tbl, long index)
208 __be64 *ptce = pnv_tce(tbl, false, index - tbl->it_offset, false);
213 return be64_to_cpu(*ptce);
216 static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
217 unsigned long size, unsigned int levels)
219 const unsigned long addr_ul = (unsigned long) addr &
220 ~(TCE_PCI_READ | TCE_PCI_WRITE);
224 u64 *tmp = (u64 *) addr_ul;
226 for (i = 0; i < size; ++i) {
227 unsigned long hpa = be64_to_cpu(tmp[i]);
229 if (!(hpa & (TCE_PCI_READ | TCE_PCI_WRITE)))
232 pnv_pci_ioda2_table_do_free_pages(__va(hpa), size,
237 free_pages(addr_ul, get_order(size << 3));
240 void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl)
242 const unsigned long size = tbl->it_indirect_levels ?
243 tbl->it_level_size : tbl->it_size;
248 pnv_pci_ioda2_table_do_free_pages((__be64 *)tbl->it_base, size,
249 tbl->it_indirect_levels);
250 if (tbl->it_userspace) {
251 pnv_pci_ioda2_table_do_free_pages(tbl->it_userspace, size,
252 tbl->it_indirect_levels);
256 static __be64 *pnv_pci_ioda2_table_do_alloc_pages(int nid, unsigned int shift,
257 unsigned int levels, unsigned long limit,
258 unsigned long *current_offset, unsigned long *total_allocated)
261 unsigned long allocated = 1UL << shift;
262 unsigned int entries = 1UL << (shift - 3);
265 addr = pnv_alloc_tce_level(nid, shift);
266 *total_allocated += allocated;
270 *current_offset += allocated;
274 for (i = 0; i < entries; ++i) {
275 tmp = pnv_pci_ioda2_table_do_alloc_pages(nid, shift,
276 levels, limit, current_offset, total_allocated);
280 addr[i] = cpu_to_be64(__pa(tmp) |
281 TCE_PCI_READ | TCE_PCI_WRITE);
283 if (*current_offset >= limit)
290 long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
291 __u32 page_shift, __u64 window_size, __u32 levels,
292 bool alloc_userspace_copy, struct iommu_table *tbl)
294 void *addr, *uas = NULL;
295 unsigned long offset = 0, level_shift, total_allocated = 0;
296 unsigned long total_allocated_uas = 0;
297 const unsigned int window_shift = ilog2(window_size);
298 unsigned int entries_shift = window_shift - page_shift;
299 unsigned int table_shift = max_t(unsigned int, entries_shift + 3,
301 const unsigned long tce_table_size = 1UL << table_shift;
303 if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS))
306 if (!is_power_of_2(window_size))
309 /* Adjust direct table size from window_size and levels */
310 entries_shift = (entries_shift + levels - 1) / levels;
311 level_shift = entries_shift + 3;
312 level_shift = max_t(unsigned int, level_shift, PAGE_SHIFT);
314 if ((level_shift - 3) * levels + page_shift >= 55)
317 /* Allocate TCE table */
318 addr = pnv_pci_ioda2_table_do_alloc_pages(nid, level_shift,
319 1, tce_table_size, &offset, &total_allocated);
321 /* addr==NULL means that the first level allocation failed */
326 * First level was allocated but some lower level failed as
327 * we did not allocate as much as we wanted,
328 * release partially allocated table.
330 if (levels == 1 && offset < tce_table_size)
333 /* Allocate userspace view of the TCE table */
334 if (alloc_userspace_copy) {
336 uas = pnv_pci_ioda2_table_do_alloc_pages(nid, level_shift,
337 1, tce_table_size, &offset,
338 &total_allocated_uas);
341 if (levels == 1 && (offset < tce_table_size ||
342 total_allocated_uas != total_allocated))
346 /* Setup linux iommu table */
347 pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, bus_offset,
349 tbl->it_level_size = 1ULL << (level_shift - 3);
350 tbl->it_indirect_levels = levels - 1;
351 tbl->it_userspace = uas;
354 pr_debug("Created TCE table: ws=%08llx ts=%lx @%08llx base=%lx uas=%p levels=%d/%d\n",
355 window_size, tce_table_size, bus_offset, tbl->it_base,
356 tbl->it_userspace, 1, levels);
361 pnv_pci_ioda2_table_do_free_pages(uas,
362 1ULL << (level_shift - 3), levels - 1);
364 pnv_pci_ioda2_table_do_free_pages(addr,
365 1ULL << (level_shift - 3), levels - 1);
370 void pnv_pci_unlink_table_and_group(struct iommu_table *tbl,
371 struct iommu_table_group *table_group)
375 struct iommu_table_group_link *tgl;
377 if (!tbl || !table_group)
380 /* Remove link to a group from table's list of attached groups */
384 list_for_each_entry_rcu(tgl, &tbl->it_group_list, next) {
385 if (tgl->table_group == table_group) {
386 list_del_rcu(&tgl->next);
397 /* Clean a pointer to iommu_table in iommu_table_group::tables[] */
399 for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i) {
400 if (table_group->tables[i] == tbl) {
401 iommu_tce_table_put(tbl);
402 table_group->tables[i] = NULL;
410 long pnv_pci_link_table_and_group(int node, int num,
411 struct iommu_table *tbl,
412 struct iommu_table_group *table_group)
414 struct iommu_table_group_link *tgl = NULL;
416 if (WARN_ON(!tbl || !table_group))
419 tgl = kzalloc_node(sizeof(struct iommu_table_group_link), GFP_KERNEL,
424 tgl->table_group = table_group;
425 list_add_rcu(&tgl->next, &tbl->it_group_list);
427 table_group->tables[num] = iommu_tce_table_get(tbl);