···200200 (((address) | ((pagesize) - 1)) & \201201 (~(pagesize >> 1)) & PM_ADDR_MASK)202202203203+/*204204+ * Takes a PTE value with mode=0x07 and returns the page size it maps205205+ */206206+#define PTE_PAGE_SIZE(pte) \207207+ (1ULL << (1 + ffz(((pte) | 0xfffULL))))208208+203209#define IOMMU_PTE_P (1ULL << 0)204210#define IOMMU_PTE_TV (1ULL << 1)205211#define IOMMU_PTE_U (1ULL << 59)
+72-18
arch/x86/kernel/amd_iommu.c
···776776 * This function checks if there is a PTE for a given dma address. If777777 * there is one, it returns the pointer to it.778778 */779779-static u64 *fetch_pte(struct protection_domain *domain,780780- unsigned long address, int map_size)779779+static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)781780{782781 int level;783782 u64 *pte;784783785785- level = domain->mode - 1;786786- pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];784784+ if (address > PM_LEVEL_SIZE(domain->mode))785785+ return NULL;787786788788- while (level > map_size) {787787+ level = domain->mode - 1;788788+ pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];789789+790790+ while (level > 0) {791791+792792+ /* Not Present */789793 if (!IOMMU_PTE_PRESENT(*pte))794794+ return NULL;795795+796796+ /* Large PTE */797797+ if (PM_PTE_LEVEL(*pte) == 0x07) {798798+ unsigned long pte_mask, __pte;799799+800800+ /*801801+ * If we have a series of large PTEs, make802802+ * sure to return a pointer to the first one.803803+ */804804+ pte_mask = PTE_PAGE_SIZE(*pte);805805+ pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);806806+ __pte = ((unsigned long)pte) & pte_mask;807807+808808+ return (u64 *)__pte;809809+ }810810+811811+ /* No level skipping support yet */812812+ if (PM_PTE_LEVEL(*pte) != level)790813 return NULL;791814792815 level -= 1;793816817817+ /* Walk to the next level */794818 pte = IOMMU_PTE_PAGE(*pte);795819 pte = &pte[PM_LEVEL_INDEX(level, address)];796796-797797- if ((PM_PTE_LEVEL(*pte) == 0) && level != map_size) {798798- pte = NULL;799799- break;800800- }801820 }802821803822 return pte;···869850 return 0;870851}871852872872-static void iommu_unmap_page(struct protection_domain *dom,873873- unsigned long bus_addr, int map_size)853853+static unsigned long iommu_unmap_page(struct protection_domain *dom,854854+ unsigned long bus_addr,855855+ unsigned long page_size)874856{875875- u64 *pte = fetch_pte(dom, bus_addr, map_size);857857+ unsigned long long unmap_size, unmapped;858858+ u64 *pte;876859877877- if (pte)878878- *pte = 0;860860+ BUG_ON(!is_power_of_2(page_size));861861+862862+ unmapped = 0;863863+864864+ while (unmapped < page_size) {865865+866866+ pte = fetch_pte(dom, bus_addr);867867+868868+ if (!pte) {869869+ /*870870+ * No PTE for this address871871+ * move forward in 4kb steps872872+ */873873+ unmap_size = PAGE_SIZE;874874+ } else if (PM_PTE_LEVEL(*pte) == 0) {875875+ /* 4kb PTE found for this address */876876+ unmap_size = PAGE_SIZE;877877+ *pte = 0ULL;878878+ } else {879879+ int count, i;880880+881881+ /* Large PTE found which maps this address */882882+ unmap_size = PTE_PAGE_SIZE(*pte);883883+ count = PAGE_SIZE_PTE_COUNT(unmap_size);884884+ for (i = 0; i < count; i++)885885+ pte[i] = 0ULL;886886+ }887887+888888+ bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;889889+ unmapped += unmap_size;890890+ }891891+892892+ BUG_ON(!is_power_of_2(unmapped));893893+894894+ return unmapped;879895}880896881897/*···11081054 for (i = dma_dom->aperture[index]->offset;11091055 i < dma_dom->aperture_size;11101056 i += PAGE_SIZE) {11111111- u64 *pte = fetch_pte(&dma_dom->domain, i, PM_MAP_4k);10571057+ u64 *pte = fetch_pte(&dma_dom->domain, i);11121058 if (!pte || !IOMMU_PTE_PRESENT(*pte))11131059 continue;11141060···25452491 iova &= PAGE_MASK;2546249225472493 for (i = 0; i < npages; ++i) {25482548- iommu_unmap_page(domain, iova, PM_MAP_4k);24942494+ iommu_unmap_page(domain, iova, PAGE_SIZE);25492495 iova += PAGE_SIZE;25502496 }25512497···25602506 phys_addr_t paddr;25612507 u64 *pte;2562250825632563- pte = fetch_pte(domain, iova, PM_MAP_4k);25092509+ pte = fetch_pte(domain, iova);2564251025652511 if (!pte || !IOMMU_PTE_PRESENT(*pte))25662512 return 0;