Path: blob/master/arch/powerpc/platforms/powernv/pci-ioda-tce.c
26481 views
// SPDX-License-Identifier: GPL-2.0+1/*2* TCE helpers for IODA PCI/PCIe on PowerNV platforms3*4* Copyright 2018 IBM Corp.5*6* This program is free software; you can redistribute it and/or7* modify it under the terms of the GNU General Public License8* as published by the Free Software Foundation; either version9* 2 of the License, or (at your option) any later version.10*/1112#include <linux/kernel.h>13#include <linux/iommu.h>1415#include <asm/iommu.h>16#include <asm/tce.h>17#include "pci.h"1819unsigned long pnv_ioda_parse_tce_sizes(struct pnv_phb *phb)20{21struct pci_controller *hose = phb->hose;22struct device_node *dn = hose->dn;23unsigned long mask = 0;24int i, rc, count;25u32 val;2627count = of_property_count_u32_elems(dn, "ibm,supported-tce-sizes");28if (count <= 0) {29mask = SZ_4K | SZ_64K;30/* Add 16M for POWER8 by default */31if (cpu_has_feature(CPU_FTR_ARCH_207S) &&32!cpu_has_feature(CPU_FTR_ARCH_300))33mask |= SZ_16M | SZ_256M;34return mask;35}3637for (i = 0; i < count; i++) {38rc = of_property_read_u32_index(dn, "ibm,supported-tce-sizes",39i, &val);40if (rc == 0)41mask |= 1ULL << val;42}4344return mask;45}4647void pnv_pci_setup_iommu_table(struct iommu_table *tbl,48void *tce_mem, u64 tce_size,49u64 dma_offset, unsigned int page_shift)50{51tbl->it_blocksize = 16;52tbl->it_base = (unsigned long)tce_mem;53tbl->it_page_shift = page_shift;54tbl->it_offset = dma_offset >> tbl->it_page_shift;55tbl->it_index = 0;56tbl->it_size = tce_size >> 3;57tbl->it_busno = 0;58tbl->it_type = TCE_PCI;59}6061static __be64 *pnv_alloc_tce_level(int nid, unsigned int shift)62{63struct page *tce_mem = NULL;64__be64 *addr;6566tce_mem = alloc_pages_node(nid, GFP_ATOMIC | __GFP_NOWARN,67shift - PAGE_SHIFT);68if (!tce_mem) {69pr_err("Failed to allocate a TCE memory, level shift=%d\n",70shift);71return NULL;72}73addr = page_address(tce_mem);74memset(addr, 0, 1UL << shift);7576return addr;77}7879static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,80unsigned long size, unsigned int levels);8182static __be64 *pnv_tce(struct iommu_table *tbl, bool user, long idx, bool alloc)83{84__be64 *tmp = user ? tbl->it_userspace : (__be64 *) tbl->it_base;85int level = tbl->it_indirect_levels;86const long shift = ilog2(tbl->it_level_size);87unsigned long mask = (tbl->it_level_size - 1) << (level * shift);8889while (level) {90int n = (idx & mask) >> (level * shift);91unsigned long oldtce, tce = be64_to_cpu(READ_ONCE(tmp[n]));9293if (!tce) {94__be64 *tmp2;9596if (!alloc)97return NULL;9899tmp2 = pnv_alloc_tce_level(tbl->it_nid,100ilog2(tbl->it_level_size) + 3);101if (!tmp2)102return NULL;103104tce = __pa(tmp2) | TCE_PCI_READ | TCE_PCI_WRITE;105oldtce = be64_to_cpu(cmpxchg(&tmp[n], 0,106cpu_to_be64(tce)));107if (oldtce) {108pnv_pci_ioda2_table_do_free_pages(tmp2,109ilog2(tbl->it_level_size) + 3, 1);110tce = oldtce;111}112}113114tmp = __va(tce & ~(TCE_PCI_READ | TCE_PCI_WRITE));115idx &= ~mask;116mask >>= shift;117--level;118}119120return tmp + idx;121}122123int pnv_tce_build(struct iommu_table *tbl, long index, long npages,124unsigned long uaddr, enum dma_data_direction direction,125unsigned long attrs)126{127u64 proto_tce = iommu_direction_to_tce_perm(direction);128u64 rpn = __pa(uaddr) >> tbl->it_page_shift;129long i;130131if (proto_tce & TCE_PCI_WRITE)132proto_tce |= TCE_PCI_READ;133134for (i = 0; i < npages; i++) {135unsigned long newtce = proto_tce |136((rpn + i) << tbl->it_page_shift);137unsigned long idx = index - tbl->it_offset + i;138139*(pnv_tce(tbl, false, idx, true)) = cpu_to_be64(newtce);140}141142return 0;143}144145#ifdef CONFIG_IOMMU_API146int pnv_tce_xchg(struct iommu_table *tbl, long index,147unsigned long *hpa, enum dma_data_direction *direction)148{149u64 proto_tce = iommu_direction_to_tce_perm(*direction);150unsigned long newtce = *hpa | proto_tce, oldtce;151unsigned long idx = index - tbl->it_offset;152__be64 *ptce = NULL;153154BUG_ON(*hpa & ~IOMMU_PAGE_MASK(tbl));155156if (*direction == DMA_NONE) {157ptce = pnv_tce(tbl, false, idx, false);158if (!ptce) {159*hpa = 0;160return 0;161}162}163164if (!ptce) {165ptce = pnv_tce(tbl, false, idx, true);166if (!ptce)167return -ENOMEM;168}169170if (newtce & TCE_PCI_WRITE)171newtce |= TCE_PCI_READ;172173oldtce = be64_to_cpu(xchg(ptce, cpu_to_be64(newtce)));174*hpa = oldtce & ~(TCE_PCI_READ | TCE_PCI_WRITE);175*direction = iommu_tce_direction(oldtce);176177return 0;178}179180__be64 *pnv_tce_useraddrptr(struct iommu_table *tbl, long index, bool alloc)181{182if (WARN_ON_ONCE(!tbl->it_userspace))183return NULL;184185return pnv_tce(tbl, true, index - tbl->it_offset, alloc);186}187#endif188189void pnv_tce_free(struct iommu_table *tbl, long index, long npages)190{191long i;192193for (i = 0; i < npages; i++) {194unsigned long idx = index - tbl->it_offset + i;195__be64 *ptce = pnv_tce(tbl, false, idx, false);196197if (ptce)198*ptce = cpu_to_be64(0);199else200/* Skip the rest of the level */201i |= tbl->it_level_size - 1;202}203}204205unsigned long pnv_tce_get(struct iommu_table *tbl, long index)206{207__be64 *ptce = pnv_tce(tbl, false, index - tbl->it_offset, false);208209if (!ptce)210return 0;211212return be64_to_cpu(*ptce);213}214215static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,216unsigned long size, unsigned int levels)217{218const unsigned long addr_ul = (unsigned long) addr &219~(TCE_PCI_READ | TCE_PCI_WRITE);220221if (levels) {222long i;223u64 *tmp = (u64 *) addr_ul;224225for (i = 0; i < size; ++i) {226unsigned long hpa = be64_to_cpu(tmp[i]);227228if (!(hpa & (TCE_PCI_READ | TCE_PCI_WRITE)))229continue;230231pnv_pci_ioda2_table_do_free_pages(__va(hpa), size,232levels - 1);233}234}235236free_pages(addr_ul, get_order(size << 3));237}238239void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl)240{241const unsigned long size = tbl->it_indirect_levels ?242tbl->it_level_size : tbl->it_size;243244if (!tbl->it_size)245return;246247pnv_pci_ioda2_table_do_free_pages((__be64 *)tbl->it_base, size,248tbl->it_indirect_levels);249if (tbl->it_userspace) {250pnv_pci_ioda2_table_do_free_pages(tbl->it_userspace, size,251tbl->it_indirect_levels);252}253}254255static __be64 *pnv_pci_ioda2_table_do_alloc_pages(int nid, unsigned int shift,256unsigned int levels, unsigned long limit,257unsigned long *current_offset, unsigned long *total_allocated)258{259__be64 *addr, *tmp;260unsigned long allocated = 1UL << shift;261unsigned int entries = 1UL << (shift - 3);262long i;263264addr = pnv_alloc_tce_level(nid, shift);265*total_allocated += allocated;266267--levels;268if (!levels) {269*current_offset += allocated;270return addr;271}272273for (i = 0; i < entries; ++i) {274tmp = pnv_pci_ioda2_table_do_alloc_pages(nid, shift,275levels, limit, current_offset, total_allocated);276if (!tmp)277break;278279addr[i] = cpu_to_be64(__pa(tmp) |280TCE_PCI_READ | TCE_PCI_WRITE);281282if (*current_offset >= limit)283break;284}285286return addr;287}288289long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,290__u32 page_shift, __u64 window_size, __u32 levels,291bool alloc_userspace_copy, struct iommu_table *tbl)292{293void *addr, *uas = NULL;294unsigned long offset = 0, level_shift, total_allocated = 0;295unsigned long total_allocated_uas = 0;296const unsigned int window_shift = ilog2(window_size);297unsigned int entries_shift = window_shift - page_shift;298unsigned int table_shift = max_t(unsigned int, entries_shift + 3,299PAGE_SHIFT);300const unsigned long tce_table_size = 1UL << table_shift;301302if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS))303return -EINVAL;304305if (!is_power_of_2(window_size))306return -EINVAL;307308/* Adjust direct table size from window_size and levels */309entries_shift = (entries_shift + levels - 1) / levels;310level_shift = entries_shift + 3;311level_shift = max_t(unsigned int, level_shift, PAGE_SHIFT);312313if ((level_shift - 3) * levels + page_shift >= 55)314return -EINVAL;315316/* Allocate TCE table */317addr = pnv_pci_ioda2_table_do_alloc_pages(nid, level_shift,3181, tce_table_size, &offset, &total_allocated);319320/* addr==NULL means that the first level allocation failed */321if (!addr)322return -ENOMEM;323324/*325* First level was allocated but some lower level failed as326* we did not allocate as much as we wanted,327* release partially allocated table.328*/329if (levels == 1 && offset < tce_table_size)330goto free_tces_exit;331332/* Allocate userspace view of the TCE table */333if (alloc_userspace_copy) {334offset = 0;335uas = pnv_pci_ioda2_table_do_alloc_pages(nid, level_shift,3361, tce_table_size, &offset,337&total_allocated_uas);338if (!uas)339goto free_tces_exit;340if (levels == 1 && (offset < tce_table_size ||341total_allocated_uas != total_allocated))342goto free_uas_exit;343}344345/* Setup linux iommu table */346pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, bus_offset,347page_shift);348tbl->it_level_size = 1ULL << (level_shift - 3);349tbl->it_indirect_levels = levels - 1;350tbl->it_userspace = uas;351tbl->it_nid = nid;352353pr_debug("Created TCE table: ws=%08llx ts=%lx @%08llx base=%lx uas=%p levels=%d/%d\n",354window_size, tce_table_size, bus_offset, tbl->it_base,355tbl->it_userspace, 1, levels);356357return 0;358359free_uas_exit:360pnv_pci_ioda2_table_do_free_pages(uas,3611ULL << (level_shift - 3), levels - 1);362free_tces_exit:363pnv_pci_ioda2_table_do_free_pages(addr,3641ULL << (level_shift - 3), levels - 1);365366return -ENOMEM;367}368369void pnv_pci_unlink_table_and_group(struct iommu_table *tbl,370struct iommu_table_group *table_group)371{372long i;373bool found;374struct iommu_table_group_link *tgl;375376if (!tbl || !table_group)377return;378379/* Remove link to a group from table's list of attached groups */380found = false;381382rcu_read_lock();383list_for_each_entry_rcu(tgl, &tbl->it_group_list, next) {384if (tgl->table_group == table_group) {385list_del_rcu(&tgl->next);386kfree_rcu(tgl, rcu);387found = true;388break;389}390}391rcu_read_unlock();392393if (WARN_ON(!found))394return;395396/* Clean a pointer to iommu_table in iommu_table_group::tables[] */397found = false;398for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i) {399if (table_group->tables[i] == tbl) {400iommu_tce_table_put(tbl);401table_group->tables[i] = NULL;402found = true;403break;404}405}406WARN_ON(!found);407}408409long pnv_pci_link_table_and_group(int node, int num,410struct iommu_table *tbl,411struct iommu_table_group *table_group)412{413struct iommu_table_group_link *tgl = NULL;414415if (WARN_ON(!tbl || !table_group))416return -EINVAL;417418tgl = kzalloc_node(sizeof(struct iommu_table_group_link), GFP_KERNEL,419node);420if (!tgl)421return -ENOMEM;422423tgl->table_group = table_group;424list_add_rcu(&tgl->next, &tbl->it_group_list);425426table_group->tables[num] = iommu_tce_table_get(tbl);427428return 0;429}430431432