···22202220 forcing Dual Address Cycle for PCI cards supporting22212221 greater than 32-bit addressing.2222222222232223- iommu.strict= [ARM64, X86] Configure TLB invalidation behaviour22232223+ iommu.strict= [ARM64, X86, S390] Configure TLB invalidation behaviour22242224 Format: { "0" | "1" }22252225 0 - Lazy mode.22262226 Request that DMA unmap operations use deferred···56115611 s390_iommu= [HW,S390]56125612 Set s390 IOTLB flushing mode56135613 strict56145614- With strict flushing every unmap operation will result in56155615- an IOTLB flush. Default is lazy flushing before reuse,56165616- which is faster.56145614+ With strict flushing every unmap operation will result56155615+ in an IOTLB flush. Default is lazy flushing before56165616+ reuse, which is faster. Deprecated, equivalent to56175617+ iommu.strict=1.5617561856185619 s390_iommu_aperture= [KNL,S390]56195620 Specifies the size of the per device DMA address space
···11-// SPDX-License-Identifier: GPL-2.022-/*33- * Copyright IBM Corp. 201244- *55- * Author(s):66- * Jan Glauber <jang@linux.vnet.ibm.com>77- */88-99-#include <linux/kernel.h>1010-#include <linux/slab.h>1111-#include <linux/export.h>1212-#include <linux/iommu-helper.h>1313-#include <linux/dma-map-ops.h>1414-#include <linux/vmalloc.h>1515-#include <linux/pci.h>1616-#include <asm/pci_dma.h>1717-1818-static struct kmem_cache *dma_region_table_cache;1919-static struct kmem_cache *dma_page_table_cache;2020-static int s390_iommu_strict;2121-static u64 s390_iommu_aperture;2222-static u32 s390_iommu_aperture_factor = 1;2323-2424-static int zpci_refresh_global(struct zpci_dev *zdev)2525-{2626- return zpci_refresh_trans((u64) zdev->fh << 32, zdev->start_dma,2727- zdev->iommu_pages * PAGE_SIZE);2828-}2929-3030-unsigned long *dma_alloc_cpu_table(gfp_t gfp)3131-{3232- unsigned long *table, *entry;3333-3434- table = kmem_cache_alloc(dma_region_table_cache, gfp);3535- if (!table)3636- return NULL;3737-3838- for (entry = table; entry < table + ZPCI_TABLE_ENTRIES; entry++)3939- *entry = ZPCI_TABLE_INVALID;4040- return table;4141-}4242-4343-static void dma_free_cpu_table(void *table)4444-{4545- kmem_cache_free(dma_region_table_cache, table);4646-}4747-4848-static unsigned long *dma_alloc_page_table(gfp_t gfp)4949-{5050- unsigned long *table, *entry;5151-5252- table = kmem_cache_alloc(dma_page_table_cache, gfp);5353- if (!table)5454- return NULL;5555-5656- for (entry = table; entry < table + ZPCI_PT_ENTRIES; entry++)5757- *entry = ZPCI_PTE_INVALID;5858- return table;5959-}6060-6161-static void dma_free_page_table(void *table)6262-{6363- kmem_cache_free(dma_page_table_cache, table);6464-}6565-6666-static unsigned long *dma_get_seg_table_origin(unsigned long *rtep, gfp_t gfp)6767-{6868- unsigned long old_rte, rte;6969- unsigned long *sto;7070-7171- rte = READ_ONCE(*rtep);7272- if (reg_entry_isvalid(rte)) {7373- sto = get_rt_sto(rte);7474- } else {7575- sto = dma_alloc_cpu_table(gfp);7676- if (!sto)7777- return NULL;7878-7979- set_rt_sto(&rte, virt_to_phys(sto));8080- validate_rt_entry(&rte);8181- entry_clr_protected(&rte);8282-8383- old_rte = cmpxchg(rtep, ZPCI_TABLE_INVALID, rte);8484- if (old_rte != ZPCI_TABLE_INVALID) {8585- /* Somone else was faster, use theirs */8686- dma_free_cpu_table(sto);8787- sto = get_rt_sto(old_rte);8888- }8989- }9090- return sto;9191-}9292-9393-static unsigned long *dma_get_page_table_origin(unsigned long *step, gfp_t gfp)9494-{9595- unsigned long old_ste, ste;9696- unsigned long *pto;9797-9898- ste = READ_ONCE(*step);9999- if (reg_entry_isvalid(ste)) {100100- pto = get_st_pto(ste);101101- } else {102102- pto = dma_alloc_page_table(gfp);103103- if (!pto)104104- return NULL;105105- set_st_pto(&ste, virt_to_phys(pto));106106- validate_st_entry(&ste);107107- entry_clr_protected(&ste);108108-109109- old_ste = cmpxchg(step, ZPCI_TABLE_INVALID, ste);110110- if (old_ste != ZPCI_TABLE_INVALID) {111111- /* Somone else was faster, use theirs */112112- dma_free_page_table(pto);113113- pto = get_st_pto(old_ste);114114- }115115- }116116- return pto;117117-}118118-119119-unsigned long *dma_walk_cpu_trans(unsigned long *rto, dma_addr_t dma_addr,120120- gfp_t gfp)121121-{122122- unsigned long *sto, *pto;123123- unsigned int rtx, sx, px;124124-125125- rtx = calc_rtx(dma_addr);126126- sto = dma_get_seg_table_origin(&rto[rtx], gfp);127127- if (!sto)128128- return NULL;129129-130130- sx = calc_sx(dma_addr);131131- pto = dma_get_page_table_origin(&sto[sx], gfp);132132- if (!pto)133133- return NULL;134134-135135- px = calc_px(dma_addr);136136- return &pto[px];137137-}138138-139139-void dma_update_cpu_trans(unsigned long *ptep, phys_addr_t page_addr, int flags)140140-{141141- unsigned long pte;142142-143143- pte = READ_ONCE(*ptep);144144- if (flags & ZPCI_PTE_INVALID) {145145- invalidate_pt_entry(&pte);146146- } else {147147- set_pt_pfaa(&pte, page_addr);148148- validate_pt_entry(&pte);149149- }150150-151151- if (flags & ZPCI_TABLE_PROTECTED)152152- entry_set_protected(&pte);153153- else154154- entry_clr_protected(&pte);155155-156156- xchg(ptep, pte);157157-}158158-159159-static int __dma_update_trans(struct zpci_dev *zdev, phys_addr_t pa,160160- dma_addr_t dma_addr, size_t size, int flags)161161-{162162- unsigned int nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;163163- phys_addr_t page_addr = (pa & PAGE_MASK);164164- unsigned long *entry;165165- int i, rc = 0;166166-167167- if (!nr_pages)168168- return -EINVAL;169169-170170- if (!zdev->dma_table)171171- return -EINVAL;172172-173173- for (i = 0; i < nr_pages; i++) {174174- entry = dma_walk_cpu_trans(zdev->dma_table, dma_addr,175175- GFP_ATOMIC);176176- if (!entry) {177177- rc = -ENOMEM;178178- goto undo_cpu_trans;179179- }180180- dma_update_cpu_trans(entry, page_addr, flags);181181- page_addr += PAGE_SIZE;182182- dma_addr += PAGE_SIZE;183183- }184184-185185-undo_cpu_trans:186186- if (rc && ((flags & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID)) {187187- flags = ZPCI_PTE_INVALID;188188- while (i-- > 0) {189189- page_addr -= PAGE_SIZE;190190- dma_addr -= PAGE_SIZE;191191- entry = dma_walk_cpu_trans(zdev->dma_table, dma_addr,192192- GFP_ATOMIC);193193- if (!entry)194194- break;195195- dma_update_cpu_trans(entry, page_addr, flags);196196- }197197- }198198- return rc;199199-}200200-201201-static int __dma_purge_tlb(struct zpci_dev *zdev, dma_addr_t dma_addr,202202- size_t size, int flags)203203-{204204- unsigned long irqflags;205205- int ret;206206-207207- /*208208- * With zdev->tlb_refresh == 0, rpcit is not required to establish new209209- * translations when previously invalid translation-table entries are210210- * validated. With lazy unmap, rpcit is skipped for previously valid211211- * entries, but a global rpcit is then required before any address can212212- * be re-used, i.e. after each iommu bitmap wrap-around.213213- */214214- if ((flags & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID) {215215- if (!zdev->tlb_refresh)216216- return 0;217217- } else {218218- if (!s390_iommu_strict)219219- return 0;220220- }221221-222222- ret = zpci_refresh_trans((u64) zdev->fh << 32, dma_addr,223223- PAGE_ALIGN(size));224224- if (ret == -ENOMEM && !s390_iommu_strict) {225225- /* enable the hypervisor to free some resources */226226- if (zpci_refresh_global(zdev))227227- goto out;228228-229229- spin_lock_irqsave(&zdev->iommu_bitmap_lock, irqflags);230230- bitmap_andnot(zdev->iommu_bitmap, zdev->iommu_bitmap,231231- zdev->lazy_bitmap, zdev->iommu_pages);232232- bitmap_zero(zdev->lazy_bitmap, zdev->iommu_pages);233233- spin_unlock_irqrestore(&zdev->iommu_bitmap_lock, irqflags);234234- ret = 0;235235- }236236-out:237237- return ret;238238-}239239-240240-static int dma_update_trans(struct zpci_dev *zdev, phys_addr_t pa,241241- dma_addr_t dma_addr, size_t size, int flags)242242-{243243- int rc;244244-245245- rc = __dma_update_trans(zdev, pa, dma_addr, size, flags);246246- if (rc)247247- return rc;248248-249249- rc = __dma_purge_tlb(zdev, dma_addr, size, flags);250250- if (rc && ((flags & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID))251251- __dma_update_trans(zdev, pa, dma_addr, size, ZPCI_PTE_INVALID);252252-253253- return rc;254254-}255255-256256-void dma_free_seg_table(unsigned long entry)257257-{258258- unsigned long *sto = get_rt_sto(entry);259259- int sx;260260-261261- for (sx = 0; sx < ZPCI_TABLE_ENTRIES; sx++)262262- if (reg_entry_isvalid(sto[sx]))263263- dma_free_page_table(get_st_pto(sto[sx]));264264-265265- dma_free_cpu_table(sto);266266-}267267-268268-void dma_cleanup_tables(unsigned long *table)269269-{270270- int rtx;271271-272272- if (!table)273273- return;274274-275275- for (rtx = 0; rtx < ZPCI_TABLE_ENTRIES; rtx++)276276- if (reg_entry_isvalid(table[rtx]))277277- dma_free_seg_table(table[rtx]);278278-279279- dma_free_cpu_table(table);280280-}281281-282282-static unsigned long __dma_alloc_iommu(struct device *dev,283283- unsigned long start, int size)284284-{285285- struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));286286-287287- return iommu_area_alloc(zdev->iommu_bitmap, zdev->iommu_pages,288288- start, size, zdev->start_dma >> PAGE_SHIFT,289289- dma_get_seg_boundary_nr_pages(dev, PAGE_SHIFT),290290- 0);291291-}292292-293293-static dma_addr_t dma_alloc_address(struct device *dev, int size)294294-{295295- struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));296296- unsigned long offset, flags;297297-298298- spin_lock_irqsave(&zdev->iommu_bitmap_lock, flags);299299- offset = __dma_alloc_iommu(dev, zdev->next_bit, size);300300- if (offset == -1) {301301- if (!s390_iommu_strict) {302302- /* global flush before DMA addresses are reused */303303- if (zpci_refresh_global(zdev))304304- goto out_error;305305-306306- bitmap_andnot(zdev->iommu_bitmap, zdev->iommu_bitmap,307307- zdev->lazy_bitmap, zdev->iommu_pages);308308- bitmap_zero(zdev->lazy_bitmap, zdev->iommu_pages);309309- }310310- /* wrap-around */311311- offset = __dma_alloc_iommu(dev, 0, size);312312- if (offset == -1)313313- goto out_error;314314- }315315- zdev->next_bit = offset + size;316316- spin_unlock_irqrestore(&zdev->iommu_bitmap_lock, flags);317317-318318- return zdev->start_dma + offset * PAGE_SIZE;319319-320320-out_error:321321- spin_unlock_irqrestore(&zdev->iommu_bitmap_lock, flags);322322- return DMA_MAPPING_ERROR;323323-}324324-325325-static void dma_free_address(struct device *dev, dma_addr_t dma_addr, int size)326326-{327327- struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));328328- unsigned long flags, offset;329329-330330- offset = (dma_addr - zdev->start_dma) >> PAGE_SHIFT;331331-332332- spin_lock_irqsave(&zdev->iommu_bitmap_lock, flags);333333- if (!zdev->iommu_bitmap)334334- goto out;335335-336336- if (s390_iommu_strict)337337- bitmap_clear(zdev->iommu_bitmap, offset, size);338338- else339339- bitmap_set(zdev->lazy_bitmap, offset, size);340340-341341-out:342342- spin_unlock_irqrestore(&zdev->iommu_bitmap_lock, flags);343343-}344344-345345-static inline void zpci_err_dma(unsigned long rc, unsigned long addr)346346-{347347- struct {348348- unsigned long rc;349349- unsigned long addr;350350- } __packed data = {rc, addr};351351-352352- zpci_err_hex(&data, sizeof(data));353353-}354354-355355-static dma_addr_t s390_dma_map_pages(struct device *dev, struct page *page,356356- unsigned long offset, size_t size,357357- enum dma_data_direction direction,358358- unsigned long attrs)359359-{360360- struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));361361- unsigned long pa = page_to_phys(page) + offset;362362- int flags = ZPCI_PTE_VALID;363363- unsigned long nr_pages;364364- dma_addr_t dma_addr;365365- int ret;366366-367367- /* This rounds up number of pages based on size and offset */368368- nr_pages = iommu_num_pages(pa, size, PAGE_SIZE);369369- dma_addr = dma_alloc_address(dev, nr_pages);370370- if (dma_addr == DMA_MAPPING_ERROR) {371371- ret = -ENOSPC;372372- goto out_err;373373- }374374-375375- /* Use rounded up size */376376- size = nr_pages * PAGE_SIZE;377377-378378- if (direction == DMA_NONE || direction == DMA_TO_DEVICE)379379- flags |= ZPCI_TABLE_PROTECTED;380380-381381- ret = dma_update_trans(zdev, pa, dma_addr, size, flags);382382- if (ret)383383- goto out_free;384384-385385- atomic64_add(nr_pages, &zdev->mapped_pages);386386- return dma_addr + (offset & ~PAGE_MASK);387387-388388-out_free:389389- dma_free_address(dev, dma_addr, nr_pages);390390-out_err:391391- zpci_err("map error:\n");392392- zpci_err_dma(ret, pa);393393- return DMA_MAPPING_ERROR;394394-}395395-396396-static void s390_dma_unmap_pages(struct device *dev, dma_addr_t dma_addr,397397- size_t size, enum dma_data_direction direction,398398- unsigned long attrs)399399-{400400- struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));401401- int npages, ret;402402-403403- npages = iommu_num_pages(dma_addr, size, PAGE_SIZE);404404- dma_addr = dma_addr & PAGE_MASK;405405- ret = dma_update_trans(zdev, 0, dma_addr, npages * PAGE_SIZE,406406- ZPCI_PTE_INVALID);407407- if (ret) {408408- zpci_err("unmap error:\n");409409- zpci_err_dma(ret, dma_addr);410410- return;411411- }412412-413413- atomic64_add(npages, &zdev->unmapped_pages);414414- dma_free_address(dev, dma_addr, npages);415415-}416416-417417-static void *s390_dma_alloc(struct device *dev, size_t size,418418- dma_addr_t *dma_handle, gfp_t flag,419419- unsigned long attrs)420420-{421421- struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));422422- struct page *page;423423- phys_addr_t pa;424424- dma_addr_t map;425425-426426- size = PAGE_ALIGN(size);427427- page = alloc_pages(flag | __GFP_ZERO, get_order(size));428428- if (!page)429429- return NULL;430430-431431- pa = page_to_phys(page);432432- map = s390_dma_map_pages(dev, page, 0, size, DMA_BIDIRECTIONAL, 0);433433- if (dma_mapping_error(dev, map)) {434434- __free_pages(page, get_order(size));435435- return NULL;436436- }437437-438438- atomic64_add(size / PAGE_SIZE, &zdev->allocated_pages);439439- if (dma_handle)440440- *dma_handle = map;441441- return phys_to_virt(pa);442442-}443443-444444-static void s390_dma_free(struct device *dev, size_t size,445445- void *vaddr, dma_addr_t dma_handle,446446- unsigned long attrs)447447-{448448- struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));449449-450450- size = PAGE_ALIGN(size);451451- atomic64_sub(size / PAGE_SIZE, &zdev->allocated_pages);452452- s390_dma_unmap_pages(dev, dma_handle, size, DMA_BIDIRECTIONAL, 0);453453- free_pages((unsigned long)vaddr, get_order(size));454454-}455455-456456-/* Map a segment into a contiguous dma address area */457457-static int __s390_dma_map_sg(struct device *dev, struct scatterlist *sg,458458- size_t size, dma_addr_t *handle,459459- enum dma_data_direction dir)460460-{461461- unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;462462- struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));463463- dma_addr_t dma_addr_base, dma_addr;464464- int flags = ZPCI_PTE_VALID;465465- struct scatterlist *s;466466- phys_addr_t pa = 0;467467- int ret;468468-469469- dma_addr_base = dma_alloc_address(dev, nr_pages);470470- if (dma_addr_base == DMA_MAPPING_ERROR)471471- return -ENOMEM;472472-473473- dma_addr = dma_addr_base;474474- if (dir == DMA_NONE || dir == DMA_TO_DEVICE)475475- flags |= ZPCI_TABLE_PROTECTED;476476-477477- for (s = sg; dma_addr < dma_addr_base + size; s = sg_next(s)) {478478- pa = page_to_phys(sg_page(s));479479- ret = __dma_update_trans(zdev, pa, dma_addr,480480- s->offset + s->length, flags);481481- if (ret)482482- goto unmap;483483-484484- dma_addr += s->offset + s->length;485485- }486486- ret = __dma_purge_tlb(zdev, dma_addr_base, size, flags);487487- if (ret)488488- goto unmap;489489-490490- *handle = dma_addr_base;491491- atomic64_add(nr_pages, &zdev->mapped_pages);492492-493493- return ret;494494-495495-unmap:496496- dma_update_trans(zdev, 0, dma_addr_base, dma_addr - dma_addr_base,497497- ZPCI_PTE_INVALID);498498- dma_free_address(dev, dma_addr_base, nr_pages);499499- zpci_err("map error:\n");500500- zpci_err_dma(ret, pa);501501- return ret;502502-}503503-504504-static int s390_dma_map_sg(struct device *dev, struct scatterlist *sg,505505- int nr_elements, enum dma_data_direction dir,506506- unsigned long attrs)507507-{508508- struct scatterlist *s = sg, *start = sg, *dma = sg;509509- unsigned int max = dma_get_max_seg_size(dev);510510- unsigned int size = s->offset + s->length;511511- unsigned int offset = s->offset;512512- int count = 0, i, ret;513513-514514- for (i = 1; i < nr_elements; i++) {515515- s = sg_next(s);516516-517517- s->dma_length = 0;518518-519519- if (s->offset || (size & ~PAGE_MASK) ||520520- size + s->length > max) {521521- ret = __s390_dma_map_sg(dev, start, size,522522- &dma->dma_address, dir);523523- if (ret)524524- goto unmap;525525-526526- dma->dma_address += offset;527527- dma->dma_length = size - offset;528528-529529- size = offset = s->offset;530530- start = s;531531- dma = sg_next(dma);532532- count++;533533- }534534- size += s->length;535535- }536536- ret = __s390_dma_map_sg(dev, start, size, &dma->dma_address, dir);537537- if (ret)538538- goto unmap;539539-540540- dma->dma_address += offset;541541- dma->dma_length = size - offset;542542-543543- return count + 1;544544-unmap:545545- for_each_sg(sg, s, count, i)546546- s390_dma_unmap_pages(dev, sg_dma_address(s), sg_dma_len(s),547547- dir, attrs);548548-549549- return ret;550550-}551551-552552-static void s390_dma_unmap_sg(struct device *dev, struct scatterlist *sg,553553- int nr_elements, enum dma_data_direction dir,554554- unsigned long attrs)555555-{556556- struct scatterlist *s;557557- int i;558558-559559- for_each_sg(sg, s, nr_elements, i) {560560- if (s->dma_length)561561- s390_dma_unmap_pages(dev, s->dma_address, s->dma_length,562562- dir, attrs);563563- s->dma_address = 0;564564- s->dma_length = 0;565565- }566566-}567567-568568-static unsigned long *bitmap_vzalloc(size_t bits, gfp_t flags)569569-{570570- size_t n = BITS_TO_LONGS(bits);571571- size_t bytes;572572-573573- if (unlikely(check_mul_overflow(n, sizeof(unsigned long), &bytes)))574574- return NULL;575575-576576- return vzalloc(bytes);577577-}578578-579579-int zpci_dma_init_device(struct zpci_dev *zdev)580580-{581581- u8 status;582582- int rc;583583-584584- /*585585- * At this point, if the device is part of an IOMMU domain, this would586586- * be a strong hint towards a bug in the IOMMU API (common) code and/or587587- * simultaneous access via IOMMU and DMA API. So let's issue a warning.588588- */589589- WARN_ON(zdev->s390_domain);590590-591591- spin_lock_init(&zdev->iommu_bitmap_lock);592592-593593- zdev->dma_table = dma_alloc_cpu_table(GFP_KERNEL);594594- if (!zdev->dma_table) {595595- rc = -ENOMEM;596596- goto out;597597- }598598-599599- /*600600- * Restrict the iommu bitmap size to the minimum of the following:601601- * - s390_iommu_aperture which defaults to high_memory602602- * - 3-level pagetable address limit minus start_dma offset603603- * - DMA address range allowed by the hardware (clp query pci fn)604604- *605605- * Also set zdev->end_dma to the actual end address of the usable606606- * range, instead of the theoretical maximum as reported by hardware.607607- *608608- * This limits the number of concurrently usable DMA mappings since609609- * for each DMA mapped memory address we need a DMA address including610610- * extra DMA addresses for multiple mappings of the same memory address.611611- */612612- zdev->start_dma = PAGE_ALIGN(zdev->start_dma);613613- zdev->iommu_size = min3(s390_iommu_aperture,614614- ZPCI_TABLE_SIZE_RT - zdev->start_dma,615615- zdev->end_dma - zdev->start_dma + 1);616616- zdev->end_dma = zdev->start_dma + zdev->iommu_size - 1;617617- zdev->iommu_pages = zdev->iommu_size >> PAGE_SHIFT;618618- zdev->iommu_bitmap = bitmap_vzalloc(zdev->iommu_pages, GFP_KERNEL);619619- if (!zdev->iommu_bitmap) {620620- rc = -ENOMEM;621621- goto free_dma_table;622622- }623623- if (!s390_iommu_strict) {624624- zdev->lazy_bitmap = bitmap_vzalloc(zdev->iommu_pages, GFP_KERNEL);625625- if (!zdev->lazy_bitmap) {626626- rc = -ENOMEM;627627- goto free_bitmap;628628- }629629-630630- }631631- if (zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma,632632- virt_to_phys(zdev->dma_table), &status)) {633633- rc = -EIO;634634- goto free_bitmap;635635- }636636-637637- return 0;638638-free_bitmap:639639- vfree(zdev->iommu_bitmap);640640- zdev->iommu_bitmap = NULL;641641- vfree(zdev->lazy_bitmap);642642- zdev->lazy_bitmap = NULL;643643-free_dma_table:644644- dma_free_cpu_table(zdev->dma_table);645645- zdev->dma_table = NULL;646646-out:647647- return rc;648648-}649649-650650-int zpci_dma_exit_device(struct zpci_dev *zdev)651651-{652652- int cc = 0;653653-654654- /*655655- * At this point, if the device is part of an IOMMU domain, this would656656- * be a strong hint towards a bug in the IOMMU API (common) code and/or657657- * simultaneous access via IOMMU and DMA API. So let's issue a warning.658658- */659659- WARN_ON(zdev->s390_domain);660660- if (zdev_enabled(zdev))661661- cc = zpci_unregister_ioat(zdev, 0);662662- /*663663- * cc == 3 indicates the function is gone already. This can happen664664- * if the function was deconfigured/disabled suddenly and we have not665665- * received a new handle yet.666666- */667667- if (cc && cc != 3)668668- return -EIO;669669-670670- dma_cleanup_tables(zdev->dma_table);671671- zdev->dma_table = NULL;672672- vfree(zdev->iommu_bitmap);673673- zdev->iommu_bitmap = NULL;674674- vfree(zdev->lazy_bitmap);675675- zdev->lazy_bitmap = NULL;676676- zdev->next_bit = 0;677677- return 0;678678-}679679-680680-static int __init dma_alloc_cpu_table_caches(void)681681-{682682- dma_region_table_cache = kmem_cache_create("PCI_DMA_region_tables",683683- ZPCI_TABLE_SIZE, ZPCI_TABLE_ALIGN,684684- 0, NULL);685685- if (!dma_region_table_cache)686686- return -ENOMEM;687687-688688- dma_page_table_cache = kmem_cache_create("PCI_DMA_page_tables",689689- ZPCI_PT_SIZE, ZPCI_PT_ALIGN,690690- 0, NULL);691691- if (!dma_page_table_cache) {692692- kmem_cache_destroy(dma_region_table_cache);693693- return -ENOMEM;694694- }695695- return 0;696696-}697697-698698-int __init zpci_dma_init(void)699699-{700700- s390_iommu_aperture = (u64)virt_to_phys(high_memory);701701- if (!s390_iommu_aperture_factor)702702- s390_iommu_aperture = ULONG_MAX;703703- else704704- s390_iommu_aperture *= s390_iommu_aperture_factor;705705-706706- return dma_alloc_cpu_table_caches();707707-}708708-709709-void zpci_dma_exit(void)710710-{711711- kmem_cache_destroy(dma_page_table_cache);712712- kmem_cache_destroy(dma_region_table_cache);713713-}714714-715715-const struct dma_map_ops s390_pci_dma_ops = {716716- .alloc = s390_dma_alloc,717717- .free = s390_dma_free,718718- .map_sg = s390_dma_map_sg,719719- .unmap_sg = s390_dma_unmap_sg,720720- .map_page = s390_dma_map_pages,721721- .unmap_page = s390_dma_unmap_pages,722722- .mmap = dma_common_mmap,723723- .get_sgtable = dma_common_get_sgtable,724724- .alloc_pages = dma_common_alloc_pages,725725- .free_pages = dma_common_free_pages,726726- /* dma_supported is unconditionally true without a callback */727727-};728728-EXPORT_SYMBOL_GPL(s390_pci_dma_ops);729729-730730-static int __init s390_iommu_setup(char *str)731731-{732732- if (!strcmp(str, "strict"))733733- s390_iommu_strict = 1;734734- return 1;735735-}736736-737737-__setup("s390_iommu=", s390_iommu_setup);738738-739739-static int __init s390_iommu_aperture_setup(char *str)740740-{741741- if (kstrtou32(str, 10, &s390_iommu_aperture_factor))742742- s390_iommu_aperture_factor = 1;743743- return 1;744744-}745745-746746-__setup("s390_iommu_aperture=", s390_iommu_aperture_setup);
+11-6
arch/s390/pci/pci_event.c
···5959 }6060}61616262-static bool is_passed_through(struct zpci_dev *zdev)6262+static bool is_passed_through(struct pci_dev *pdev)6363{6464- return zdev->s390_domain;6464+ struct zpci_dev *zdev = to_zpci(pdev);6565+ bool ret;6666+6767+ mutex_lock(&zdev->kzdev_lock);6868+ ret = !!zdev->kzdev;6969+ mutex_unlock(&zdev->kzdev_lock);7070+7171+ return ret;6572}66736774static bool is_driver_supported(struct pci_driver *driver)···183176 }184177 pdev->error_state = pci_channel_io_frozen;185178186186- if (is_passed_through(to_zpci(pdev))) {179179+ if (is_passed_through(pdev)) {187180 pr_info("%s: Cannot be recovered in the host because it is a pass-through device\n",188181 pci_name(pdev));189182 goto out_unlock;···246239 * we will inject the error event and let the guest recover the device247240 * itself.248241 */249249- if (is_passed_through(to_zpci(pdev)))242242+ if (is_passed_through(pdev))250243 goto out;251244 driver = to_pci_driver(pdev->dev.driver);252245 if (driver && driver->err_handler && driver->err_handler->error_detected)···313306 /* Even though the device is already gone we still314307 * need to free zPCI resources as part of the disable.315308 */316316- if (zdev->dma_table)317317- zpci_dma_exit_device(zdev);318309 if (zdev_enabled(zdev))319310 zpci_disable_device(zdev);320311 zdev->state = ZPCI_FN_STATE_STANDBY;
+8-11
arch/s390/pci/pci_sysfs.c
···5656 struct pci_dev *pdev = to_pci_dev(dev);5757 struct zpci_dev *zdev = to_zpci(pdev);5858 int ret = 0;5959+ u8 status;59606061 /* Can't use device_remove_self() here as that would lead us to lock6162 * the pci_rescan_remove_lock while holding the device' kernfs lock.···8382 pci_lock_rescan_remove();8483 if (pci_dev_is_added(pdev)) {8584 pci_stop_and_remove_bus_device(pdev);8686- if (zdev->dma_table) {8787- ret = zpci_dma_exit_device(zdev);8888- if (ret)8989- goto out;9090- }9191-9285 if (zdev_enabled(zdev)) {9386 ret = zpci_disable_device(zdev);9487 /*···100105 ret = zpci_enable_device(zdev);101106 if (ret)102107 goto out;103103- ret = zpci_dma_init_device(zdev);104104- if (ret) {105105- zpci_disable_device(zdev);106106- goto out;108108+109109+ if (zdev->dma_table) {110110+ ret = zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma,111111+ virt_to_phys(zdev->dma_table), &status);112112+ if (ret)113113+ zpci_disable_device(zdev);107114 }108108- pci_rescan_bus(zdev->zbus->bus);109115 }110116out:117117+ pci_rescan_bus(zdev->zbus->bus);111118 pci_unlock_rescan_remove();112119 if (kn)113120 sysfs_unbreak_active_protection(kn);
+2-13
drivers/iommu/Kconfig
···9191choice9292 prompt "IOMMU default domain type"9393 depends on IOMMU_API9494- default IOMMU_DEFAULT_DMA_LAZY if X86 || IA649494+ default IOMMU_DEFAULT_DMA_LAZY if X86 || IA64 || S3909595 default IOMMU_DEFAULT_DMA_STRICT9696 help9797 Choose the type of IOMMU domain used to manage DMA API usage by···146146147147# IOMMU-agnostic DMA-mapping layer148148config IOMMU_DMA149149- def_bool ARM64 || IA64 || X86149149+ def_bool ARM64 || IA64 || X86 || S390150150 select DMA_OPS151151 select IOMMU_API152152 select IOMMU_IOVA···235235 select IOMMU_API236236 help237237 Support for the IOMMU introduced in the Allwinner H6 SoCs.238238-239239-config TEGRA_IOMMU_GART240240- bool "Tegra GART IOMMU Support"241241- depends on ARCH_TEGRA_2x_SOC242242- depends on TEGRA_MC243243- select IOMMU_API244244- help245245- Enables support for remapping discontiguous physical memory246246- shared with the operating system into contiguous I/O virtual247247- space through the GART (Graphics Address Relocation Table)248248- hardware included on Tegra SoCs.249238250239config TEGRA_IOMMU_SMMU251240 bool "NVIDIA Tegra SMMU Support"
···2222 your BIOS for an option to enable it or if you have an IVRS ACPI2323 table.24242525-config AMD_IOMMU_V22626- tristate "AMD IOMMU Version 2 driver"2727- depends on AMD_IOMMU2828- select MMU_NOTIFIER2929- help3030- This option enables support for the AMD IOMMUv2 features of the IOMMU3131- hardware. Select this option if you want to use devices that support3232- the PCI PRI and PASID interface.3333-3425config AMD_IOMMU_DEBUGFS3526 bool "Enable AMD IOMMU internals in DebugFS"3627 depends on AMD_IOMMU && IOMMU_DEBUGFS
···451451#define PD_IOMMUV2_MASK BIT(3) /* domain has gcr3 table */452452#define PD_GIOV_MASK BIT(4) /* domain enable GIOV support */453453454454+/* Timeout stuff */455455+#define LOOP_TIMEOUT 100000456456+#define MMIO_STATUS_TIMEOUT 2000000457457+454458extern bool amd_iommu_dump;455459#define DUMP_printk(format, arg...) \456460 do { \···509505#define APERTURE_RANGE_INDEX(a) ((a) >> APERTURE_RANGE_SHIFT)510506#define APERTURE_PAGE_INDEX(a) (((a) >> 21) & 0x3fULL)511507512512-/*513513- * This struct is used to pass information about514514- * incoming PPR faults around.515515- */516516-struct amd_iommu_fault {517517- u64 address; /* IO virtual address of the fault*/518518- u32 pasid; /* Address space identifier */519519- u32 sbdf; /* Originating PCI device id */520520- u16 tag; /* PPR tag */521521- u16 flags; /* Fault flags */522522-523523-};524524-525508526509struct amd_iommu;527510struct iommu_domain;···535544 struct io_pgtable iop;536545 int mode;537546 u64 *root;538538- atomic64_t pt_root; /* pgtable root and pgtable mode */539547 u64 *pgd; /* v2 pgtable pgd pointer */540548};541549···666676 /* Extended features 2 */667677 u64 features2;668678669669- /* IOMMUv2 */670670- bool is_iommu_v2;671671-672679 /* PCI device id of the IOMMU device */673680 u16 devid;674681···786799 bool cmd_line;787800};788801802802+#define AMD_IOMMU_DEVICE_FLAG_ATS_SUP 0x1 /* ATS feature supported */803803+#define AMD_IOMMU_DEVICE_FLAG_PRI_SUP 0x2 /* PRI feature supported */804804+#define AMD_IOMMU_DEVICE_FLAG_PASID_SUP 0x4 /* PASID context supported */805805+/* Device may request execution on memory pages */806806+#define AMD_IOMMU_DEVICE_FLAG_EXEC_SUP 0x8807807+/* Device may request super-user privileges */808808+#define AMD_IOMMU_DEVICE_FLAG_PRIV_SUP 0x10809809+789810/*790811 * This struct contains device specific data for the IOMMU791812 */···806811 struct protection_domain *domain; /* Domain the device is bound to */807812 struct device *dev;808813 u16 devid; /* PCI Device ID */809809- bool iommu_v2; /* Device can make use of IOMMUv2 */810810- struct {811811- bool enabled;812812- int qdep;813813- } ats; /* ATS state */814814- bool pri_tlp; /* PASID TLB required for814814+815815+ u32 flags; /* Holds AMD_IOMMU_DEVICE_FLAG_<*> */816816+ int ats_qdep;817817+ u8 ats_enabled :1; /* ATS state */818818+ u8 pri_enabled :1; /* PRI state */819819+ u8 pasid_enabled:1; /* PASID state */820820+ u8 pri_tlp :1; /* PASID TLB required for815821 PPR completions */822822+ u8 ppr :1; /* Enable device PPR support */816823 bool use_vapic; /* Enable device to use vapic mode */817824 bool defer_attach;818825···881884/* allocation bitmap for domain ids */882885extern unsigned long *amd_iommu_pd_alloc_bitmap;883886884884-/* Smallest max PASID supported by any IOMMU in the system */885885-extern u32 amd_iommu_max_pasid;886886-887887-extern bool amd_iommu_v2_present;888888-889887extern bool amd_iommu_force_isolation;890888891889/* Max levels of glxval supported */892890extern int amd_iommu_max_glx_val;891891+892892+/* Global EFR and EFR2 registers */893893+extern u64 amd_iommu_efr;894894+extern u64 amd_iommu_efr2;893895894896/*895897 * This function flushes all internal caches of
+43-78
drivers/iommu/amd/init.c
···8383#define ACPI_DEVFLAG_LINT1 0x808484#define ACPI_DEVFLAG_ATSDIS 0x1000000085858686-#define LOOP_TIMEOUT 20000008787-8886#define IVRS_GET_SBDF_ID(seg, bus, dev, fn) (((seg & 0xffff) << 16) | ((bus & 0xff) << 8) \8987 | ((dev & 0x1f) << 3) | (fn & 0x7))9088···185187bool amd_iommu_np_cache __read_mostly;186188bool amd_iommu_iotlb_sup __read_mostly = true;187189188188-u32 amd_iommu_max_pasid __read_mostly = ~0;189189-190190-bool amd_iommu_v2_present __read_mostly;191190static bool amd_iommu_pc_present __read_mostly;192191bool amdr_ivrs_remap_support __read_mostly;193192···267272 * Iterate through all the IOMMUs to get common EFR268273 * masks among all IOMMUs and warn if found inconsistency.269274 */270270-static void get_global_efr(void)275275+static __init void get_global_efr(void)271276{272277 struct amd_iommu *iommu;273278···297302 }298303299304 pr_info("Using global IVHD EFR:%#llx, EFR2:%#llx\n", amd_iommu_efr, amd_iommu_efr2);300300-}301301-302302-static bool check_feature_on_all_iommus(u64 mask)303303-{304304- return !!(amd_iommu_efr & mask);305305-}306306-307307-static inline int check_feature_gpt_level(void)308308-{309309- return ((amd_iommu_efr >> FEATURE_GATS_SHIFT) & FEATURE_GATS_MASK);310305}311306312307/*···384399 u64 start = iommu_virt_to_phys((void *)iommu->cmd_sem);385400 u64 entry = start & PM_ADDR_MASK;386401387387- if (!check_feature_on_all_iommus(FEATURE_SNP))402402+ if (!check_feature(FEATURE_SNP))388403 return;389404390405 /* Note:···854869 void *buf = (void *)__get_free_pages(gfp, order);855870856871 if (buf &&857857- check_feature_on_all_iommus(FEATURE_SNP) &&872872+ check_feature(FEATURE_SNP) &&858873 set_memory_4k((unsigned long)buf, (1 << order))) {859874 free_pages((unsigned long)buf, order);860875 buf = NULL;···970985 iommu_feature_enable(iommu, CONTROL_GAINT_EN);971986 iommu_feature_enable(iommu, CONTROL_GALOG_EN);972987973973- for (i = 0; i < LOOP_TIMEOUT; ++i) {988988+ for (i = 0; i < MMIO_STATUS_TIMEOUT; ++i) {974989 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);975990 if (status & (MMIO_STATUS_GALOG_RUN_MASK))976991 break;977992 udelay(10);978993 }979994980980- if (WARN_ON(i >= LOOP_TIMEOUT))995995+ if (WARN_ON(i >= MMIO_STATUS_TIMEOUT))981996 return -EINVAL;982997983998 return 0;···1033104810341049static void iommu_enable_gt(struct amd_iommu *iommu)10351050{10361036- if (!iommu_feature(iommu, FEATURE_GT))10511051+ if (!check_feature(FEATURE_GT))10371052 return;1038105310391054 iommu_feature_enable(iommu, CONTROL_GT_EN);···19721987 u64 val;19731988 struct pci_dev *pdev = iommu->dev;1974198919751975- if (!iommu_feature(iommu, FEATURE_PC))19901990+ if (!check_feature(FEATURE_PC))19761991 return;1977199219781993 amd_iommu_pc_present = true;···19992014 struct device_attribute *attr,20002015 char *buf)20012016{20022002- struct amd_iommu *iommu = dev_to_amd_iommu(dev);20032003- return sysfs_emit(buf, "%llx:%llx\n", iommu->features2, iommu->features);20172017+ return sysfs_emit(buf, "%llx:%llx\n", amd_iommu_efr, amd_iommu_efr2);20042018}20052019static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);20062020···20352051 features = readq(iommu->mmio_base + MMIO_EXT_FEATURES);20362052 features2 = readq(iommu->mmio_base + MMIO_EXT_FEATURES2);2037205320382038- if (!iommu->features) {20392039- iommu->features = features;20402040- iommu->features2 = features2;20542054+ if (!amd_iommu_efr) {20552055+ amd_iommu_efr = features;20562056+ amd_iommu_efr2 = features2;20412057 return;20422058 }20432059···20452061 * Sanity check and warn if EFR values from20462062 * IVHD and MMIO conflict.20472063 */20482048- if (features != iommu->features ||20492049- features2 != iommu->features2) {20642064+ if (features != amd_iommu_efr ||20652065+ features2 != amd_iommu_efr2) {20502066 pr_warn(FW_WARN20512067 "EFR mismatch. Use IVHD EFR (%#llx : %#llx), EFR2 (%#llx : %#llx).\n",20522052- features, iommu->features,20532053- features2, iommu->features2);20682068+ features, amd_iommu_efr,20692069+ features2, amd_iommu_efr2);20542070 }20552071}20562072···2076209220772093 late_iommu_features_init(iommu);2078209420792079- if (iommu_feature(iommu, FEATURE_GT)) {20952095+ if (check_feature(FEATURE_GT)) {20802096 int glxval;20812081- u32 max_pasid;20822097 u64 pasmax;2083209820842084- pasmax = iommu->features & FEATURE_PASID_MASK;20992099+ pasmax = amd_iommu_efr & FEATURE_PASID_MASK;20852100 pasmax >>= FEATURE_PASID_SHIFT;20862086- max_pasid = (1 << (pasmax + 1)) - 1;21012101+ iommu->iommu.max_pasids = (1 << (pasmax + 1)) - 1;2087210220882088- amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);21032103+ BUG_ON(iommu->iommu.max_pasids & ~PASID_MASK);2089210420902090- BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);20912091-20922092- glxval = iommu->features & FEATURE_GLXVAL_MASK;21052105+ glxval = amd_iommu_efr & FEATURE_GLXVAL_MASK;20932106 glxval >>= FEATURE_GLXVAL_SHIFT;2094210720952108 if (amd_iommu_max_glx_val == -1)···20952114 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);20962115 }2097211620982098- if (iommu_feature(iommu, FEATURE_GT) &&20992099- iommu_feature(iommu, FEATURE_PPR)) {21002100- iommu->is_iommu_v2 = true;21012101- amd_iommu_v2_present = true;21022102- }21032103-21042104- if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))21172117+ if (check_feature(FEATURE_PPR) && alloc_ppr_log(iommu))21052118 return -ENOMEM;2106211921072120 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE)) {···21072132 init_iommu_perf_ctr(iommu);2108213321092134 if (amd_iommu_pgtable == AMD_IOMMU_V2) {21102110- if (!iommu_feature(iommu, FEATURE_GIOSUP) ||21112111- !iommu_feature(iommu, FEATURE_GT)) {21352135+ if (!check_feature(FEATURE_GIOSUP) ||21362136+ !check_feature(FEATURE_GT)) {21122137 pr_warn("Cannot enable v2 page table for DMA-API. Fallback to v1.\n");21132113- amd_iommu_pgtable = AMD_IOMMU_V1;21142114- } else if (iommu_default_passthrough()) {21152115- pr_warn("V2 page table doesn't support passthrough mode. Fallback to v1.\n");21162138 amd_iommu_pgtable = AMD_IOMMU_V1;21172139 }21182140 }···2158218621592187static void print_iommu_info(void)21602188{21892189+ int i;21612190 static const char * const feat_str[] = {21622191 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",21632192 "IA", "GA", "HE", "PC"21642193 };21652165- struct amd_iommu *iommu;2166219421672167- for_each_iommu(iommu) {21682168- struct pci_dev *pdev = iommu->dev;21692169- int i;21952195+ if (amd_iommu_efr) {21962196+ pr_info("Extended features (%#llx, %#llx):", amd_iommu_efr, amd_iommu_efr2);2170219721712171- pci_info(pdev, "Found IOMMU cap 0x%x\n", iommu->cap_ptr);21722172-21732173- if (iommu->cap & (1 << IOMMU_CAP_EFR)) {21742174- pr_info("Extended features (%#llx, %#llx):", iommu->features, iommu->features2);21752175-21762176- for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {21772177- if (iommu_feature(iommu, (1ULL << i)))21782178- pr_cont(" %s", feat_str[i]);21792179- }21802180-21812181- if (iommu->features & FEATURE_GAM_VAPIC)21822182- pr_cont(" GA_vAPIC");21832183-21842184- if (iommu->features & FEATURE_SNP)21852185- pr_cont(" SNP");21862186-21872187- pr_cont("\n");21982198+ for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {21992199+ if (check_feature(1ULL << i))22002200+ pr_cont(" %s", feat_str[i]);21882201 }22022202+22032203+ if (check_feature(FEATURE_GAM_VAPIC))22042204+ pr_cont(" GA_vAPIC");22052205+22062206+ if (check_feature(FEATURE_SNP))22072207+ pr_cont(" SNP");22082208+22092209+ pr_cont("\n");21892210 }22112211+21902212 if (irq_remapping_enabled) {21912213 pr_info("Interrupt remapping enabled\n");21922214 if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)···28662900 * Need to set and poll check the GALOGRun bit to zero before28672901 * we can set/ modify GA Log registers safely.28682902 */28692869- for (i = 0; i < LOOP_TIMEOUT; ++i) {29032903+ for (i = 0; i < MMIO_STATUS_TIMEOUT; ++i) {28702904 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);28712905 if (!(status & MMIO_STATUS_GALOG_RUN_MASK))28722906 break;28732907 udelay(10);28742908 }2875290928762876- if (WARN_ON(i >= LOOP_TIMEOUT))29102910+ if (WARN_ON(i >= MMIO_STATUS_TIMEOUT))28772911 return;28782912 }2879291328802914 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&28812881- !check_feature_on_all_iommus(FEATURE_GAM_VAPIC)) {29152915+ !check_feature(FEATURE_GAM_VAPIC)) {28822916 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;28832917 return;28842918 }···36643698 * (i.e. EFR[SNPSup]=1), IOMMUv2 page table cannot be used without36653699 * setting up IOMMUv1 page table.36663700 */36673667- return amd_iommu_v2_present && !amd_iommu_snp_en;37013701+ return amd_iommu_gt_ppr_supported() && !amd_iommu_snp_en;36683702}36693669-EXPORT_SYMBOL(amd_iommu_v2_supported);3670370336713704struct amd_iommu *get_amd_iommu(unsigned int idx)36723705{···37893824 return -EINVAL;37903825 }3791382637923792- amd_iommu_snp_en = check_feature_on_all_iommus(FEATURE_SNP);38273827+ amd_iommu_snp_en = check_feature(FEATURE_SNP);37933828 if (!amd_iommu_snp_en)37943829 return -EINVAL;37953830
+4-4
drivers/iommu/amd/io_pgtable_v2.c
···363363 if (!(pdom->flags & PD_IOMMUV2_MASK))364364 return;365365366366- /*367367- * Make changes visible to IOMMUs. No need to clear gcr3 entry368368- * as gcr3 table is already freed.369369- */366366+ /* Clear gcr3 entry */367367+ amd_iommu_domain_clear_gcr3(&pdom->domain, 0);368368+369369+ /* Make changes visible to IOMMUs */370370 amd_iommu_domain_update(pdom);371371372372 /* Free page table */
+243-338
drivers/iommu/amd/iommu.c
···44444545#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))46464747-#define LOOP_TIMEOUT 1000004848-4947/* IO virtual address start page frame number */5048#define IOVA_START_PFN (1)5149#define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)···64666567const struct iommu_ops amd_iommu_ops;66686767-static ATOMIC_NOTIFIER_HEAD(ppr_notifier);6869int amd_iommu_max_glx_val = -1;69707071/*···7679struct kmem_cache *amd_iommu_irq_cache;77807881static void detach_device(struct device *dev);7979-static int domain_enable_v2(struct protection_domain *domain, int pasids);80828183/****************************************************************************8284 *···318322 return entry->group;319323}320324321321-static bool pci_iommuv2_capable(struct pci_dev *pdev)325325+static inline bool pdev_pasid_supported(struct iommu_dev_data *dev_data)322326{323323- static const int caps[] = {324324- PCI_EXT_CAP_ID_PRI,325325- PCI_EXT_CAP_ID_PASID,326326- };327327- int i, pos;327327+ return (dev_data->flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP);328328+}328329329329- if (!pci_ats_supported(pdev))330330- return false;330330+static u32 pdev_get_caps(struct pci_dev *pdev)331331+{332332+ int features;333333+ u32 flags = 0;331334332332- for (i = 0; i < 2; ++i) {333333- pos = pci_find_ext_capability(pdev, caps[i]);334334- if (pos == 0)335335- return false;335335+ if (pci_ats_supported(pdev))336336+ flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;337337+338338+ if (pci_pri_supported(pdev))339339+ flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;340340+341341+ features = pci_pasid_features(pdev);342342+ if (features >= 0) {343343+ flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;344344+345345+ if (features & PCI_PASID_CAP_EXEC)346346+ flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;347347+348348+ if (features & PCI_PASID_CAP_PRIV)349349+ flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;336350 }337351338338- return true;352352+ return flags;353353+}354354+355355+static inline int pdev_enable_cap_ats(struct pci_dev *pdev)356356+{357357+ struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev);358358+ int ret = -EINVAL;359359+360360+ if (dev_data->ats_enabled)361361+ return 0;362362+363363+ if (amd_iommu_iotlb_sup &&364364+ (dev_data->flags & AMD_IOMMU_DEVICE_FLAG_ATS_SUP)) {365365+ ret = pci_enable_ats(pdev, PAGE_SHIFT);366366+ if (!ret) {367367+ dev_data->ats_enabled = 1;368368+ dev_data->ats_qdep = pci_ats_queue_depth(pdev);369369+ }370370+ }371371+372372+ return ret;373373+}374374+375375+static inline void pdev_disable_cap_ats(struct pci_dev *pdev)376376+{377377+ struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev);378378+379379+ if (dev_data->ats_enabled) {380380+ pci_disable_ats(pdev);381381+ dev_data->ats_enabled = 0;382382+ }383383+}384384+385385+int amd_iommu_pdev_enable_cap_pri(struct pci_dev *pdev)386386+{387387+ struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev);388388+ int ret = -EINVAL;389389+390390+ if (dev_data->pri_enabled)391391+ return 0;392392+393393+ if (dev_data->flags & AMD_IOMMU_DEVICE_FLAG_PRI_SUP) {394394+ /*395395+ * First reset the PRI state of the device.396396+ * FIXME: Hardcode number of outstanding requests for now397397+ */398398+ if (!pci_reset_pri(pdev) && !pci_enable_pri(pdev, 32)) {399399+ dev_data->pri_enabled = 1;400400+ dev_data->pri_tlp = pci_prg_resp_pasid_required(pdev);401401+402402+ ret = 0;403403+ }404404+ }405405+406406+ return ret;407407+}408408+409409+void amd_iommu_pdev_disable_cap_pri(struct pci_dev *pdev)410410+{411411+ struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev);412412+413413+ if (dev_data->pri_enabled) {414414+ pci_disable_pri(pdev);415415+ dev_data->pri_enabled = 0;416416+ }417417+}418418+419419+static inline int pdev_enable_cap_pasid(struct pci_dev *pdev)420420+{421421+ struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev);422422+ int ret = -EINVAL;423423+424424+ if (dev_data->pasid_enabled)425425+ return 0;426426+427427+ if (dev_data->flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP) {428428+ /* Only allow access to user-accessible pages */429429+ ret = pci_enable_pasid(pdev, 0);430430+ if (!ret)431431+ dev_data->pasid_enabled = 1;432432+ }433433+434434+ return ret;435435+}436436+437437+static inline void pdev_disable_cap_pasid(struct pci_dev *pdev)438438+{439439+ struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev);440440+441441+ if (dev_data->pasid_enabled) {442442+ pci_disable_pasid(pdev);443443+ dev_data->pasid_enabled = 0;444444+ }445445+}446446+447447+static void pdev_enable_caps(struct pci_dev *pdev)448448+{449449+ pdev_enable_cap_ats(pdev);450450+ pdev_enable_cap_pasid(pdev);451451+ amd_iommu_pdev_enable_cap_pri(pdev);452452+453453+}454454+455455+static void pdev_disable_caps(struct pci_dev *pdev)456456+{457457+ pdev_disable_cap_ats(pdev);458458+ pdev_disable_cap_pasid(pdev);459459+ amd_iommu_pdev_disable_cap_pri(pdev);339460}340461341462/*···512399 * it'll be forced to go into translation mode.513400 */514401 if ((iommu_default_passthrough() || !amd_iommu_force_isolation) &&515515- dev_is_pci(dev) && pci_iommuv2_capable(to_pci_dev(dev))) {516516- dev_data->iommu_v2 = iommu->is_iommu_v2;402402+ dev_is_pci(dev) && amd_iommu_gt_ppr_supported()) {403403+ dev_data->flags = pdev_get_caps(to_pci_dev(dev));517404 }518405519406 dev_iommu_priv_set(dev, dev_data);···814701 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);815702}816703817817-static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)818818-{819819- struct amd_iommu_fault fault;820820-821821- if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {822822- pr_err_ratelimited("Unknown PPR request received\n");823823- return;824824- }825825-826826- fault.address = raw[1];827827- fault.pasid = PPR_PASID(raw[0]);828828- fault.sbdf = PCI_SEG_DEVID_TO_SBDF(iommu->pci_seg->id, PPR_DEVID(raw[0]));829829- fault.tag = PPR_TAG(raw[0]);830830- fault.flags = PPR_FLAGS(raw[0]);831831-832832- atomic_notifier_call_chain(&ppr_notifier, 0, &fault);833833-}834834-835704static void iommu_poll_ppr_log(struct amd_iommu *iommu)836705{837706 u32 head, tail;···859764 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;860765 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);861766862862- /* Handle PPR entry */863863- iommu_handle_ppr_entry(iommu, entry);767767+ /* TODO: PPR Handler will be added when we add IOPF support */864768865769 /* Refresh ring-buffer information */866770 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);···11881094}1189109511901096static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, u32 pasid,11911191- int status, int tag, bool gn)10971097+ int status, int tag, u8 gn)11921098{11931099 memset(cmd, 0, sizeof(*cmd));11941100···1392129813931299void iommu_flush_all_caches(struct amd_iommu *iommu)13941300{13951395- if (iommu_feature(iommu, FEATURE_IA)) {13011301+ if (check_feature(FEATURE_IA)) {13961302 amd_iommu_flush_all(iommu);13971303 } else {13981304 amd_iommu_flush_dte_all(iommu);···14111317 struct iommu_cmd cmd;14121318 int qdep;1413131914141414- qdep = dev_data->ats.qdep;13201320+ qdep = dev_data->ats_qdep;14151321 iommu = rlookup_amd_iommu(dev_data->dev);14161322 if (!iommu)14171323 return -EINVAL;···14621368 return ret;14631369 }1464137014651465- if (dev_data->ats.enabled)13711371+ if (dev_data->ats_enabled)14661372 ret = device_flush_iotlb(dev_data, 0, ~0UL);1467137314681374 return ret;···1495140114961402 list_for_each_entry(dev_data, &domain->dev_list, list) {1497140314981498- if (!dev_data->ats.enabled)14041404+ if (!dev_data->ats_enabled)14991405 continue;1500140615011407 ret |= device_flush_iotlb(dev_data, address, size);···16711577 free_page((unsigned long)domain->gcr3_tbl);16721578}1673157915801580+/*15811581+ * Number of GCR3 table levels required. Level must be 4-Kbyte15821582+ * page and can contain up to 512 entries.15831583+ */15841584+static int get_gcr3_levels(int pasids)15851585+{15861586+ int levels;15871587+15881588+ if (pasids == -1)15891589+ return amd_iommu_max_glx_val;15901590+15911591+ levels = get_count_order(pasids);15921592+15931593+ return levels ? (DIV_ROUND_UP(levels, 9) - 1) : levels;15941594+}15951595+15961596+/* Note: This function expects iommu_domain->lock to be held prior calling the function. */15971597+static int setup_gcr3_table(struct protection_domain *domain, int pasids)15981598+{15991599+ int levels = get_gcr3_levels(pasids);16001600+16011601+ if (levels > amd_iommu_max_glx_val)16021602+ return -EINVAL;16031603+16041604+ domain->gcr3_tbl = alloc_pgtable_page(domain->nid, GFP_ATOMIC);16051605+ if (domain->gcr3_tbl == NULL)16061606+ return -ENOMEM;16071607+16081608+ domain->glx = levels;16091609+ domain->flags |= PD_IOMMUV2_MASK;16101610+16111611+ amd_iommu_domain_update(domain);16121612+16131613+ return 0;16141614+}16151615+16741616static void set_dte_entry(struct amd_iommu *iommu, u16 devid,16751617 struct protection_domain *domain, bool ats, bool ppr)16761618{···17351605 if (ats)17361606 flags |= DTE_FLAG_IOTLB;1737160717381738- if (ppr) {17391739- if (iommu_feature(iommu, FEATURE_EPHSUP))17401740- pte_root |= 1ULL << DEV_ENTRY_PPR;17411741- }16081608+ if (ppr)16091609+ pte_root |= 1ULL << DEV_ENTRY_PPR;1742161017431611 if (domain->flags & PD_IOMMUV2_MASK) {17441612 u64 gcr3 = iommu_virt_to_phys(domain->gcr3_tbl);···18131685 iommu = rlookup_amd_iommu(dev_data->dev);18141686 if (!iommu)18151687 return;18161816- ats = dev_data->ats.enabled;16881688+ ats = dev_data->ats_enabled;1817168918181690 /* Update data structures */18191691 dev_data->domain = domain;···1829170118301702 /* Update device table */18311703 set_dte_entry(iommu, dev_data->devid, domain,18321832- ats, dev_data->iommu_v2);17041704+ ats, dev_data->ppr);18331705 clone_aliases(iommu, dev_data->dev);1834170618351707 device_flush_dte(dev_data);···18641736 domain->dev_cnt -= 1;18651737}1866173818671867-static void pdev_iommuv2_disable(struct pci_dev *pdev)18681868-{18691869- pci_disable_ats(pdev);18701870- pci_disable_pri(pdev);18711871- pci_disable_pasid(pdev);18721872-}18731873-18741874-static int pdev_pri_ats_enable(struct pci_dev *pdev)18751875-{18761876- int ret;18771877-18781878- /* Only allow access to user-accessible pages */18791879- ret = pci_enable_pasid(pdev, 0);18801880- if (ret)18811881- return ret;18821882-18831883- /* First reset the PRI state of the device */18841884- ret = pci_reset_pri(pdev);18851885- if (ret)18861886- goto out_err_pasid;18871887-18881888- /* Enable PRI */18891889- /* FIXME: Hardcode number of outstanding requests for now */18901890- ret = pci_enable_pri(pdev, 32);18911891- if (ret)18921892- goto out_err_pasid;18931893-18941894- ret = pci_enable_ats(pdev, PAGE_SHIFT);18951895- if (ret)18961896- goto out_err_pri;18971897-18981898- return 0;18991899-19001900-out_err_pri:19011901- pci_disable_pri(pdev);19021902-19031903-out_err_pasid:19041904- pci_disable_pasid(pdev);19051905-19061906- return ret;19071907-}19081908-19091739/*19101740 * If a device is not yet associated with a domain, this function makes the19111741 * device visible in the domain···18721786 struct protection_domain *domain)18731787{18741788 struct iommu_dev_data *dev_data;18751875- struct pci_dev *pdev;18761789 unsigned long flags;18771877- int ret;17901790+ int ret = 0;1878179118791792 spin_lock_irqsave(&domain->lock, flags);18801793···1881179618821797 spin_lock(&dev_data->lock);1883179818841884- ret = -EBUSY;18851885- if (dev_data->domain != NULL)17991799+ if (dev_data->domain != NULL) {18001800+ ret = -EBUSY;18861801 goto out;18871887-18881888- if (!dev_is_pci(dev))18891889- goto skip_ats_check;18901890-18911891- pdev = to_pci_dev(dev);18921892- if (domain->flags & PD_IOMMUV2_MASK) {18931893- struct iommu_domain *def_domain = iommu_get_dma_domain(dev);18941894-18951895- ret = -EINVAL;18961896-18971897- /*18981898- * In case of using AMD_IOMMU_V1 page table mode and the device18991899- * is enabling for PPR/ATS support (using v2 table),19001900- * we need to make sure that the domain type is identity map.19011901- */19021902- if ((amd_iommu_pgtable == AMD_IOMMU_V1) &&19031903- def_domain->type != IOMMU_DOMAIN_IDENTITY) {19041904- goto out;19051905- }19061906-19071907- if (dev_data->iommu_v2) {19081908- if (pdev_pri_ats_enable(pdev) != 0)19091909- goto out;19101910-19111911- dev_data->ats.enabled = true;19121912- dev_data->ats.qdep = pci_ats_queue_depth(pdev);19131913- dev_data->pri_tlp = pci_prg_resp_pasid_required(pdev);19141914- }19151915- } else if (amd_iommu_iotlb_sup &&19161916- pci_enable_ats(pdev, PAGE_SHIFT) == 0) {19171917- dev_data->ats.enabled = true;19181918- dev_data->ats.qdep = pci_ats_queue_depth(pdev);19191802 }1920180319211921-skip_ats_check:19221922- ret = 0;18041804+ if (dev_is_pci(dev))18051805+ pdev_enable_caps(to_pci_dev(dev));1923180619241807 do_attach(dev_data, domain);19251808···1935188219361883 do_detach(dev_data);1937188419381938- if (!dev_is_pci(dev))19391939- goto out;19401940-19411941- if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2)19421942- pdev_iommuv2_disable(to_pci_dev(dev));19431943- else if (dev_data->ats.enabled)19441944- pci_disable_ats(to_pci_dev(dev));19451945-19461946- dev_data->ats.enabled = false;18851885+ if (dev_is_pci(dev))18861886+ pdev_disable_caps(to_pci_dev(dev));1947188719481888out:19491889 spin_unlock(&dev_data->lock);···20261980 if (!iommu)20271981 continue;20281982 set_dte_entry(iommu, dev_data->devid, domain,20292029- dev_data->ats.enabled, dev_data->iommu_v2);19831983+ dev_data->ats_enabled, dev_data->ppr);20301984 clone_aliases(iommu, dev_data->dev);20311985 }20321986}···20602014static void cleanup_domain(struct protection_domain *domain)20612015{20622016 struct iommu_dev_data *entry;20632063- unsigned long flags;2064201720652065- spin_lock_irqsave(&domain->lock, flags);20182018+ lockdep_assert_held(&domain->lock);20192019+20202020+ if (!domain->dev_cnt)20212021+ return;2066202220672023 while (!list_empty(&domain->dev_list)) {20682024 entry = list_first_entry(&domain->dev_list,···20722024 BUG_ON(!entry->domain);20732025 do_detach(entry);20742026 }20752075-20762076- spin_unlock_irqrestore(&domain->lock, flags);20272027+ WARN_ON(domain->dev_cnt != 0);20772028}2078202920792030static void protection_domain_free(struct protection_domain *domain)···2082203520832036 if (domain->iop.pgtbl_cfg.tlb)20842037 free_io_pgtable_ops(&domain->iop.iop.ops);20382038+20392039+ if (domain->flags & PD_IOMMUV2_MASK)20402040+ free_gcr3_table(domain);20412041+20422042+ if (domain->iop.root)20432043+ free_page((unsigned long)domain->iop.root);2085204420862045 if (domain->id)20872046 domain_id_free(domain->id);···2101204821022049 BUG_ON(mode < PAGE_MODE_NONE || mode > PAGE_MODE_6_LEVEL);2103205021042104- spin_lock_init(&domain->lock);21052105- domain->id = domain_id_alloc();21062106- if (!domain->id)21072107- return -ENOMEM;21082108- INIT_LIST_HEAD(&domain->dev_list);21092109-21102051 if (mode != PAGE_MODE_NONE) {21112052 pt_root = (void *)get_zeroed_page(GFP_KERNEL);21122112- if (!pt_root) {21132113- domain_id_free(domain->id);20532053+ if (!pt_root)21142054 return -ENOMEM;21152115- }21162055 }2117205621182057 amd_iommu_domain_set_pgtable(domain, pt_root, mode);···2114206921152070static int protection_domain_init_v2(struct protection_domain *domain)21162071{21172117- spin_lock_init(&domain->lock);21182118- domain->id = domain_id_alloc();21192119- if (!domain->id)21202120- return -ENOMEM;21212121- INIT_LIST_HEAD(&domain->dev_list);21222122-21232072 domain->flags |= PD_GIOV_MASK;2124207321252074 domain->domain.pgsize_bitmap = AMD_IOMMU_PGSIZES_V2;2126207521272127- if (domain_enable_v2(domain, 1)) {21282128- domain_id_free(domain->id);20762076+ if (setup_gcr3_table(domain, 1))21292077 return -ENOMEM;21302130- }2131207821322079 return 0;21332080}···21292092 struct io_pgtable_ops *pgtbl_ops;21302093 struct protection_domain *domain;21312094 int pgtable;21322132- int mode = DEFAULT_PGTABLE_LEVEL;21332095 int ret;21342134-21352135- /*21362136- * Force IOMMU v1 page table when iommu=pt and21372137- * when allocating domain for pass-through devices.21382138- */21392139- if (type == IOMMU_DOMAIN_IDENTITY) {21402140- pgtable = AMD_IOMMU_V1;21412141- mode = PAGE_MODE_NONE;21422142- } else if (type == IOMMU_DOMAIN_UNMANAGED) {21432143- pgtable = AMD_IOMMU_V1;21442144- } else if (type == IOMMU_DOMAIN_DMA || type == IOMMU_DOMAIN_DMA_FQ) {21452145- pgtable = amd_iommu_pgtable;21462146- } else {21472147- return NULL;21482148- }2149209621502097 domain = kzalloc(sizeof(*domain), GFP_KERNEL);21512098 if (!domain)21522099 return NULL;2153210021012101+ domain->id = domain_id_alloc();21022102+ if (!domain->id)21032103+ goto out_err;21042104+21052105+ spin_lock_init(&domain->lock);21062106+ INIT_LIST_HEAD(&domain->dev_list);21072107+ domain->nid = NUMA_NO_NODE;21082108+21092109+ switch (type) {21102110+ /* No need to allocate io pgtable ops in passthrough mode */21112111+ case IOMMU_DOMAIN_IDENTITY:21122112+ return domain;21132113+ case IOMMU_DOMAIN_DMA:21142114+ pgtable = amd_iommu_pgtable;21152115+ break;21162116+ /*21172117+ * Force IOMMU v1 page table when allocating21182118+ * domain for pass-through devices.21192119+ */21202120+ case IOMMU_DOMAIN_UNMANAGED:21212121+ pgtable = AMD_IOMMU_V1;21222122+ break;21232123+ default:21242124+ goto out_err;21252125+ }21262126+21542127 switch (pgtable) {21552128 case AMD_IOMMU_V1:21562156- ret = protection_domain_init_v1(domain, mode);21292129+ ret = protection_domain_init_v1(domain, DEFAULT_PGTABLE_LEVEL);21572130 break;21582131 case AMD_IOMMU_V2:21592132 ret = protection_domain_init_v2(domain);21602133 break;21612134 default:21622135 ret = -EINVAL;21362136+ break;21632137 }2164213821652139 if (ret)21662140 goto out_err;2167214121682168- /* No need to allocate io pgtable ops in passthrough mode */21692169- if (type == IOMMU_DOMAIN_IDENTITY)21702170- return domain;21712171-21722172- domain->nid = NUMA_NO_NODE;21732173-21742142 pgtbl_ops = alloc_io_pgtable_ops(pgtable, &domain->iop.pgtbl_cfg, domain);21752175- if (!pgtbl_ops) {21762176- domain_id_free(domain->id);21432143+ if (!pgtbl_ops)21772144 goto out_err;21782178- }2179214521802146 return domain;21812147out_err:21822182- kfree(domain);21482148+ protection_domain_free(domain);21832149 return NULL;21842150}21852151···22202180static void amd_iommu_domain_free(struct iommu_domain *dom)22212181{22222182 struct protection_domain *domain;22232223-22242224- domain = to_pdomain(dom);22252225-22262226- if (domain->dev_cnt > 0)22272227- cleanup_domain(domain);22282228-22292229- BUG_ON(domain->dev_cnt != 0);21832183+ unsigned long flags;2230218422312185 if (!dom)22322186 return;2233218722342234- if (domain->flags & PD_IOMMUV2_MASK)22352235- free_gcr3_table(domain);21882188+ domain = to_pdomain(dom);21892189+21902190+ spin_lock_irqsave(&domain->lock, flags);21912191+21922192+ cleanup_domain(domain);21932193+21942194+ spin_unlock_irqrestore(&domain->lock, flags);2236219522372196 protection_domain_free(domain);22382197}···22722233 return ret;22732234}2274223522752275-static void amd_iommu_iotlb_sync_map(struct iommu_domain *dom,22762276- unsigned long iova, size_t size)22362236+static int amd_iommu_iotlb_sync_map(struct iommu_domain *dom,22372237+ unsigned long iova, size_t size)22772238{22782239 struct protection_domain *domain = to_pdomain(dom);22792240 struct io_pgtable_ops *ops = &domain->iop.iop.ops;2280224122812242 if (ops->map_pages)22822243 domain_flush_np_cache(domain, iova, size);22442244+ return 0;22832245}2284224622852247static int amd_iommu_map_pages(struct iommu_domain *dom, unsigned long iova,···2446240624472407 return dev_data->defer_attach;24482408}24492449-EXPORT_SYMBOL_GPL(amd_iommu_is_attach_deferred);2450240924512410static void amd_iommu_flush_iotlb_all(struct iommu_domain *domain)24522411{···24852446 * and require remapping.24862447 * - SNP is enabled, because it prohibits DTE[Mode]=0.24872448 */24882488- if (dev_data->iommu_v2 &&24492449+ if (pdev_pasid_supported(dev_data) &&24892450 !cc_platform_has(CC_ATTR_MEM_ENCRYPT) &&24902451 !amd_iommu_snp_en) {24912452 return IOMMU_DOMAIN_IDENTITY;···25242485 }25252486};2526248725272527-/*****************************************************************************25282528- *25292529- * The next functions do a basic initialization of IOMMU for pass through25302530- * mode25312531- *25322532- * In passthrough mode the IOMMU is initialized and enabled but not used for25332533- * DMA-API translation.25342534- *25352535- *****************************************************************************/25362536-25372537-/* IOMMUv2 specific functions */25382538-int amd_iommu_register_ppr_notifier(struct notifier_block *nb)25392539-{25402540- return atomic_notifier_chain_register(&ppr_notifier, nb);25412541-}25422542-EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);25432543-25442544-int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)25452545-{25462546- return atomic_notifier_chain_unregister(&ppr_notifier, nb);25472547-}25482548-EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);25492549-25502550-void amd_iommu_domain_direct_map(struct iommu_domain *dom)25512551-{25522552- struct protection_domain *domain = to_pdomain(dom);25532553- unsigned long flags;25542554-25552555- spin_lock_irqsave(&domain->lock, flags);25562556-25572557- if (domain->iop.pgtbl_cfg.tlb)25582558- free_io_pgtable_ops(&domain->iop.iop.ops);25592559-25602560- spin_unlock_irqrestore(&domain->lock, flags);25612561-}25622562-EXPORT_SYMBOL(amd_iommu_domain_direct_map);25632563-25642564-/* Note: This function expects iommu_domain->lock to be held prior calling the function. */25652565-static int domain_enable_v2(struct protection_domain *domain, int pasids)25662566-{25672567- int levels;25682568-25692569- /* Number of GCR3 table levels required */25702570- for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)25712571- levels += 1;25722572-25732573- if (levels > amd_iommu_max_glx_val)25742574- return -EINVAL;25752575-25762576- domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);25772577- if (domain->gcr3_tbl == NULL)25782578- return -ENOMEM;25792579-25802580- domain->glx = levels;25812581- domain->flags |= PD_IOMMUV2_MASK;25822582-25832583- amd_iommu_domain_update(domain);25842584-25852585- return 0;25862586-}25872587-25882588-int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)25892589-{25902590- struct protection_domain *pdom = to_pdomain(dom);25912591- unsigned long flags;25922592- int ret;25932593-25942594- spin_lock_irqsave(&pdom->lock, flags);25952595-25962596- /*25972597- * Save us all sanity checks whether devices already in the25982598- * domain support IOMMUv2. Just force that the domain has no25992599- * devices attached when it is switched into IOMMUv2 mode.26002600- */26012601- ret = -EBUSY;26022602- if (pdom->dev_cnt > 0 || pdom->flags & PD_IOMMUV2_MASK)26032603- goto out;26042604-26052605- if (!pdom->gcr3_tbl)26062606- ret = domain_enable_v2(pdom, pasids);26072607-26082608-out:26092609- spin_unlock_irqrestore(&pdom->lock, flags);26102610- return ret;26112611-}26122612-EXPORT_SYMBOL(amd_iommu_domain_enable_v2);26132613-26142488static int __flush_pasid(struct protection_domain *domain, u32 pasid,26152489 u64 address, bool size)26162490{···25612609 There might be non-IOMMUv2 capable devices in an IOMMUv225622610 * domain.25632611 */25642564- if (!dev_data->ats.enabled)26122612+ if (!dev_data->ats_enabled)25652613 continue;2566261425672567- qdep = dev_data->ats.qdep;26152615+ qdep = dev_data->ats_qdep;25682616 iommu = rlookup_amd_iommu(dev_data->dev);25692617 if (!iommu)25702618 continue;···2605265326062654 return ret;26072655}26082608-EXPORT_SYMBOL(amd_iommu_flush_page);2609265626102657static int __amd_iommu_flush_tlb(struct protection_domain *domain, u32 pasid)26112658{···2624267326252674 return ret;26262675}26272627-EXPORT_SYMBOL(amd_iommu_flush_tlb);2628267626292677static u64 *__get_gcr3_pte(u64 *root, int level, u32 pasid, bool alloc)26302678{···2703275327042754 return ret;27052755}27062706-EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);2707275627082757int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, u32 pasid)27092758{···2716276727172768 return ret;27182769}27192719-EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);2720277027212771int amd_iommu_complete_ppr(struct pci_dev *pdev, u32 pasid,27222772 int status, int tag)···2734278627352787 return iommu_queue_command(iommu, &cmd);27362788}27372737-EXPORT_SYMBOL(amd_iommu_complete_ppr);27382738-27392739-int amd_iommu_device_info(struct pci_dev *pdev,27402740- struct amd_iommu_device_info *info)27412741-{27422742- int max_pasids;27432743- int pos;27442744-27452745- if (pdev == NULL || info == NULL)27462746- return -EINVAL;27472747-27482748- if (!amd_iommu_v2_supported())27492749- return -EINVAL;27502750-27512751- memset(info, 0, sizeof(*info));27522752-27532753- if (pci_ats_supported(pdev))27542754- info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;27552755-27562756- pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);27572757- if (pos)27582758- info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;27592759-27602760- pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);27612761- if (pos) {27622762- int features;27632763-27642764- max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));27652765- max_pasids = min(max_pasids, (1 << 20));27662766-27672767- info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;27682768- info->max_pasids = min(pci_max_pasids(pdev), max_pasids);27692769-27702770- features = pci_pasid_features(pdev);27712771- if (features & PCI_PASID_CAP_EXEC)27722772- info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;27732773- if (features & PCI_PASID_CAP_PRIV)27742774- info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;27752775- }27762776-27772777- return 0;27782778-}27792779-EXPORT_SYMBOL(amd_iommu_device_info);2780278927812790#ifdef CONFIG_IRQ_REMAP27822791
-996
drivers/iommu/amd/iommu_v2.c
···11-// SPDX-License-Identifier: GPL-2.0-only22-/*33- * Copyright (C) 2010-2012 Advanced Micro Devices, Inc.44- * Author: Joerg Roedel <jroedel@suse.de>55- */66-77-#define pr_fmt(fmt) "AMD-Vi: " fmt88-99-#include <linux/refcount.h>1010-#include <linux/mmu_notifier.h>1111-#include <linux/amd-iommu.h>1212-#include <linux/mm_types.h>1313-#include <linux/profile.h>1414-#include <linux/module.h>1515-#include <linux/sched.h>1616-#include <linux/sched/mm.h>1717-#include <linux/wait.h>1818-#include <linux/pci.h>1919-#include <linux/gfp.h>2020-#include <linux/cc_platform.h>2121-2222-#include "amd_iommu.h"2323-2424-MODULE_LICENSE("GPL v2");2525-MODULE_AUTHOR("Joerg Roedel <jroedel@suse.de>");2626-2727-#define PRI_QUEUE_SIZE 5122828-2929-struct pri_queue {3030- atomic_t inflight;3131- bool finish;3232- int status;3333-};3434-3535-struct pasid_state {3636- struct list_head list; /* For global state-list */3737- refcount_t count; /* Reference count */3838- unsigned mmu_notifier_count; /* Counting nested mmu_notifier3939- calls */4040- struct mm_struct *mm; /* mm_struct for the faults */4141- struct mmu_notifier mn; /* mmu_notifier handle */4242- struct pri_queue pri[PRI_QUEUE_SIZE]; /* PRI tag states */4343- struct device_state *device_state; /* Link to our device_state */4444- u32 pasid; /* PASID index */4545- bool invalid; /* Used during setup and4646- teardown of the pasid */4747- spinlock_t lock; /* Protect pri_queues and4848- mmu_notifer_count */4949- wait_queue_head_t wq; /* To wait for count == 0 */5050-};5151-5252-struct device_state {5353- struct list_head list;5454- u32 sbdf;5555- atomic_t count;5656- struct pci_dev *pdev;5757- struct pasid_state **states;5858- struct iommu_domain *domain;5959- int pasid_levels;6060- int max_pasids;6161- amd_iommu_invalid_ppr_cb inv_ppr_cb;6262- amd_iommu_invalidate_ctx inv_ctx_cb;6363- spinlock_t lock;6464- wait_queue_head_t wq;6565-};6666-6767-struct fault {6868- struct work_struct work;6969- struct device_state *dev_state;7070- struct pasid_state *state;7171- struct mm_struct *mm;7272- u64 address;7373- u32 pasid;7474- u16 tag;7575- u16 finish;7676- u16 flags;7777-};7878-7979-static LIST_HEAD(state_list);8080-static DEFINE_SPINLOCK(state_lock);8181-8282-static struct workqueue_struct *iommu_wq;8383-8484-static void free_pasid_states(struct device_state *dev_state);8585-8686-static struct device_state *__get_device_state(u32 sbdf)8787-{8888- struct device_state *dev_state;8989-9090- list_for_each_entry(dev_state, &state_list, list) {9191- if (dev_state->sbdf == sbdf)9292- return dev_state;9393- }9494-9595- return NULL;9696-}9797-9898-static struct device_state *get_device_state(u32 sbdf)9999-{100100- struct device_state *dev_state;101101- unsigned long flags;102102-103103- spin_lock_irqsave(&state_lock, flags);104104- dev_state = __get_device_state(sbdf);105105- if (dev_state != NULL)106106- atomic_inc(&dev_state->count);107107- spin_unlock_irqrestore(&state_lock, flags);108108-109109- return dev_state;110110-}111111-112112-static void free_device_state(struct device_state *dev_state)113113-{114114- struct iommu_group *group;115115-116116- /* Get rid of any remaining pasid states */117117- free_pasid_states(dev_state);118118-119119- /*120120- * Wait until the last reference is dropped before freeing121121- * the device state.122122- */123123- wait_event(dev_state->wq, !atomic_read(&dev_state->count));124124-125125- /*126126- * First detach device from domain - No more PRI requests will arrive127127- * from that device after it is unbound from the IOMMUv2 domain.128128- */129129- group = iommu_group_get(&dev_state->pdev->dev);130130- if (WARN_ON(!group))131131- return;132132-133133- iommu_detach_group(dev_state->domain, group);134134-135135- iommu_group_put(group);136136-137137- /* Everything is down now, free the IOMMUv2 domain */138138- iommu_domain_free(dev_state->domain);139139-140140- /* Finally get rid of the device-state */141141- kfree(dev_state);142142-}143143-144144-static void put_device_state(struct device_state *dev_state)145145-{146146- if (atomic_dec_and_test(&dev_state->count))147147- wake_up(&dev_state->wq);148148-}149149-150150-/* Must be called under dev_state->lock */151151-static struct pasid_state **__get_pasid_state_ptr(struct device_state *dev_state,152152- u32 pasid, bool alloc)153153-{154154- struct pasid_state **root, **ptr;155155- int level, index;156156-157157- level = dev_state->pasid_levels;158158- root = dev_state->states;159159-160160- while (true) {161161-162162- index = (pasid >> (9 * level)) & 0x1ff;163163- ptr = &root[index];164164-165165- if (level == 0)166166- break;167167-168168- if (*ptr == NULL) {169169- if (!alloc)170170- return NULL;171171-172172- *ptr = (void *)get_zeroed_page(GFP_ATOMIC);173173- if (*ptr == NULL)174174- return NULL;175175- }176176-177177- root = (struct pasid_state **)*ptr;178178- level -= 1;179179- }180180-181181- return ptr;182182-}183183-184184-static int set_pasid_state(struct device_state *dev_state,185185- struct pasid_state *pasid_state,186186- u32 pasid)187187-{188188- struct pasid_state **ptr;189189- unsigned long flags;190190- int ret;191191-192192- spin_lock_irqsave(&dev_state->lock, flags);193193- ptr = __get_pasid_state_ptr(dev_state, pasid, true);194194-195195- ret = -ENOMEM;196196- if (ptr == NULL)197197- goto out_unlock;198198-199199- ret = -ENOMEM;200200- if (*ptr != NULL)201201- goto out_unlock;202202-203203- *ptr = pasid_state;204204-205205- ret = 0;206206-207207-out_unlock:208208- spin_unlock_irqrestore(&dev_state->lock, flags);209209-210210- return ret;211211-}212212-213213-static void clear_pasid_state(struct device_state *dev_state, u32 pasid)214214-{215215- struct pasid_state **ptr;216216- unsigned long flags;217217-218218- spin_lock_irqsave(&dev_state->lock, flags);219219- ptr = __get_pasid_state_ptr(dev_state, pasid, true);220220-221221- if (ptr == NULL)222222- goto out_unlock;223223-224224- *ptr = NULL;225225-226226-out_unlock:227227- spin_unlock_irqrestore(&dev_state->lock, flags);228228-}229229-230230-static struct pasid_state *get_pasid_state(struct device_state *dev_state,231231- u32 pasid)232232-{233233- struct pasid_state **ptr, *ret = NULL;234234- unsigned long flags;235235-236236- spin_lock_irqsave(&dev_state->lock, flags);237237- ptr = __get_pasid_state_ptr(dev_state, pasid, false);238238-239239- if (ptr == NULL)240240- goto out_unlock;241241-242242- ret = *ptr;243243- if (ret)244244- refcount_inc(&ret->count);245245-246246-out_unlock:247247- spin_unlock_irqrestore(&dev_state->lock, flags);248248-249249- return ret;250250-}251251-252252-static void free_pasid_state(struct pasid_state *pasid_state)253253-{254254- kfree(pasid_state);255255-}256256-257257-static void put_pasid_state(struct pasid_state *pasid_state)258258-{259259- if (refcount_dec_and_test(&pasid_state->count))260260- wake_up(&pasid_state->wq);261261-}262262-263263-static void put_pasid_state_wait(struct pasid_state *pasid_state)264264-{265265- if (!refcount_dec_and_test(&pasid_state->count))266266- wait_event(pasid_state->wq, !refcount_read(&pasid_state->count));267267- free_pasid_state(pasid_state);268268-}269269-270270-static void unbind_pasid(struct pasid_state *pasid_state)271271-{272272- struct iommu_domain *domain;273273-274274- domain = pasid_state->device_state->domain;275275-276276- /*277277- * Mark pasid_state as invalid, no more faults will we added to the278278- * work queue after this is visible everywhere.279279- */280280- pasid_state->invalid = true;281281-282282- /* Make sure this is visible */283283- smp_wmb();284284-285285- /* After this the device/pasid can't access the mm anymore */286286- amd_iommu_domain_clear_gcr3(domain, pasid_state->pasid);287287-288288- /* Make sure no more pending faults are in the queue */289289- flush_workqueue(iommu_wq);290290-}291291-292292-static void free_pasid_states_level1(struct pasid_state **tbl)293293-{294294- int i;295295-296296- for (i = 0; i < 512; ++i) {297297- if (tbl[i] == NULL)298298- continue;299299-300300- free_page((unsigned long)tbl[i]);301301- }302302-}303303-304304-static void free_pasid_states_level2(struct pasid_state **tbl)305305-{306306- struct pasid_state **ptr;307307- int i;308308-309309- for (i = 0; i < 512; ++i) {310310- if (tbl[i] == NULL)311311- continue;312312-313313- ptr = (struct pasid_state **)tbl[i];314314- free_pasid_states_level1(ptr);315315- }316316-}317317-318318-static void free_pasid_states(struct device_state *dev_state)319319-{320320- struct pasid_state *pasid_state;321321- int i;322322-323323- for (i = 0; i < dev_state->max_pasids; ++i) {324324- pasid_state = get_pasid_state(dev_state, i);325325- if (pasid_state == NULL)326326- continue;327327-328328- put_pasid_state(pasid_state);329329-330330- /* Clear the pasid state so that the pasid can be re-used */331331- clear_pasid_state(dev_state, pasid_state->pasid);332332-333333- /*334334- * This will call the mn_release function and335335- * unbind the PASID336336- */337337- mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);338338-339339- put_pasid_state_wait(pasid_state); /* Reference taken in340340- amd_iommu_bind_pasid */341341-342342- /* Drop reference taken in amd_iommu_bind_pasid */343343- put_device_state(dev_state);344344- }345345-346346- if (dev_state->pasid_levels == 2)347347- free_pasid_states_level2(dev_state->states);348348- else if (dev_state->pasid_levels == 1)349349- free_pasid_states_level1(dev_state->states);350350- else351351- BUG_ON(dev_state->pasid_levels != 0);352352-353353- free_page((unsigned long)dev_state->states);354354-}355355-356356-static struct pasid_state *mn_to_state(struct mmu_notifier *mn)357357-{358358- return container_of(mn, struct pasid_state, mn);359359-}360360-361361-static void mn_arch_invalidate_secondary_tlbs(struct mmu_notifier *mn,362362- struct mm_struct *mm,363363- unsigned long start, unsigned long end)364364-{365365- struct pasid_state *pasid_state;366366- struct device_state *dev_state;367367-368368- pasid_state = mn_to_state(mn);369369- dev_state = pasid_state->device_state;370370-371371- if ((start ^ (end - 1)) < PAGE_SIZE)372372- amd_iommu_flush_page(dev_state->domain, pasid_state->pasid,373373- start);374374- else375375- amd_iommu_flush_tlb(dev_state->domain, pasid_state->pasid);376376-}377377-378378-static void mn_release(struct mmu_notifier *mn, struct mm_struct *mm)379379-{380380- struct pasid_state *pasid_state;381381- struct device_state *dev_state;382382- bool run_inv_ctx_cb;383383-384384- might_sleep();385385-386386- pasid_state = mn_to_state(mn);387387- dev_state = pasid_state->device_state;388388- run_inv_ctx_cb = !pasid_state->invalid;389389-390390- if (run_inv_ctx_cb && dev_state->inv_ctx_cb)391391- dev_state->inv_ctx_cb(dev_state->pdev, pasid_state->pasid);392392-393393- unbind_pasid(pasid_state);394394-}395395-396396-static const struct mmu_notifier_ops iommu_mn = {397397- .release = mn_release,398398- .arch_invalidate_secondary_tlbs = mn_arch_invalidate_secondary_tlbs,399399-};400400-401401-static void set_pri_tag_status(struct pasid_state *pasid_state,402402- u16 tag, int status)403403-{404404- unsigned long flags;405405-406406- spin_lock_irqsave(&pasid_state->lock, flags);407407- pasid_state->pri[tag].status = status;408408- spin_unlock_irqrestore(&pasid_state->lock, flags);409409-}410410-411411-static void finish_pri_tag(struct device_state *dev_state,412412- struct pasid_state *pasid_state,413413- u16 tag)414414-{415415- unsigned long flags;416416-417417- spin_lock_irqsave(&pasid_state->lock, flags);418418- if (atomic_dec_and_test(&pasid_state->pri[tag].inflight) &&419419- pasid_state->pri[tag].finish) {420420- amd_iommu_complete_ppr(dev_state->pdev, pasid_state->pasid,421421- pasid_state->pri[tag].status, tag);422422- pasid_state->pri[tag].finish = false;423423- pasid_state->pri[tag].status = PPR_SUCCESS;424424- }425425- spin_unlock_irqrestore(&pasid_state->lock, flags);426426-}427427-428428-static void handle_fault_error(struct fault *fault)429429-{430430- int status;431431-432432- if (!fault->dev_state->inv_ppr_cb) {433433- set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);434434- return;435435- }436436-437437- status = fault->dev_state->inv_ppr_cb(fault->dev_state->pdev,438438- fault->pasid,439439- fault->address,440440- fault->flags);441441- switch (status) {442442- case AMD_IOMMU_INV_PRI_RSP_SUCCESS:443443- set_pri_tag_status(fault->state, fault->tag, PPR_SUCCESS);444444- break;445445- case AMD_IOMMU_INV_PRI_RSP_INVALID:446446- set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);447447- break;448448- case AMD_IOMMU_INV_PRI_RSP_FAIL:449449- set_pri_tag_status(fault->state, fault->tag, PPR_FAILURE);450450- break;451451- default:452452- BUG();453453- }454454-}455455-456456-static bool access_error(struct vm_area_struct *vma, struct fault *fault)457457-{458458- unsigned long requested = 0;459459-460460- if (fault->flags & PPR_FAULT_EXEC)461461- requested |= VM_EXEC;462462-463463- if (fault->flags & PPR_FAULT_READ)464464- requested |= VM_READ;465465-466466- if (fault->flags & PPR_FAULT_WRITE)467467- requested |= VM_WRITE;468468-469469- return (requested & ~vma->vm_flags) != 0;470470-}471471-472472-static void do_fault(struct work_struct *work)473473-{474474- struct fault *fault = container_of(work, struct fault, work);475475- struct vm_area_struct *vma;476476- vm_fault_t ret = VM_FAULT_ERROR;477477- unsigned int flags = 0;478478- struct mm_struct *mm;479479- u64 address;480480-481481- mm = fault->state->mm;482482- address = fault->address;483483-484484- if (fault->flags & PPR_FAULT_USER)485485- flags |= FAULT_FLAG_USER;486486- if (fault->flags & PPR_FAULT_WRITE)487487- flags |= FAULT_FLAG_WRITE;488488- flags |= FAULT_FLAG_REMOTE;489489-490490- mmap_read_lock(mm);491491- vma = vma_lookup(mm, address);492492- if (!vma)493493- /* failed to get a vma in the right range */494494- goto out;495495-496496- /* Check if we have the right permissions on the vma */497497- if (access_error(vma, fault))498498- goto out;499499-500500- ret = handle_mm_fault(vma, address, flags, NULL);501501-out:502502- mmap_read_unlock(mm);503503-504504- if (ret & VM_FAULT_ERROR)505505- /* failed to service fault */506506- handle_fault_error(fault);507507-508508- finish_pri_tag(fault->dev_state, fault->state, fault->tag);509509-510510- put_pasid_state(fault->state);511511-512512- kfree(fault);513513-}514514-515515-static int ppr_notifier(struct notifier_block *nb, unsigned long e, void *data)516516-{517517- struct amd_iommu_fault *iommu_fault;518518- struct pasid_state *pasid_state;519519- struct device_state *dev_state;520520- struct pci_dev *pdev = NULL;521521- unsigned long flags;522522- struct fault *fault;523523- bool finish;524524- u16 tag, devid, seg_id;525525- int ret;526526-527527- iommu_fault = data;528528- tag = iommu_fault->tag & 0x1ff;529529- finish = (iommu_fault->tag >> 9) & 1;530530-531531- seg_id = PCI_SBDF_TO_SEGID(iommu_fault->sbdf);532532- devid = PCI_SBDF_TO_DEVID(iommu_fault->sbdf);533533- pdev = pci_get_domain_bus_and_slot(seg_id, PCI_BUS_NUM(devid),534534- devid & 0xff);535535- if (!pdev)536536- return -ENODEV;537537-538538- ret = NOTIFY_DONE;539539-540540- /* In kdump kernel pci dev is not initialized yet -> send INVALID */541541- if (amd_iommu_is_attach_deferred(&pdev->dev)) {542542- amd_iommu_complete_ppr(pdev, iommu_fault->pasid,543543- PPR_INVALID, tag);544544- goto out;545545- }546546-547547- dev_state = get_device_state(iommu_fault->sbdf);548548- if (dev_state == NULL)549549- goto out;550550-551551- pasid_state = get_pasid_state(dev_state, iommu_fault->pasid);552552- if (pasid_state == NULL || pasid_state->invalid) {553553- /* We know the device but not the PASID -> send INVALID */554554- amd_iommu_complete_ppr(dev_state->pdev, iommu_fault->pasid,555555- PPR_INVALID, tag);556556- goto out_drop_state;557557- }558558-559559- spin_lock_irqsave(&pasid_state->lock, flags);560560- atomic_inc(&pasid_state->pri[tag].inflight);561561- if (finish)562562- pasid_state->pri[tag].finish = true;563563- spin_unlock_irqrestore(&pasid_state->lock, flags);564564-565565- fault = kzalloc(sizeof(*fault), GFP_ATOMIC);566566- if (fault == NULL) {567567- /* We are OOM - send success and let the device re-fault */568568- finish_pri_tag(dev_state, pasid_state, tag);569569- goto out_drop_state;570570- }571571-572572- fault->dev_state = dev_state;573573- fault->address = iommu_fault->address;574574- fault->state = pasid_state;575575- fault->tag = tag;576576- fault->finish = finish;577577- fault->pasid = iommu_fault->pasid;578578- fault->flags = iommu_fault->flags;579579- INIT_WORK(&fault->work, do_fault);580580-581581- queue_work(iommu_wq, &fault->work);582582-583583- ret = NOTIFY_OK;584584-585585-out_drop_state:586586-587587- if (ret != NOTIFY_OK && pasid_state)588588- put_pasid_state(pasid_state);589589-590590- put_device_state(dev_state);591591-592592-out:593593- pci_dev_put(pdev);594594- return ret;595595-}596596-597597-static struct notifier_block ppr_nb = {598598- .notifier_call = ppr_notifier,599599-};600600-601601-int amd_iommu_bind_pasid(struct pci_dev *pdev, u32 pasid,602602- struct task_struct *task)603603-{604604- struct pasid_state *pasid_state;605605- struct device_state *dev_state;606606- struct mm_struct *mm;607607- u32 sbdf;608608- int ret;609609-610610- might_sleep();611611-612612- if (!amd_iommu_v2_supported())613613- return -ENODEV;614614-615615- sbdf = get_pci_sbdf_id(pdev);616616- dev_state = get_device_state(sbdf);617617-618618- if (dev_state == NULL)619619- return -EINVAL;620620-621621- ret = -EINVAL;622622- if (pasid >= dev_state->max_pasids)623623- goto out;624624-625625- ret = -ENOMEM;626626- pasid_state = kzalloc(sizeof(*pasid_state), GFP_KERNEL);627627- if (pasid_state == NULL)628628- goto out;629629-630630-631631- refcount_set(&pasid_state->count, 1);632632- init_waitqueue_head(&pasid_state->wq);633633- spin_lock_init(&pasid_state->lock);634634-635635- mm = get_task_mm(task);636636- pasid_state->mm = mm;637637- pasid_state->device_state = dev_state;638638- pasid_state->pasid = pasid;639639- pasid_state->invalid = true; /* Mark as valid only if we are640640- done with setting up the pasid */641641- pasid_state->mn.ops = &iommu_mn;642642-643643- if (pasid_state->mm == NULL)644644- goto out_free;645645-646646- ret = mmu_notifier_register(&pasid_state->mn, mm);647647- if (ret)648648- goto out_free;649649-650650- ret = set_pasid_state(dev_state, pasid_state, pasid);651651- if (ret)652652- goto out_unregister;653653-654654- ret = amd_iommu_domain_set_gcr3(dev_state->domain, pasid,655655- __pa(pasid_state->mm->pgd));656656- if (ret)657657- goto out_clear_state;658658-659659- /* Now we are ready to handle faults */660660- pasid_state->invalid = false;661661-662662- /*663663- * Drop the reference to the mm_struct here. We rely on the664664- * mmu_notifier release call-back to inform us when the mm665665- * is going away.666666- */667667- mmput(mm);668668-669669- return 0;670670-671671-out_clear_state:672672- clear_pasid_state(dev_state, pasid);673673-674674-out_unregister:675675- mmu_notifier_unregister(&pasid_state->mn, mm);676676- mmput(mm);677677-678678-out_free:679679- free_pasid_state(pasid_state);680680-681681-out:682682- put_device_state(dev_state);683683-684684- return ret;685685-}686686-EXPORT_SYMBOL(amd_iommu_bind_pasid);687687-688688-void amd_iommu_unbind_pasid(struct pci_dev *pdev, u32 pasid)689689-{690690- struct pasid_state *pasid_state;691691- struct device_state *dev_state;692692- u32 sbdf;693693-694694- might_sleep();695695-696696- if (!amd_iommu_v2_supported())697697- return;698698-699699- sbdf = get_pci_sbdf_id(pdev);700700- dev_state = get_device_state(sbdf);701701- if (dev_state == NULL)702702- return;703703-704704- if (pasid >= dev_state->max_pasids)705705- goto out;706706-707707- pasid_state = get_pasid_state(dev_state, pasid);708708- if (pasid_state == NULL)709709- goto out;710710- /*711711- * Drop reference taken here. We are safe because we still hold712712- * the reference taken in the amd_iommu_bind_pasid function.713713- */714714- put_pasid_state(pasid_state);715715-716716- /* Clear the pasid state so that the pasid can be re-used */717717- clear_pasid_state(dev_state, pasid_state->pasid);718718-719719- /*720720- * Call mmu_notifier_unregister to drop our reference721721- * to pasid_state->mm722722- */723723- mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);724724-725725- put_pasid_state_wait(pasid_state); /* Reference taken in726726- amd_iommu_bind_pasid */727727-out:728728- /* Drop reference taken in this function */729729- put_device_state(dev_state);730730-731731- /* Drop reference taken in amd_iommu_bind_pasid */732732- put_device_state(dev_state);733733-}734734-EXPORT_SYMBOL(amd_iommu_unbind_pasid);735735-736736-int amd_iommu_init_device(struct pci_dev *pdev, int pasids)737737-{738738- struct device_state *dev_state;739739- struct iommu_group *group;740740- unsigned long flags;741741- int ret, tmp;742742- u32 sbdf;743743-744744- might_sleep();745745-746746- /*747747- * When memory encryption is active the device is likely not in a748748- * direct-mapped domain. Forbid using IOMMUv2 functionality for now.749749- */750750- if (cc_platform_has(CC_ATTR_MEM_ENCRYPT))751751- return -ENODEV;752752-753753- if (!amd_iommu_v2_supported())754754- return -ENODEV;755755-756756- if (pasids <= 0 || pasids > (PASID_MASK + 1))757757- return -EINVAL;758758-759759- sbdf = get_pci_sbdf_id(pdev);760760-761761- dev_state = kzalloc(sizeof(*dev_state), GFP_KERNEL);762762- if (dev_state == NULL)763763- return -ENOMEM;764764-765765- spin_lock_init(&dev_state->lock);766766- init_waitqueue_head(&dev_state->wq);767767- dev_state->pdev = pdev;768768- dev_state->sbdf = sbdf;769769-770770- tmp = pasids;771771- for (dev_state->pasid_levels = 0; (tmp - 1) & ~0x1ff; tmp >>= 9)772772- dev_state->pasid_levels += 1;773773-774774- atomic_set(&dev_state->count, 1);775775- dev_state->max_pasids = pasids;776776-777777- ret = -ENOMEM;778778- dev_state->states = (void *)get_zeroed_page(GFP_KERNEL);779779- if (dev_state->states == NULL)780780- goto out_free_dev_state;781781-782782- dev_state->domain = iommu_domain_alloc(&pci_bus_type);783783- if (dev_state->domain == NULL)784784- goto out_free_states;785785-786786- /* See iommu_is_default_domain() */787787- dev_state->domain->type = IOMMU_DOMAIN_IDENTITY;788788- amd_iommu_domain_direct_map(dev_state->domain);789789-790790- ret = amd_iommu_domain_enable_v2(dev_state->domain, pasids);791791- if (ret)792792- goto out_free_domain;793793-794794- group = iommu_group_get(&pdev->dev);795795- if (!group) {796796- ret = -EINVAL;797797- goto out_free_domain;798798- }799799-800800- ret = iommu_attach_group(dev_state->domain, group);801801- if (ret != 0)802802- goto out_drop_group;803803-804804- iommu_group_put(group);805805-806806- spin_lock_irqsave(&state_lock, flags);807807-808808- if (__get_device_state(sbdf) != NULL) {809809- spin_unlock_irqrestore(&state_lock, flags);810810- ret = -EBUSY;811811- goto out_free_domain;812812- }813813-814814- list_add_tail(&dev_state->list, &state_list);815815-816816- spin_unlock_irqrestore(&state_lock, flags);817817-818818- return 0;819819-820820-out_drop_group:821821- iommu_group_put(group);822822-823823-out_free_domain:824824- iommu_domain_free(dev_state->domain);825825-826826-out_free_states:827827- free_page((unsigned long)dev_state->states);828828-829829-out_free_dev_state:830830- kfree(dev_state);831831-832832- return ret;833833-}834834-EXPORT_SYMBOL(amd_iommu_init_device);835835-836836-void amd_iommu_free_device(struct pci_dev *pdev)837837-{838838- struct device_state *dev_state;839839- unsigned long flags;840840- u32 sbdf;841841-842842- if (!amd_iommu_v2_supported())843843- return;844844-845845- sbdf = get_pci_sbdf_id(pdev);846846-847847- spin_lock_irqsave(&state_lock, flags);848848-849849- dev_state = __get_device_state(sbdf);850850- if (dev_state == NULL) {851851- spin_unlock_irqrestore(&state_lock, flags);852852- return;853853- }854854-855855- list_del(&dev_state->list);856856-857857- spin_unlock_irqrestore(&state_lock, flags);858858-859859- put_device_state(dev_state);860860- free_device_state(dev_state);861861-}862862-EXPORT_SYMBOL(amd_iommu_free_device);863863-864864-int amd_iommu_set_invalid_ppr_cb(struct pci_dev *pdev,865865- amd_iommu_invalid_ppr_cb cb)866866-{867867- struct device_state *dev_state;868868- unsigned long flags;869869- u32 sbdf;870870- int ret;871871-872872- if (!amd_iommu_v2_supported())873873- return -ENODEV;874874-875875- sbdf = get_pci_sbdf_id(pdev);876876-877877- spin_lock_irqsave(&state_lock, flags);878878-879879- ret = -EINVAL;880880- dev_state = __get_device_state(sbdf);881881- if (dev_state == NULL)882882- goto out_unlock;883883-884884- dev_state->inv_ppr_cb = cb;885885-886886- ret = 0;887887-888888-out_unlock:889889- spin_unlock_irqrestore(&state_lock, flags);890890-891891- return ret;892892-}893893-EXPORT_SYMBOL(amd_iommu_set_invalid_ppr_cb);894894-895895-int amd_iommu_set_invalidate_ctx_cb(struct pci_dev *pdev,896896- amd_iommu_invalidate_ctx cb)897897-{898898- struct device_state *dev_state;899899- unsigned long flags;900900- u32 sbdf;901901- int ret;902902-903903- if (!amd_iommu_v2_supported())904904- return -ENODEV;905905-906906- sbdf = get_pci_sbdf_id(pdev);907907-908908- spin_lock_irqsave(&state_lock, flags);909909-910910- ret = -EINVAL;911911- dev_state = __get_device_state(sbdf);912912- if (dev_state == NULL)913913- goto out_unlock;914914-915915- dev_state->inv_ctx_cb = cb;916916-917917- ret = 0;918918-919919-out_unlock:920920- spin_unlock_irqrestore(&state_lock, flags);921921-922922- return ret;923923-}924924-EXPORT_SYMBOL(amd_iommu_set_invalidate_ctx_cb);925925-926926-static int __init amd_iommu_v2_init(void)927927-{928928- int ret;929929-930930- if (!amd_iommu_v2_supported()) {931931- pr_info("AMD IOMMUv2 functionality not available on this system - This is not a bug.\n");932932- /*933933- * Load anyway to provide the symbols to other modules934934- * which may use AMD IOMMUv2 optionally.935935- */936936- return 0;937937- }938938-939939- ret = -ENOMEM;940940- iommu_wq = alloc_workqueue("amd_iommu_v2", WQ_MEM_RECLAIM, 0);941941- if (iommu_wq == NULL)942942- goto out;943943-944944- amd_iommu_register_ppr_notifier(&ppr_nb);945945-946946- pr_info("AMD IOMMUv2 loaded and initialized\n");947947-948948- return 0;949949-950950-out:951951- return ret;952952-}953953-954954-static void __exit amd_iommu_v2_exit(void)955955-{956956- struct device_state *dev_state, *next;957957- unsigned long flags;958958- LIST_HEAD(freelist);959959-960960- if (!amd_iommu_v2_supported())961961- return;962962-963963- amd_iommu_unregister_ppr_notifier(&ppr_nb);964964-965965- flush_workqueue(iommu_wq);966966-967967- /*968968- * The loop below might call flush_workqueue(), so call969969- * destroy_workqueue() after it970970- */971971- spin_lock_irqsave(&state_lock, flags);972972-973973- list_for_each_entry_safe(dev_state, next, &state_list, list) {974974- WARN_ON_ONCE(1);975975-976976- put_device_state(dev_state);977977- list_del(&dev_state->list);978978- list_add_tail(&dev_state->list, &freelist);979979- }980980-981981- spin_unlock_irqrestore(&state_lock, flags);982982-983983- /*984984- * Since free_device_state waits on the count to be zero,985985- * we need to free dev_state outside the spinlock.986986- */987987- list_for_each_entry_safe(dev_state, next, &freelist, list) {988988- list_del(&dev_state->list);989989- free_device_state(dev_state);990990- }991991-992992- destroy_workqueue(iommu_wq);993993-}994994-995995-module_init(amd_iommu_v2_init);996996-module_exit(amd_iommu_v2_exit);
+85-57
drivers/iommu/apple-dart.c
···196196 * @lock: lock for hardware operations involving this dart197197 * @pgsize: pagesize supported by this DART198198 * @supports_bypass: indicates if this DART supports bypass mode199199- * @force_bypass: force bypass mode due to pagesize mismatch?200199 * @sid2group: maps stream ids to iommu_groups201200 * @iommu: iommu core device202201 */···216217 u32 pgsize;217218 u32 num_streams;218219 u32 supports_bypass : 1;219219- u32 force_bypass : 1;220220221221 struct iommu_group *sid2group[DART_MAX_STREAMS];222222 struct iommu_device iommu;···504506 apple_dart_domain_flush_tlb(to_dart_domain(domain));505507}506508507507-static void apple_dart_iotlb_sync_map(struct iommu_domain *domain,508508- unsigned long iova, size_t size)509509+static int apple_dart_iotlb_sync_map(struct iommu_domain *domain,510510+ unsigned long iova, size_t size)509511{510512 apple_dart_domain_flush_tlb(to_dart_domain(domain));513513+ return 0;511514}512515513516static phys_addr_t apple_dart_iova_to_phys(struct iommu_domain *domain,···567568 stream_map->dart->hw->invalidate_tlb(stream_map);568569}569570570570-static int apple_dart_finalize_domain(struct iommu_domain *domain,571571+static int apple_dart_finalize_domain(struct apple_dart_domain *dart_domain,571572 struct apple_dart_master_cfg *cfg)572573{573573- struct apple_dart_domain *dart_domain = to_dart_domain(domain);574574 struct apple_dart *dart = cfg->stream_maps[0].dart;575575 struct io_pgtable_cfg pgtbl_cfg;576576 int ret = 0;577577 int i, j;578578+579579+ if (dart->pgsize > PAGE_SIZE)580580+ return -EINVAL;578581579582 mutex_lock(&dart_domain->init_lock);580583···598597 .iommu_dev = dart->dev,599598 };600599601601- dart_domain->pgtbl_ops =602602- alloc_io_pgtable_ops(dart->hw->fmt, &pgtbl_cfg, domain);600600+ dart_domain->pgtbl_ops = alloc_io_pgtable_ops(dart->hw->fmt, &pgtbl_cfg,601601+ &dart_domain->domain);603602 if (!dart_domain->pgtbl_ops) {604603 ret = -ENOMEM;605604 goto done;606605 }607606608608- domain->pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;609609- domain->geometry.aperture_start = 0;610610- domain->geometry.aperture_end = (dma_addr_t)DMA_BIT_MASK(dart->ias);611611- domain->geometry.force_aperture = true;607607+ dart_domain->domain.pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;608608+ dart_domain->domain.geometry.aperture_start = 0;609609+ dart_domain->domain.geometry.aperture_end =610610+ (dma_addr_t)DMA_BIT_MASK(dart->ias);611611+ dart_domain->domain.geometry.force_aperture = true;612612613613 dart_domain->finalized = true;614614···653651 true);654652}655653656656-static int apple_dart_attach_dev(struct iommu_domain *domain,657657- struct device *dev)654654+static int apple_dart_attach_dev_paging(struct iommu_domain *domain,655655+ struct device *dev)658656{659657 int ret, i;660658 struct apple_dart_stream_map *stream_map;661659 struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev);662660 struct apple_dart_domain *dart_domain = to_dart_domain(domain);663661664664- if (cfg->stream_maps[0].dart->force_bypass &&665665- domain->type != IOMMU_DOMAIN_IDENTITY)666666- return -EINVAL;667667- if (!cfg->stream_maps[0].dart->supports_bypass &&668668- domain->type == IOMMU_DOMAIN_IDENTITY)669669- return -EINVAL;670670-671671- ret = apple_dart_finalize_domain(domain, cfg);662662+ ret = apple_dart_finalize_domain(dart_domain, cfg);672663 if (ret)673664 return ret;674665675675- switch (domain->type) {676676- default:677677- ret = apple_dart_domain_add_streams(dart_domain, cfg);678678- if (ret)679679- return ret;666666+ ret = apple_dart_domain_add_streams(dart_domain, cfg);667667+ if (ret)668668+ return ret;680669681681- for_each_stream_map(i, cfg, stream_map)682682- apple_dart_setup_translation(dart_domain, stream_map);683683- break;684684- case IOMMU_DOMAIN_BLOCKED:685685- for_each_stream_map(i, cfg, stream_map)686686- apple_dart_hw_disable_dma(stream_map);687687- break;688688- case IOMMU_DOMAIN_IDENTITY:689689- for_each_stream_map(i, cfg, stream_map)690690- apple_dart_hw_enable_bypass(stream_map);691691- break;692692- }693693-694694- return ret;670670+ for_each_stream_map(i, cfg, stream_map)671671+ apple_dart_setup_translation(dart_domain, stream_map);672672+ return 0;695673}674674+675675+static int apple_dart_attach_dev_identity(struct iommu_domain *domain,676676+ struct device *dev)677677+{678678+ struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev);679679+ struct apple_dart_stream_map *stream_map;680680+ int i;681681+682682+ if (!cfg->stream_maps[0].dart->supports_bypass)683683+ return -EINVAL;684684+685685+ for_each_stream_map(i, cfg, stream_map)686686+ apple_dart_hw_enable_bypass(stream_map);687687+ return 0;688688+}689689+690690+static const struct iommu_domain_ops apple_dart_identity_ops = {691691+ .attach_dev = apple_dart_attach_dev_identity,692692+};693693+694694+static struct iommu_domain apple_dart_identity_domain = {695695+ .type = IOMMU_DOMAIN_IDENTITY,696696+ .ops = &apple_dart_identity_ops,697697+};698698+699699+static int apple_dart_attach_dev_blocked(struct iommu_domain *domain,700700+ struct device *dev)701701+{702702+ struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev);703703+ struct apple_dart_stream_map *stream_map;704704+ int i;705705+706706+ for_each_stream_map(i, cfg, stream_map)707707+ apple_dart_hw_disable_dma(stream_map);708708+ return 0;709709+}710710+711711+static const struct iommu_domain_ops apple_dart_blocked_ops = {712712+ .attach_dev = apple_dart_attach_dev_blocked,713713+};714714+715715+static struct iommu_domain apple_dart_blocked_domain = {716716+ .type = IOMMU_DOMAIN_BLOCKED,717717+ .ops = &apple_dart_blocked_ops,718718+};696719697720static struct iommu_device *apple_dart_probe_device(struct device *dev)698721{···744717 kfree(cfg);745718}746719747747-static struct iommu_domain *apple_dart_domain_alloc(unsigned int type)720720+static struct iommu_domain *apple_dart_domain_alloc_paging(struct device *dev)748721{749722 struct apple_dart_domain *dart_domain;750750-751751- if (type != IOMMU_DOMAIN_DMA && type != IOMMU_DOMAIN_UNMANAGED &&752752- type != IOMMU_DOMAIN_IDENTITY && type != IOMMU_DOMAIN_BLOCKED)753753- return NULL;754723755724 dart_domain = kzalloc(sizeof(*dart_domain), GFP_KERNEL);756725 if (!dart_domain)···754731755732 mutex_init(&dart_domain->init_lock);756733757757- /* no need to allocate pgtbl_ops or do any other finalization steps */758758- if (type == IOMMU_DOMAIN_IDENTITY || type == IOMMU_DOMAIN_BLOCKED)759759- dart_domain->finalized = true;734734+ if (dev) {735735+ struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev);736736+ int ret;760737738738+ ret = apple_dart_finalize_domain(dart_domain, cfg);739739+ if (ret) {740740+ kfree(dart_domain);741741+ return ERR_PTR(ret);742742+ }743743+ }761744 return &dart_domain->domain;762745}763746···798769 cfg_dart = cfg->stream_maps[0].dart;799770 if (cfg_dart) {800771 if (cfg_dart->supports_bypass != dart->supports_bypass)801801- return -EINVAL;802802- if (cfg_dart->force_bypass != dart->force_bypass)803772 return -EINVAL;804773 if (cfg_dart->pgsize != dart->pgsize)805774 return -EINVAL;···940913{941914 struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev);942915943943- if (cfg->stream_maps[0].dart->force_bypass)916916+ if (cfg->stream_maps[0].dart->pgsize > PAGE_SIZE)944917 return IOMMU_DOMAIN_IDENTITY;945918 if (!cfg->stream_maps[0].dart->supports_bypass)946919 return IOMMU_DOMAIN_DMA;···974947}975948976949static const struct iommu_ops apple_dart_iommu_ops = {977977- .domain_alloc = apple_dart_domain_alloc,950950+ .identity_domain = &apple_dart_identity_domain,951951+ .blocked_domain = &apple_dart_blocked_domain,952952+ .domain_alloc_paging = apple_dart_domain_alloc_paging,978953 .probe_device = apple_dart_probe_device,979954 .release_device = apple_dart_release_device,980955 .device_group = apple_dart_device_group,···986957 .pgsize_bitmap = -1UL, /* Restricted during dart probe */987958 .owner = THIS_MODULE,988959 .default_domain_ops = &(const struct iommu_domain_ops) {989989- .attach_dev = apple_dart_attach_dev,960960+ .attach_dev = apple_dart_attach_dev_paging,990961 .map_pages = apple_dart_map_pages,991962 .unmap_pages = apple_dart_unmap_pages,992963 .flush_iotlb_all = apple_dart_flush_iotlb_all,···11401111 goto err_clk_disable;11411112 }1142111311431143- dart->force_bypass = dart->pgsize > PAGE_SIZE;11441144-11451114 ret = apple_dart_hw_reset(dart);11461115 if (ret)11471116 goto err_clk_disable;···11631136 dev_info(11641137 &pdev->dev,11651138 "DART [pagesize %x, %d streams, bypass support: %d, bypass forced: %d] initialized\n",11661166- dart->pgsize, dart->num_streams, dart->supports_bypass, dart->force_bypass);11391139+ dart->pgsize, dart->num_streams, dart->supports_bypass,11401140+ dart->pgsize > PAGE_SIZE);11671141 return 0;1168114211691143err_sysfs_remove:
+43-28
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c
···2525#define mn_to_smmu(mn) container_of(mn, struct arm_smmu_mmu_notifier, mn)26262727struct arm_smmu_bond {2828- struct iommu_sva sva;2928 struct mm_struct *mm;3029 struct arm_smmu_mmu_notifier *smmu_mn;3130 struct list_head list;3232- refcount_t refs;3331};34323533#define sva_to_bond(handle) \3634 container_of(handle, struct arm_smmu_bond, sva)37353836static DEFINE_MUTEX(sva_lock);3737+3838+/*3939+ * Write the CD to the CD tables for all masters that this domain is attached4040+ * to. Note that this is only used to update existing CD entries in the target4141+ * CD table, for which it's assumed that arm_smmu_write_ctx_desc can't fail.4242+ */4343+static void arm_smmu_update_ctx_desc_devices(struct arm_smmu_domain *smmu_domain,4444+ int ssid,4545+ struct arm_smmu_ctx_desc *cd)4646+{4747+ struct arm_smmu_master *master;4848+ unsigned long flags;4949+5050+ spin_lock_irqsave(&smmu_domain->devices_lock, flags);5151+ list_for_each_entry(master, &smmu_domain->devices, domain_head) {5252+ arm_smmu_write_ctx_desc(master, ssid, cd);5353+ }5454+ spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);5555+}39564057/*4158 * Check if the CPU ASID is available on the SMMU side. If a private context···7962 return cd;8063 }81648282- smmu_domain = container_of(cd, struct arm_smmu_domain, s1_cfg.cd);6565+ smmu_domain = container_of(cd, struct arm_smmu_domain, cd);8366 smmu = smmu_domain->smmu;84678568 ret = xa_alloc(&arm_smmu_asid_xa, &new_asid, cd,···9780 * be some overlap between use of both ASIDs, until we invalidate the9881 * TLB.9982 */100100- arm_smmu_write_ctx_desc(smmu_domain, IOMMU_NO_PASID, cd);8383+ arm_smmu_update_ctx_desc_devices(smmu_domain, IOMMU_NO_PASID, cd);1018410285 /* Invalidate TLB entries previously associated with that context */10386 arm_smmu_tlb_inv_asid(smmu, asid);···264247 * DMA may still be running. Keep the cd valid to avoid C_BAD_CD events,265248 * but disable translation.266249 */267267- arm_smmu_write_ctx_desc(smmu_domain, mm->pasid, &quiet_cd);250250+ arm_smmu_update_ctx_desc_devices(smmu_domain, mm->pasid, &quiet_cd);268251269252 arm_smmu_tlb_inv_asid(smmu_domain->smmu, smmu_mn->cd->asid);270253 arm_smmu_atc_inv_domain(smmu_domain, mm->pasid, 0, 0);···290273 struct mm_struct *mm)291274{292275 int ret;276276+ unsigned long flags;293277 struct arm_smmu_ctx_desc *cd;294278 struct arm_smmu_mmu_notifier *smmu_mn;279279+ struct arm_smmu_master *master;295280296281 list_for_each_entry(smmu_mn, &smmu_domain->mmu_notifiers, list) {297282 if (smmu_mn->mn.mm == mm) {···323304 goto err_free_cd;324305 }325306326326- ret = arm_smmu_write_ctx_desc(smmu_domain, mm->pasid, cd);307307+ spin_lock_irqsave(&smmu_domain->devices_lock, flags);308308+ list_for_each_entry(master, &smmu_domain->devices, domain_head) {309309+ ret = arm_smmu_write_ctx_desc(master, mm->pasid, cd);310310+ if (ret) {311311+ list_for_each_entry_from_reverse(master, &smmu_domain->devices, domain_head)312312+ arm_smmu_write_ctx_desc(master, mm->pasid, NULL);313313+ break;314314+ }315315+ }316316+ spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);327317 if (ret)328318 goto err_put_notifier;329319···357329 return;358330359331 list_del(&smmu_mn->list);360360- arm_smmu_write_ctx_desc(smmu_domain, mm->pasid, NULL);332332+333333+ arm_smmu_update_ctx_desc_devices(smmu_domain, mm->pasid, NULL);361334362335 /*363336 * If we went through clear(), we've already invalidated, and no···374345 arm_smmu_free_shared_cd(cd);375346}376347377377-static struct iommu_sva *378378-__arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm)348348+static int __arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm)379349{380350 int ret;381351 struct arm_smmu_bond *bond;···383355 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);384356385357 if (!master || !master->sva_enabled)386386- return ERR_PTR(-ENODEV);387387-388388- /* If bind() was already called for this {dev, mm} pair, reuse it. */389389- list_for_each_entry(bond, &master->bonds, list) {390390- if (bond->mm == mm) {391391- refcount_inc(&bond->refs);392392- return &bond->sva;393393- }394394- }358358+ return -ENODEV;395359396360 bond = kzalloc(sizeof(*bond), GFP_KERNEL);397361 if (!bond)398398- return ERR_PTR(-ENOMEM);362362+ return -ENOMEM;399363400364 bond->mm = mm;401401- bond->sva.dev = dev;402402- refcount_set(&bond->refs, 1);403365404366 bond->smmu_mn = arm_smmu_mmu_notifier_get(smmu_domain, mm);405367 if (IS_ERR(bond->smmu_mn)) {···398380 }399381400382 list_add(&bond->list, &master->bonds);401401- return &bond->sva;383383+ return 0;402384403385err_free_bond:404386 kfree(bond);405405- return ERR_PTR(ret);387387+ return ret;406388}407389408390bool arm_smmu_sva_supported(struct arm_smmu_device *smmu)···568550 }569551 }570552571571- if (!WARN_ON(!bond) && refcount_dec_and_test(&bond->refs)) {553553+ if (!WARN_ON(!bond)) {572554 list_del(&bond->list);573555 arm_smmu_mmu_notifier_put(bond->smmu_mn);574556 kfree(bond);···580562 struct device *dev, ioasid_t id)581563{582564 int ret = 0;583583- struct iommu_sva *handle;584565 struct mm_struct *mm = domain->mm;585566586567 mutex_lock(&sva_lock);587587- handle = __arm_smmu_sva_bind(dev, mm);588588- if (IS_ERR(handle))589589- ret = PTR_ERR(handle);568568+ ret = __arm_smmu_sva_bind(dev, mm);590569 mutex_unlock(&sva_lock);591570592571 return ret;
+123-130
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
···971971 arm_smmu_cmdq_issue_cmd_with_sync(smmu, &cmd);972972}973973974974-static void arm_smmu_sync_cd(struct arm_smmu_domain *smmu_domain,974974+static void arm_smmu_sync_cd(struct arm_smmu_master *master,975975 int ssid, bool leaf)976976{977977 size_t i;978978- unsigned long flags;979979- struct arm_smmu_master *master;980978 struct arm_smmu_cmdq_batch cmds;981981- struct arm_smmu_device *smmu = smmu_domain->smmu;979979+ struct arm_smmu_device *smmu = master->smmu;982980 struct arm_smmu_cmdq_ent cmd = {983981 .opcode = CMDQ_OP_CFGI_CD,984982 .cfgi = {···986988 };987989988990 cmds.num = 0;989989-990990- spin_lock_irqsave(&smmu_domain->devices_lock, flags);991991- list_for_each_entry(master, &smmu_domain->devices, domain_head) {992992- for (i = 0; i < master->num_streams; i++) {993993- cmd.cfgi.sid = master->streams[i].id;994994- arm_smmu_cmdq_batch_add(smmu, &cmds, &cmd);995995- }991991+ for (i = 0; i < master->num_streams; i++) {992992+ cmd.cfgi.sid = master->streams[i].id;993993+ arm_smmu_cmdq_batch_add(smmu, &cmds, &cmd);996994 }997997- spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);998995999996 arm_smmu_cmdq_batch_submit(smmu, &cmds);1000997}···10191026 WRITE_ONCE(*dst, cpu_to_le64(val));10201027}1021102810221022-static __le64 *arm_smmu_get_cd_ptr(struct arm_smmu_domain *smmu_domain,10231023- u32 ssid)10291029+static __le64 *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, u32 ssid)10241030{10251031 __le64 *l1ptr;10261032 unsigned int idx;10271033 struct arm_smmu_l1_ctx_desc *l1_desc;10281028- struct arm_smmu_device *smmu = smmu_domain->smmu;10291029- struct arm_smmu_ctx_desc_cfg *cdcfg = &smmu_domain->s1_cfg.cdcfg;10341034+ struct arm_smmu_device *smmu = master->smmu;10351035+ struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table;1030103610311031- if (smmu_domain->s1_cfg.s1fmt == STRTAB_STE_0_S1FMT_LINEAR)10321032- return cdcfg->cdtab + ssid * CTXDESC_CD_DWORDS;10371037+ if (cd_table->s1fmt == STRTAB_STE_0_S1FMT_LINEAR)10381038+ return cd_table->cdtab + ssid * CTXDESC_CD_DWORDS;1033103910341040 idx = ssid >> CTXDESC_SPLIT;10351035- l1_desc = &cdcfg->l1_desc[idx];10411041+ l1_desc = &cd_table->l1_desc[idx];10361042 if (!l1_desc->l2ptr) {10371043 if (arm_smmu_alloc_cd_leaf_table(smmu, l1_desc))10381044 return NULL;1039104510401040- l1ptr = cdcfg->cdtab + idx * CTXDESC_L1_DESC_DWORDS;10461046+ l1ptr = cd_table->cdtab + idx * CTXDESC_L1_DESC_DWORDS;10411047 arm_smmu_write_cd_l1_desc(l1ptr, l1_desc);10421048 /* An invalid L1CD can be cached */10431043- arm_smmu_sync_cd(smmu_domain, ssid, false);10491049+ arm_smmu_sync_cd(master, ssid, false);10441050 }10451051 idx = ssid & (CTXDESC_L2_ENTRIES - 1);10461052 return l1_desc->l2ptr + idx * CTXDESC_CD_DWORDS;10471053}1048105410491049-int arm_smmu_write_ctx_desc(struct arm_smmu_domain *smmu_domain, int ssid,10551055+int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid,10501056 struct arm_smmu_ctx_desc *cd)10511057{10521058 /*···10621070 u64 val;10631071 bool cd_live;10641072 __le64 *cdptr;10731073+ struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table;1065107410661066- if (WARN_ON(ssid >= (1 << smmu_domain->s1_cfg.s1cdmax)))10751075+ if (WARN_ON(ssid >= (1 << cd_table->s1cdmax)))10671076 return -E2BIG;1068107710691069- cdptr = arm_smmu_get_cd_ptr(smmu_domain, ssid);10781078+ cdptr = arm_smmu_get_cd_ptr(master, ssid);10701079 if (!cdptr)10711080 return -ENOMEM;10721081···10911098 cdptr[3] = cpu_to_le64(cd->mair);1092109910931100 /*10941094- * STE is live, and the SMMU might read dwords of this CD in any11011101+ * STE may be live, and the SMMU might read dwords of this CD in any10951102 * order. Ensure that it observes valid values before reading10961103 * V=1.10971104 */10981098- arm_smmu_sync_cd(smmu_domain, ssid, true);11051105+ arm_smmu_sync_cd(master, ssid, true);1099110611001107 val = cd->tcr |11011108#ifdef __BIG_ENDIAN···11071114 FIELD_PREP(CTXDESC_CD_0_ASID, cd->asid) |11081115 CTXDESC_CD_0_V;1109111611101110- if (smmu_domain->stall_enabled)11171117+ if (cd_table->stall_enabled)11111118 val |= CTXDESC_CD_0_S;11121119 }11131120···11211128 * without first making the structure invalid.11221129 */11231130 WRITE_ONCE(cdptr[0], cpu_to_le64(val));11241124- arm_smmu_sync_cd(smmu_domain, ssid, true);11311131+ arm_smmu_sync_cd(master, ssid, true);11251132 return 0;11261133}1127113411281128-static int arm_smmu_alloc_cd_tables(struct arm_smmu_domain *smmu_domain)11351135+static int arm_smmu_alloc_cd_tables(struct arm_smmu_master *master)11291136{11301137 int ret;11311138 size_t l1size;11321139 size_t max_contexts;11331133- struct arm_smmu_device *smmu = smmu_domain->smmu;11341134- struct arm_smmu_s1_cfg *cfg = &smmu_domain->s1_cfg;11351135- struct arm_smmu_ctx_desc_cfg *cdcfg = &cfg->cdcfg;11401140+ struct arm_smmu_device *smmu = master->smmu;11411141+ struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table;1136114211371137- max_contexts = 1 << cfg->s1cdmax;11431143+ cd_table->stall_enabled = master->stall_enabled;11441144+ cd_table->s1cdmax = master->ssid_bits;11451145+ max_contexts = 1 << cd_table->s1cdmax;1138114611391147 if (!(smmu->features & ARM_SMMU_FEAT_2_LVL_CDTAB) ||11401148 max_contexts <= CTXDESC_L2_ENTRIES) {11411141- cfg->s1fmt = STRTAB_STE_0_S1FMT_LINEAR;11421142- cdcfg->num_l1_ents = max_contexts;11491149+ cd_table->s1fmt = STRTAB_STE_0_S1FMT_LINEAR;11501150+ cd_table->num_l1_ents = max_contexts;1143115111441152 l1size = max_contexts * (CTXDESC_CD_DWORDS << 3);11451153 } else {11461146- cfg->s1fmt = STRTAB_STE_0_S1FMT_64K_L2;11471147- cdcfg->num_l1_ents = DIV_ROUND_UP(max_contexts,11541154+ cd_table->s1fmt = STRTAB_STE_0_S1FMT_64K_L2;11551155+ cd_table->num_l1_ents = DIV_ROUND_UP(max_contexts,11481156 CTXDESC_L2_ENTRIES);1149115711501150- cdcfg->l1_desc = devm_kcalloc(smmu->dev, cdcfg->num_l1_ents,11511151- sizeof(*cdcfg->l1_desc),11581158+ cd_table->l1_desc = devm_kcalloc(smmu->dev, cd_table->num_l1_ents,11591159+ sizeof(*cd_table->l1_desc),11521160 GFP_KERNEL);11531153- if (!cdcfg->l1_desc)11611161+ if (!cd_table->l1_desc)11541162 return -ENOMEM;1155116311561156- l1size = cdcfg->num_l1_ents * (CTXDESC_L1_DESC_DWORDS << 3);11641164+ l1size = cd_table->num_l1_ents * (CTXDESC_L1_DESC_DWORDS << 3);11571165 }1158116611591159- cdcfg->cdtab = dmam_alloc_coherent(smmu->dev, l1size, &cdcfg->cdtab_dma,11671167+ cd_table->cdtab = dmam_alloc_coherent(smmu->dev, l1size, &cd_table->cdtab_dma,11601168 GFP_KERNEL);11611161- if (!cdcfg->cdtab) {11691169+ if (!cd_table->cdtab) {11621170 dev_warn(smmu->dev, "failed to allocate context descriptor\n");11631171 ret = -ENOMEM;11641172 goto err_free_l1;···11681174 return 0;1169117511701176err_free_l1:11711171- if (cdcfg->l1_desc) {11721172- devm_kfree(smmu->dev, cdcfg->l1_desc);11731173- cdcfg->l1_desc = NULL;11771177+ if (cd_table->l1_desc) {11781178+ devm_kfree(smmu->dev, cd_table->l1_desc);11791179+ cd_table->l1_desc = NULL;11741180 }11751181 return ret;11761182}1177118311781178-static void arm_smmu_free_cd_tables(struct arm_smmu_domain *smmu_domain)11841184+static void arm_smmu_free_cd_tables(struct arm_smmu_master *master)11791185{11801186 int i;11811187 size_t size, l1size;11821182- struct arm_smmu_device *smmu = smmu_domain->smmu;11831183- struct arm_smmu_ctx_desc_cfg *cdcfg = &smmu_domain->s1_cfg.cdcfg;11881188+ struct arm_smmu_device *smmu = master->smmu;11891189+ struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table;1184119011851185- if (cdcfg->l1_desc) {11911191+ if (cd_table->l1_desc) {11861192 size = CTXDESC_L2_ENTRIES * (CTXDESC_CD_DWORDS << 3);1187119311881188- for (i = 0; i < cdcfg->num_l1_ents; i++) {11891189- if (!cdcfg->l1_desc[i].l2ptr)11941194+ for (i = 0; i < cd_table->num_l1_ents; i++) {11951195+ if (!cd_table->l1_desc[i].l2ptr)11901196 continue;1191119711921198 dmam_free_coherent(smmu->dev, size,11931193- cdcfg->l1_desc[i].l2ptr,11941194- cdcfg->l1_desc[i].l2ptr_dma);11991199+ cd_table->l1_desc[i].l2ptr,12001200+ cd_table->l1_desc[i].l2ptr_dma);11951201 }11961196- devm_kfree(smmu->dev, cdcfg->l1_desc);11971197- cdcfg->l1_desc = NULL;12021202+ devm_kfree(smmu->dev, cd_table->l1_desc);12031203+ cd_table->l1_desc = NULL;1198120411991199- l1size = cdcfg->num_l1_ents * (CTXDESC_L1_DESC_DWORDS << 3);12051205+ l1size = cd_table->num_l1_ents * (CTXDESC_L1_DESC_DWORDS << 3);12001206 } else {12011201- l1size = cdcfg->num_l1_ents * (CTXDESC_CD_DWORDS << 3);12071207+ l1size = cd_table->num_l1_ents * (CTXDESC_CD_DWORDS << 3);12021208 }1203120912041204- dmam_free_coherent(smmu->dev, l1size, cdcfg->cdtab, cdcfg->cdtab_dma);12051205- cdcfg->cdtab_dma = 0;12061206- cdcfg->cdtab = NULL;12101210+ dmam_free_coherent(smmu->dev, l1size, cd_table->cdtab, cd_table->cdtab_dma);12111211+ cd_table->cdtab_dma = 0;12121212+ cd_table->cdtab = NULL;12071213}1208121412091215bool arm_smmu_free_asid(struct arm_smmu_ctx_desc *cd)···12701276 u64 val = le64_to_cpu(dst[0]);12711277 bool ste_live = false;12721278 struct arm_smmu_device *smmu = NULL;12731273- struct arm_smmu_s1_cfg *s1_cfg = NULL;12791279+ struct arm_smmu_ctx_desc_cfg *cd_table = NULL;12741280 struct arm_smmu_s2_cfg *s2_cfg = NULL;12751281 struct arm_smmu_domain *smmu_domain = NULL;12761282 struct arm_smmu_cmdq_ent prefetch_cmd = {···12881294 if (smmu_domain) {12891295 switch (smmu_domain->stage) {12901296 case ARM_SMMU_DOMAIN_S1:12911291- s1_cfg = &smmu_domain->s1_cfg;12971297+ cd_table = &master->cd_table;12921298 break;12931299 case ARM_SMMU_DOMAIN_S2:12941300 case ARM_SMMU_DOMAIN_NESTED:···13191325 val = STRTAB_STE_0_V;1320132613211327 /* Bypass/fault */13221322- if (!smmu_domain || !(s1_cfg || s2_cfg)) {13281328+ if (!smmu_domain || !(cd_table || s2_cfg)) {13231329 if (!smmu_domain && disable_bypass)13241330 val |= FIELD_PREP(STRTAB_STE_0_CFG, STRTAB_STE_0_CFG_ABORT);13251331 else···13381344 return;13391345 }1340134613411341- if (s1_cfg) {13471347+ if (cd_table) {13421348 u64 strw = smmu->features & ARM_SMMU_FEAT_E2H ?13431349 STRTAB_STE_1_STRW_EL2 : STRTAB_STE_1_STRW_NSEL1;13441350···13541360 !master->stall_enabled)13551361 dst[1] |= cpu_to_le64(STRTAB_STE_1_S1STALLD);1356136213571357- val |= (s1_cfg->cdcfg.cdtab_dma & STRTAB_STE_0_S1CTXPTR_MASK) |13631363+ val |= (cd_table->cdtab_dma & STRTAB_STE_0_S1CTXPTR_MASK) |13581364 FIELD_PREP(STRTAB_STE_0_CFG, STRTAB_STE_0_CFG_S1_TRANS) |13591359- FIELD_PREP(STRTAB_STE_0_S1CDMAX, s1_cfg->s1cdmax) |13601360- FIELD_PREP(STRTAB_STE_0_S1FMT, s1_cfg->s1fmt);13651365+ FIELD_PREP(STRTAB_STE_0_S1CDMAX, cd_table->s1cdmax) |13661366+ FIELD_PREP(STRTAB_STE_0_S1FMT, cd_table->s1fmt);13611367 }1362136813631369 if (s2_cfg) {···18631869 * careful, 007.18641870 */18651871 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {18661866- arm_smmu_tlb_inv_asid(smmu, smmu_domain->s1_cfg.cd.asid);18721872+ arm_smmu_tlb_inv_asid(smmu, smmu_domain->cd.asid);18671873 } else {18681874 cmd.opcode = CMDQ_OP_TLBI_S12_VMALL;18691875 cmd.tlbi.vmid = smmu_domain->s2_cfg.vmid;···19561962 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {19571963 cmd.opcode = smmu_domain->smmu->features & ARM_SMMU_FEAT_E2H ?19581964 CMDQ_OP_TLBI_EL2_VA : CMDQ_OP_TLBI_NH_VA;19591959- cmd.tlbi.asid = smmu_domain->s1_cfg.cd.asid;19651965+ cmd.tlbi.asid = smmu_domain->cd.asid;19601966 } else {19611967 cmd.opcode = CMDQ_OP_TLBI_S2_IPA;19621968 cmd.tlbi.vmid = smmu_domain->s2_cfg.vmid;···2061206720622068 free_io_pgtable_ops(smmu_domain->pgtbl_ops);2063206920642064- /* Free the CD and ASID, if we allocated them */20702070+ /* Free the ASID or VMID */20652071 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {20662066- struct arm_smmu_s1_cfg *cfg = &smmu_domain->s1_cfg;20672067-20682072 /* Prevent SVA from touching the CD while we're freeing it */20692073 mutex_lock(&arm_smmu_asid_lock);20702070- if (cfg->cdcfg.cdtab)20712071- arm_smmu_free_cd_tables(smmu_domain);20722072- arm_smmu_free_asid(&cfg->cd);20742074+ arm_smmu_free_asid(&smmu_domain->cd);20732075 mutex_unlock(&arm_smmu_asid_lock);20742076 } else {20752077 struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg;···20772087}2078208820792089static int arm_smmu_domain_finalise_s1(struct arm_smmu_domain *smmu_domain,20802080- struct arm_smmu_master *master,20812090 struct io_pgtable_cfg *pgtbl_cfg)20822091{20832092 int ret;20842093 u32 asid;20852094 struct arm_smmu_device *smmu = smmu_domain->smmu;20862086- struct arm_smmu_s1_cfg *cfg = &smmu_domain->s1_cfg;20952095+ struct arm_smmu_ctx_desc *cd = &smmu_domain->cd;20872096 typeof(&pgtbl_cfg->arm_lpae_s1_cfg.tcr) tcr = &pgtbl_cfg->arm_lpae_s1_cfg.tcr;2088209720892089- refcount_set(&cfg->cd.refs, 1);20982098+ refcount_set(&cd->refs, 1);2090209920912100 /* Prevent SVA from modifying the ASID until it is written to the CD */20922101 mutex_lock(&arm_smmu_asid_lock);20932093- ret = xa_alloc(&arm_smmu_asid_xa, &asid, &cfg->cd,21022102+ ret = xa_alloc(&arm_smmu_asid_xa, &asid, cd,20942103 XA_LIMIT(1, (1 << smmu->asid_bits) - 1), GFP_KERNEL);20952104 if (ret)20962105 goto out_unlock;2097210620982098- cfg->s1cdmax = master->ssid_bits;20992099-21002100- smmu_domain->stall_enabled = master->stall_enabled;21012101-21022102- ret = arm_smmu_alloc_cd_tables(smmu_domain);21032103- if (ret)21042104- goto out_free_asid;21052105-21062106- cfg->cd.asid = (u16)asid;21072107- cfg->cd.ttbr = pgtbl_cfg->arm_lpae_s1_cfg.ttbr;21082108- cfg->cd.tcr = FIELD_PREP(CTXDESC_CD_0_TCR_T0SZ, tcr->tsz) |21072107+ cd->asid = (u16)asid;21082108+ cd->ttbr = pgtbl_cfg->arm_lpae_s1_cfg.ttbr;21092109+ cd->tcr = FIELD_PREP(CTXDESC_CD_0_TCR_T0SZ, tcr->tsz) |21092110 FIELD_PREP(CTXDESC_CD_0_TCR_TG0, tcr->tg) |21102111 FIELD_PREP(CTXDESC_CD_0_TCR_IRGN0, tcr->irgn) |21112112 FIELD_PREP(CTXDESC_CD_0_TCR_ORGN0, tcr->orgn) |21122113 FIELD_PREP(CTXDESC_CD_0_TCR_SH0, tcr->sh) |21132114 FIELD_PREP(CTXDESC_CD_0_TCR_IPS, tcr->ips) |21142115 CTXDESC_CD_0_TCR_EPD1 | CTXDESC_CD_0_AA64;21152115- cfg->cd.mair = pgtbl_cfg->arm_lpae_s1_cfg.mair;21162116-21172117- /*21182118- * Note that this will end up calling arm_smmu_sync_cd() before21192119- * the master has been added to the devices list for this domain.21202120- * This isn't an issue because the STE hasn't been installed yet.21212121- */21222122- ret = arm_smmu_write_ctx_desc(smmu_domain, IOMMU_NO_PASID, &cfg->cd);21232123- if (ret)21242124- goto out_free_cd_tables;21162116+ cd->mair = pgtbl_cfg->arm_lpae_s1_cfg.mair;2125211721262118 mutex_unlock(&arm_smmu_asid_lock);21272119 return 0;2128212021292129-out_free_cd_tables:21302130- arm_smmu_free_cd_tables(smmu_domain);21312131-out_free_asid:21322132- arm_smmu_free_asid(&cfg->cd);21332121out_unlock:21342122 mutex_unlock(&arm_smmu_asid_lock);21352123 return ret;21362124}2137212521382126static int arm_smmu_domain_finalise_s2(struct arm_smmu_domain *smmu_domain,21392139- struct arm_smmu_master *master,21402127 struct io_pgtable_cfg *pgtbl_cfg)21412128{21422129 int vmid;···21402173 return 0;21412174}2142217521432143-static int arm_smmu_domain_finalise(struct iommu_domain *domain,21442144- struct arm_smmu_master *master)21762176+static int arm_smmu_domain_finalise(struct iommu_domain *domain)21452177{21462178 int ret;21472179 unsigned long ias, oas;···21482182 struct io_pgtable_cfg pgtbl_cfg;21492183 struct io_pgtable_ops *pgtbl_ops;21502184 int (*finalise_stage_fn)(struct arm_smmu_domain *,21512151- struct arm_smmu_master *,21522185 struct io_pgtable_cfg *);21532186 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);21542187 struct arm_smmu_device *smmu = smmu_domain->smmu;···21992234 domain->geometry.aperture_end = (1UL << pgtbl_cfg.ias) - 1;22002235 domain->geometry.force_aperture = true;2201223622022202- ret = finalise_stage_fn(smmu_domain, master, &pgtbl_cfg);22372237+ ret = finalise_stage_fn(smmu_domain, &pgtbl_cfg);22032238 if (ret < 0) {22042239 free_io_pgtable_ops(pgtbl_ops);22052240 return ret;···23682403 master->domain = NULL;23692404 master->ats_enabled = false;23702405 arm_smmu_install_ste_for_dev(master);24062406+ /*24072407+ * Clearing the CD entry isn't strictly required to detach the domain24082408+ * since the table is uninstalled anyway, but it helps avoid confusion24092409+ * in the call to arm_smmu_write_ctx_desc on the next attach (which24102410+ * expects the entry to be empty).24112411+ */24122412+ if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1 && master->cd_table.cdtab)24132413+ arm_smmu_write_ctx_desc(master, IOMMU_NO_PASID, NULL);23712414}2372241523732416static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)···2409243624102437 if (!smmu_domain->smmu) {24112438 smmu_domain->smmu = smmu;24122412- ret = arm_smmu_domain_finalise(domain, master);24132413- if (ret) {24392439+ ret = arm_smmu_domain_finalise(domain);24402440+ if (ret)24142441 smmu_domain->smmu = NULL;24152415- goto out_unlock;24162416- }24172417- } else if (smmu_domain->smmu != smmu) {24422442+ } else if (smmu_domain->smmu != smmu)24182443 ret = -EINVAL;24192419- goto out_unlock;24202420- } else if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1 &&24212421- master->ssid_bits != smmu_domain->s1_cfg.s1cdmax) {24222422- ret = -EINVAL;24232423- goto out_unlock;24242424- } else if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1 &&24252425- smmu_domain->stall_enabled != master->stall_enabled) {24262426- ret = -EINVAL;24272427- goto out_unlock;24282428- }24442444+24452445+ mutex_unlock(&smmu_domain->init_mutex);24462446+ if (ret)24472447+ return ret;2429244824302449 master->domain = smmu_domain;24312450···24312466 if (smmu_domain->stage != ARM_SMMU_DOMAIN_BYPASS)24322467 master->ats_enabled = arm_smmu_ats_supported(master);2433246824342434- arm_smmu_install_ste_for_dev(master);24352435-24362469 spin_lock_irqsave(&smmu_domain->devices_lock, flags);24372470 list_add(&master->domain_head, &smmu_domain->devices);24382471 spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);2439247224402440- arm_smmu_enable_ats(master);24732473+ if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {24742474+ if (!master->cd_table.cdtab) {24752475+ ret = arm_smmu_alloc_cd_tables(master);24762476+ if (ret) {24772477+ master->domain = NULL;24782478+ goto out_list_del;24792479+ }24802480+ }2441248124422442-out_unlock:24432443- mutex_unlock(&smmu_domain->init_mutex);24822482+ /*24832483+ * Prevent SVA from concurrently modifying the CD or writing to24842484+ * the CD entry24852485+ */24862486+ mutex_lock(&arm_smmu_asid_lock);24872487+ ret = arm_smmu_write_ctx_desc(master, IOMMU_NO_PASID, &smmu_domain->cd);24882488+ mutex_unlock(&arm_smmu_asid_lock);24892489+ if (ret) {24902490+ master->domain = NULL;24912491+ goto out_list_del;24922492+ }24932493+ }24942494+24952495+ arm_smmu_install_ste_for_dev(master);24962496+24972497+ arm_smmu_enable_ats(master);24982498+ return 0;24992499+25002500+out_list_del:25012501+ spin_lock_irqsave(&smmu_domain->devices_lock, flags);25022502+ list_del(&master->domain_head);25032503+ spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);25042504+24442505 return ret;24452506}24462507···27112720 arm_smmu_detach_dev(master);27122721 arm_smmu_disable_pasid(master);27132722 arm_smmu_remove_master(master);27232723+ if (master->cd_table.cdtab)27242724+ arm_smmu_free_cd_tables(master);27142725 kfree(master);27152726}27162727
+8-9
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h
···595595 dma_addr_t cdtab_dma;596596 struct arm_smmu_l1_ctx_desc *l1_desc;597597 unsigned int num_l1_ents;598598-};599599-600600-struct arm_smmu_s1_cfg {601601- struct arm_smmu_ctx_desc_cfg cdcfg;602602- struct arm_smmu_ctx_desc cd;603598 u8 s1fmt;599599+ /* log2 of the maximum number of CDs supported by this table */604600 u8 s1cdmax;601601+ /* Whether CD entries in this table have the stall bit set. */602602+ u8 stall_enabled:1;605603};606604607605struct arm_smmu_s2_cfg {···695697 struct arm_smmu_domain *domain;696698 struct list_head domain_head;697699 struct arm_smmu_stream *streams;700700+ /* Locked by the iommu core using the group mutex */701701+ struct arm_smmu_ctx_desc_cfg cd_table;698702 unsigned int num_streams;699703 bool ats_enabled;700704 bool stall_enabled;···719719 struct mutex init_mutex; /* Protects smmu pointer */720720721721 struct io_pgtable_ops *pgtbl_ops;722722- bool stall_enabled;723722 atomic_t nr_ats_masters;724723725724 enum arm_smmu_domain_stage stage;726725 union {727727- struct arm_smmu_s1_cfg s1_cfg;728728- struct arm_smmu_s2_cfg s2_cfg;726726+ struct arm_smmu_ctx_desc cd;727727+ struct arm_smmu_s2_cfg s2_cfg;729728 };730729731730 struct iommu_domain domain;···744745extern struct mutex arm_smmu_asid_lock;745746extern struct arm_smmu_ctx_desc quiet_cd;746747747747-int arm_smmu_write_ctx_desc(struct arm_smmu_domain *smmu_domain, int ssid,748748+int arm_smmu_write_ctx_desc(struct arm_smmu_master *smmu_master, int ssid,748749 struct arm_smmu_ctx_desc *cd);749750void arm_smmu_tlb_inv_asid(struct arm_smmu_device *smmu, u16 asid);750751void arm_smmu_tlb_inv_range_asid(unsigned long iova, size_t size, int asid,
···196196{197197 struct fsl_dma_domain *dma_domain;198198199199+ /*200200+ * FIXME: This isn't creating an unmanaged domain since the201201+ * default_domain_ops do not have any map/unmap function it doesn't meet202202+ * the requirements for __IOMMU_DOMAIN_PAGING. The only purpose seems to203203+ * allow drivers/soc/fsl/qbman/qman_portal.c to do204204+ * fsl_pamu_configure_l1_stash()205205+ */199206 if (type != IOMMU_DOMAIN_UNMANAGED)200207 return NULL;201208···290283 return ret;291284}292285293293-static void fsl_pamu_set_platform_dma(struct device *dev)286286+/*287287+ * FIXME: fsl/pamu is completely broken in terms of how it works with the iommu288288+ * API. Immediately after probe the HW is left in an IDENTITY translation and289289+ * the driver provides a non-working UNMANAGED domain that it can switch over290290+ * to. However it cannot switch back to an IDENTITY translation, instead it291291+ * switches to what looks like BLOCKING.292292+ */293293+static int fsl_pamu_platform_attach(struct iommu_domain *platform_domain,294294+ struct device *dev)294295{295296 struct iommu_domain *domain = iommu_get_domain_for_dev(dev);296296- struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);297297+ struct fsl_dma_domain *dma_domain;297298 const u32 *prop;298299 int len;299300 struct pci_dev *pdev = NULL;300301 struct pci_controller *pci_ctl;302302+303303+ /*304304+ * Hack to keep things working as they always have, only leaving an305305+ * UNMANAGED domain makes it BLOCKING.306306+ */307307+ if (domain == platform_domain || !domain ||308308+ domain->type != IOMMU_DOMAIN_UNMANAGED)309309+ return 0;310310+311311+ dma_domain = to_fsl_dma_domain(domain);301312302313 /*303314 * Use LIODN of the PCI controller while detaching a···337312 detach_device(dev, dma_domain);338313 else339314 pr_debug("missing fsl,liodn property at %pOF\n", dev->of_node);315315+ return 0;340316}317317+318318+static struct iommu_domain_ops fsl_pamu_platform_ops = {319319+ .attach_dev = fsl_pamu_platform_attach,320320+};321321+322322+static struct iommu_domain fsl_pamu_platform_domain = {323323+ .type = IOMMU_DOMAIN_PLATFORM,324324+ .ops = &fsl_pamu_platform_ops,325325+};341326342327/* Set the domain stash attribute */343328int fsl_pamu_configure_l1_stash(struct iommu_domain *domain, u32 cpu)···430395}431396432397static const struct iommu_ops fsl_pamu_ops = {398398+ .default_domain = &fsl_pamu_platform_domain,433399 .capable = fsl_pamu_capable,434400 .domain_alloc = fsl_pamu_domain_alloc,435401 .probe_device = fsl_pamu_probe_device,436402 .device_group = fsl_pamu_device_group,437437- .set_platform_dma_ops = fsl_pamu_set_platform_dma,438403 .default_domain_ops = &(const struct iommu_domain_ops) {439404 .attach_dev = fsl_pamu_attach_device,440405 .iova_to_phys = fsl_pamu_iova_to_phys,
+174-41
drivers/iommu/intel/debugfs.c
···111111 IOMMU_REGSET_ENTRY(VCRSP),112112};113113114114+static struct dentry *intel_iommu_debug;115115+114116static int iommu_regset_show(struct seq_file *m, void *unused)115117{116118 struct dmar_drhd_unit *drhd;···313311static inline void314312dump_page_info(struct seq_file *m, unsigned long iova, u64 *path)315313{316316- seq_printf(m, "0x%013lx |\t0x%016llx\t0x%016llx\t0x%016llx\t0x%016llx\t0x%016llx\n",317317- iova >> VTD_PAGE_SHIFT, path[5], path[4],318318- path[3], path[2], path[1]);314314+ seq_printf(m, "0x%013lx |\t0x%016llx\t0x%016llx\t0x%016llx",315315+ iova >> VTD_PAGE_SHIFT, path[5], path[4], path[3]);316316+ if (path[2]) {317317+ seq_printf(m, "\t0x%016llx", path[2]);318318+ if (path[1])319319+ seq_printf(m, "\t0x%016llx", path[1]);320320+ }321321+ seq_putc(m, '\n');319322}320323321324static void pgtable_walk_level(struct seq_file *m, struct dma_pte *pde,···347340 }348341}349342350350-static int __show_device_domain_translation(struct device *dev, void *data)343343+static int domain_translation_struct_show(struct seq_file *m,344344+ struct device_domain_info *info,345345+ ioasid_t pasid)351346{352352- struct dmar_domain *domain;353353- struct seq_file *m = data;354354- u64 path[6] = { 0 };347347+ bool scalable, found = false;348348+ struct dmar_drhd_unit *drhd;349349+ struct intel_iommu *iommu;350350+ u16 devfn, bus, seg;355351356356- domain = to_dmar_domain(iommu_get_domain_for_dev(dev));357357- if (!domain)358358- return 0;352352+ bus = info->bus;353353+ devfn = info->devfn;354354+ seg = info->segment;359355360360- seq_printf(m, "Device %s @0x%llx\n", dev_name(dev),361361- (u64)virt_to_phys(domain->pgd));362362- seq_puts(m, "IOVA_PFN\t\tPML5E\t\t\tPML4E\t\t\tPDPE\t\t\tPDE\t\t\tPTE\n");356356+ rcu_read_lock();357357+ for_each_active_iommu(iommu, drhd) {358358+ struct context_entry *context;359359+ u64 pgd, path[6] = { 0 };360360+ u32 sts, agaw;363361364364- pgtable_walk_level(m, domain->pgd, domain->agaw + 2, 0, path);365365- seq_putc(m, '\n');362362+ if (seg != iommu->segment)363363+ continue;366364367367- /* Don't iterate */368368- return 1;369369-}365365+ sts = dmar_readl(iommu->reg + DMAR_GSTS_REG);366366+ if (!(sts & DMA_GSTS_TES)) {367367+ seq_printf(m, "DMA Remapping is not enabled on %s\n",368368+ iommu->name);369369+ continue;370370+ }371371+ if (dmar_readq(iommu->reg + DMAR_RTADDR_REG) & DMA_RTADDR_SMT)372372+ scalable = true;373373+ else374374+ scalable = false;370375371371-static int show_device_domain_translation(struct device *dev, void *data)372372-{373373- struct iommu_group *group;374374-375375- group = iommu_group_get(dev);376376- if (group) {377376 /*378378- * The group->mutex is held across the callback, which will379379- * block calls to iommu_attach/detach_group/device. Hence,377377+ * The iommu->lock is held across the callback, which will378378+ * block calls to domain_attach/domain_detach. Hence,380379 * the domain of the device will not change during traversal.381380 *382382- * All devices in an iommu group share a single domain, hence383383- * we only dump the domain of the first device. Even though,384384- * this code still possibly races with the iommu_unmap()381381+ * Traversing page table possibly races with the iommu_unmap()385382 * interface. This could be solved by RCU-freeing the page386383 * table pages in the iommu_unmap() path.387384 */388388- iommu_group_for_each_dev(group, data,389389- __show_device_domain_translation);390390- iommu_group_put(group);385385+ spin_lock(&iommu->lock);386386+387387+ context = iommu_context_addr(iommu, bus, devfn, 0);388388+ if (!context || !context_present(context))389389+ goto iommu_unlock;390390+391391+ if (scalable) { /* scalable mode */392392+ struct pasid_entry *pasid_tbl, *pasid_tbl_entry;393393+ struct pasid_dir_entry *dir_tbl, *dir_entry;394394+ u16 dir_idx, tbl_idx, pgtt;395395+ u64 pasid_dir_ptr;396396+397397+ pasid_dir_ptr = context->lo & VTD_PAGE_MASK;398398+399399+ /* Dump specified device domain mappings with PASID. */400400+ dir_idx = pasid >> PASID_PDE_SHIFT;401401+ tbl_idx = pasid & PASID_PTE_MASK;402402+403403+ dir_tbl = phys_to_virt(pasid_dir_ptr);404404+ dir_entry = &dir_tbl[dir_idx];405405+406406+ pasid_tbl = get_pasid_table_from_pde(dir_entry);407407+ if (!pasid_tbl)408408+ goto iommu_unlock;409409+410410+ pasid_tbl_entry = &pasid_tbl[tbl_idx];411411+ if (!pasid_pte_is_present(pasid_tbl_entry))412412+ goto iommu_unlock;413413+414414+ /*415415+ * According to PASID Granular Translation Type(PGTT),416416+ * get the page table pointer.417417+ */418418+ pgtt = (u16)(pasid_tbl_entry->val[0] & GENMASK_ULL(8, 6)) >> 6;419419+ agaw = (u8)(pasid_tbl_entry->val[0] & GENMASK_ULL(4, 2)) >> 2;420420+421421+ switch (pgtt) {422422+ case PASID_ENTRY_PGTT_FL_ONLY:423423+ pgd = pasid_tbl_entry->val[2];424424+ break;425425+ case PASID_ENTRY_PGTT_SL_ONLY:426426+ case PASID_ENTRY_PGTT_NESTED:427427+ pgd = pasid_tbl_entry->val[0];428428+ break;429429+ default:430430+ goto iommu_unlock;431431+ }432432+ pgd &= VTD_PAGE_MASK;433433+ } else { /* legacy mode */434434+ pgd = context->lo & VTD_PAGE_MASK;435435+ agaw = context->hi & 7;436436+ }437437+438438+ seq_printf(m, "Device %04x:%02x:%02x.%x ",439439+ iommu->segment, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));440440+441441+ if (scalable)442442+ seq_printf(m, "with pasid %x @0x%llx\n", pasid, pgd);443443+ else444444+ seq_printf(m, "@0x%llx\n", pgd);445445+446446+ seq_printf(m, "%-17s\t%-18s\t%-18s\t%-18s\t%-18s\t%-s\n",447447+ "IOVA_PFN", "PML5E", "PML4E", "PDPE", "PDE", "PTE");448448+ pgtable_walk_level(m, phys_to_virt(pgd), agaw + 2, 0, path);449449+450450+ found = true;451451+iommu_unlock:452452+ spin_unlock(&iommu->lock);453453+ if (found)454454+ break;391455 }456456+ rcu_read_unlock();392457393458 return 0;394459}395460396396-static int domain_translation_struct_show(struct seq_file *m, void *unused)461461+static int dev_domain_translation_struct_show(struct seq_file *m, void *unused)397462{398398- return bus_for_each_dev(&pci_bus_type, NULL, m,399399- show_device_domain_translation);463463+ struct device_domain_info *info = (struct device_domain_info *)m->private;464464+465465+ return domain_translation_struct_show(m, info, IOMMU_NO_PASID);400466}401401-DEFINE_SHOW_ATTRIBUTE(domain_translation_struct);467467+DEFINE_SHOW_ATTRIBUTE(dev_domain_translation_struct);468468+469469+static int pasid_domain_translation_struct_show(struct seq_file *m, void *unused)470470+{471471+ struct dev_pasid_info *dev_pasid = (struct dev_pasid_info *)m->private;472472+ struct device_domain_info *info = dev_iommu_priv_get(dev_pasid->dev);473473+474474+ return domain_translation_struct_show(m, info, dev_pasid->pasid);475475+}476476+DEFINE_SHOW_ATTRIBUTE(pasid_domain_translation_struct);402477403478static void invalidation_queue_entry_show(struct seq_file *m,404479 struct intel_iommu *iommu)···755666756667void __init intel_iommu_debugfs_init(void)757668{758758- struct dentry *intel_iommu_debug = debugfs_create_dir("intel",759759- iommu_debugfs_dir);669669+ intel_iommu_debug = debugfs_create_dir("intel", iommu_debugfs_dir);760670761671 debugfs_create_file("iommu_regset", 0444, intel_iommu_debug, NULL,762672 &iommu_regset_fops);763673 debugfs_create_file("dmar_translation_struct", 0444, intel_iommu_debug,764674 NULL, &dmar_translation_struct_fops);765765- debugfs_create_file("domain_translation_struct", 0444,766766- intel_iommu_debug, NULL,767767- &domain_translation_struct_fops);768675 debugfs_create_file("invalidation_queue", 0444, intel_iommu_debug,769676 NULL, &invalidation_queue_fops);770677#ifdef CONFIG_IRQ_REMAP···769684#endif770685 debugfs_create_file("dmar_perf_latency", 0644, intel_iommu_debug,771686 NULL, &dmar_perf_latency_fops);687687+}688688+689689+/*690690+ * Create a debugfs directory for each device, and then create a691691+ * debugfs file in this directory for users to dump the page table692692+ * of the default domain. e.g.693693+ * /sys/kernel/debug/iommu/intel/0000:00:01.0/domain_translation_struct694694+ */695695+void intel_iommu_debugfs_create_dev(struct device_domain_info *info)696696+{697697+ info->debugfs_dentry = debugfs_create_dir(dev_name(info->dev), intel_iommu_debug);698698+699699+ debugfs_create_file("domain_translation_struct", 0444, info->debugfs_dentry,700700+ info, &dev_domain_translation_struct_fops);701701+}702702+703703+/* Remove the device debugfs directory. */704704+void intel_iommu_debugfs_remove_dev(struct device_domain_info *info)705705+{706706+ debugfs_remove_recursive(info->debugfs_dentry);707707+}708708+709709+/*710710+ * Create a debugfs directory per pair of {device, pasid}, then create the711711+ * corresponding debugfs file in this directory for users to dump its page712712+ * table. e.g.713713+ * /sys/kernel/debug/iommu/intel/0000:00:01.0/1/domain_translation_struct714714+ *715715+ * The debugfs only dumps the page tables whose mappings are created and716716+ * destroyed by the iommu_map/unmap() interfaces. Check the mapping type717717+ * of the domain before creating debugfs directory.718718+ */719719+void intel_iommu_debugfs_create_dev_pasid(struct dev_pasid_info *dev_pasid)720720+{721721+ struct device_domain_info *info = dev_iommu_priv_get(dev_pasid->dev);722722+ char dir_name[10];723723+724724+ sprintf(dir_name, "%x", dev_pasid->pasid);725725+ dev_pasid->debugfs_dentry = debugfs_create_dir(dir_name, info->debugfs_dentry);726726+727727+ debugfs_create_file("domain_translation_struct", 0444, dev_pasid->debugfs_dentry,728728+ dev_pasid, &pasid_domain_translation_struct_fops);729729+}730730+731731+/* Remove the device pasid debugfs directory. */732732+void intel_iommu_debugfs_remove_dev_pasid(struct dev_pasid_info *dev_pasid)733733+{734734+ debugfs_remove_recursive(dev_pasid->debugfs_dentry);772735}
···3737#include "iommu-priv.h"38383939#include "iommu-sva.h"4040-#include "iommu-priv.h"41404241static struct kset *iommu_group_kset;4342static DEFINE_IDA(iommu_group_ida);···9596static int iommu_bus_notifier(struct notifier_block *nb,9697 unsigned long action, void *data);9798static void iommu_release_device(struct device *dev);9898-static struct iommu_domain *__iommu_domain_alloc(const struct bus_type *bus,9999- unsigned type);9999+static struct iommu_domain *100100+__iommu_group_domain_alloc(struct iommu_group *group, unsigned int type);100101static int __iommu_attach_device(struct iommu_domain *domain,101102 struct device *dev);102103static int __iommu_attach_group(struct iommu_domain *domain,···183184 case IOMMU_DOMAIN_DMA:184185 case IOMMU_DOMAIN_DMA_FQ:185186 return "Translated";187187+ case IOMMU_DOMAIN_PLATFORM:188188+ return "Platform";186189 default:187190 return "Unknown";188191 }···291290 spin_lock(&iommu_device_lock);292291 list_del(&iommu->list);293292 spin_unlock(&iommu_device_lock);293293+294294+ /* Pairs with the alloc in generic_single_device_group() */295295+ iommu_group_put(iommu->singleton_group);296296+ iommu->singleton_group = NULL;294297}295298EXPORT_SYMBOL_GPL(iommu_device_unregister);296299···409404 ret = PTR_ERR(iommu_dev);410405 goto err_module_put;411406 }407407+ dev->iommu->iommu_dev = iommu_dev;412408413409 ret = iommu_device_link(iommu_dev, dev);414410 if (ret)···424418 }425419 dev->iommu_group = group;426420427427- dev->iommu->iommu_dev = iommu_dev;428421 dev->iommu->max_pasids = dev_iommu_get_max_pasids(dev);429422 if (ops->is_attach_deferred)430423 dev->iommu->attach_deferred = ops->is_attach_deferred(dev);···437432err_module_put:438433 module_put(ops->owner);439434err_free:435435+ dev->iommu->iommu_dev = NULL;440436 dev_iommu_free(dev);441437 return ret;442438}···16431637EXPORT_SYMBOL_GPL(generic_device_group);1644163816451639/*16401640+ * Generic device_group call-back function. It just allocates one16411641+ * iommu-group per iommu driver instance shared by every device16421642+ * probed by that iommu driver.16431643+ */16441644+struct iommu_group *generic_single_device_group(struct device *dev)16451645+{16461646+ struct iommu_device *iommu = dev->iommu->iommu_dev;16471647+16481648+ if (!iommu->singleton_group) {16491649+ struct iommu_group *group;16501650+16511651+ group = iommu_group_alloc();16521652+ if (IS_ERR(group))16531653+ return group;16541654+ iommu->singleton_group = group;16551655+ }16561656+ return iommu_group_ref_get(iommu->singleton_group);16571657+}16581658+EXPORT_SYMBOL_GPL(generic_single_device_group);16591659+16601660+/*16461661 * Use standard PCI bus topology, isolation features, and DMA alias quirks16471662 * to find or create an IOMMU group for a device.16481663 */···17441717}17451718EXPORT_SYMBOL_GPL(fsl_mc_device_group);1746171917471747-static int iommu_get_def_domain_type(struct device *dev)17481748-{17491749- const struct iommu_ops *ops = dev_iommu_ops(dev);17501750-17511751- if (dev_is_pci(dev) && to_pci_dev(dev)->untrusted)17521752- return IOMMU_DOMAIN_DMA;17531753-17541754- if (ops->def_domain_type)17551755- return ops->def_domain_type(dev);17561756-17571757- return 0;17581758-}17591759-17601720static struct iommu_domain *17611761-__iommu_group_alloc_default_domain(const struct bus_type *bus,17621762- struct iommu_group *group, int req_type)17211721+__iommu_group_alloc_default_domain(struct iommu_group *group, int req_type)17631722{17641723 if (group->default_domain && group->default_domain->type == req_type)17651724 return group->default_domain;17661766- return __iommu_domain_alloc(bus, req_type);17251725+ return __iommu_group_domain_alloc(group, req_type);17261726+}17271727+17281728+/*17291729+ * Returns the iommu_ops for the devices in an iommu group.17301730+ *17311731+ * It is assumed that all devices in an iommu group are managed by a single17321732+ * IOMMU unit. Therefore, this returns the dev_iommu_ops of the first device17331733+ * in the group.17341734+ */17351735+static const struct iommu_ops *group_iommu_ops(struct iommu_group *group)17361736+{17371737+ struct group_device *device =17381738+ list_first_entry(&group->devices, struct group_device, list);17391739+17401740+ lockdep_assert_held(&group->mutex);17411741+17421742+ return dev_iommu_ops(device->dev);17671743}1768174417691745/*···17761746static struct iommu_domain *17771747iommu_group_alloc_default_domain(struct iommu_group *group, int req_type)17781748{17791779- const struct bus_type *bus =17801780- list_first_entry(&group->devices, struct group_device, list)17811781- ->dev->bus;17491749+ const struct iommu_ops *ops = group_iommu_ops(group);17821750 struct iommu_domain *dom;1783175117841752 lockdep_assert_held(&group->mutex);1785175317541754+ /*17551755+ * Allow legacy drivers to specify the domain that will be the default17561756+ * domain. This should always be either an IDENTITY/BLOCKED/PLATFORM17571757+ * domain. Do not use in new drivers.17581758+ */17591759+ if (ops->default_domain) {17601760+ if (req_type)17611761+ return NULL;17621762+ return ops->default_domain;17631763+ }17641764+17861765 if (req_type)17871787- return __iommu_group_alloc_default_domain(bus, group, req_type);17661766+ return __iommu_group_alloc_default_domain(group, req_type);1788176717891768 /* The driver gave no guidance on what type to use, try the default */17901790- dom = __iommu_group_alloc_default_domain(bus, group, iommu_def_domain_type);17691769+ dom = __iommu_group_alloc_default_domain(group, iommu_def_domain_type);17911770 if (dom)17921771 return dom;1793177217941773 /* Otherwise IDENTITY and DMA_FQ defaults will try DMA */17951774 if (iommu_def_domain_type == IOMMU_DOMAIN_DMA)17961775 return NULL;17971797- dom = __iommu_group_alloc_default_domain(bus, group, IOMMU_DOMAIN_DMA);17761776+ dom = __iommu_group_alloc_default_domain(group, IOMMU_DOMAIN_DMA);17981777 if (!dom)17991778 return NULL;18001779···18471808 return 0;18481809}1849181018501850-/* A target_type of 0 will select the best domain type and cannot fail */18111811+/*18121812+ * Combine the driver's chosen def_domain_type across all the devices in a18131813+ * group. Drivers must give a consistent result.18141814+ */18151815+static int iommu_get_def_domain_type(struct iommu_group *group,18161816+ struct device *dev, int cur_type)18171817+{18181818+ const struct iommu_ops *ops = group_iommu_ops(group);18191819+ int type;18201820+18211821+ if (!ops->def_domain_type)18221822+ return cur_type;18231823+18241824+ type = ops->def_domain_type(dev);18251825+ if (!type || cur_type == type)18261826+ return cur_type;18271827+ if (!cur_type)18281828+ return type;18291829+18301830+ dev_err_ratelimited(18311831+ dev,18321832+ "IOMMU driver error, requesting conflicting def_domain_type, %s and %s, for devices in group %u.\n",18331833+ iommu_domain_type_str(cur_type), iommu_domain_type_str(type),18341834+ group->id);18351835+18361836+ /*18371837+ * Try to recover, drivers are allowed to force IDENITY or DMA, IDENTITY18381838+ * takes precedence.18391839+ */18401840+ if (type == IOMMU_DOMAIN_IDENTITY)18411841+ return type;18421842+ return cur_type;18431843+}18441844+18451845+/*18461846+ * A target_type of 0 will select the best domain type. 0 can be returned in18471847+ * this case meaning the global default should be used.18481848+ */18511849static int iommu_get_default_domain_type(struct iommu_group *group,18521850 int target_type)18531851{18541854- int best_type = target_type;18521852+ struct device *untrusted = NULL;18551853 struct group_device *gdev;18561856- struct device *last_dev;18541854+ int driver_type = 0;1857185518581856 lockdep_assert_held(&group->mutex);1859185718601860- for_each_group_device(group, gdev) {18611861- unsigned int type = iommu_get_def_domain_type(gdev->dev);18621862-18631863- if (best_type && type && best_type != type) {18641864- if (target_type) {18651865- dev_err_ratelimited(18661866- gdev->dev,18671867- "Device cannot be in %s domain\n",18681868- iommu_domain_type_str(target_type));18691869- return -1;18701870- }18711871-18721872- dev_warn(18731873- gdev->dev,18741874- "Device needs domain type %s, but device %s in the same iommu group requires type %s - using default\n",18751875- iommu_domain_type_str(type), dev_name(last_dev),18761876- iommu_domain_type_str(best_type));18771877- return 0;18781878- }18791879- if (!best_type)18801880- best_type = type;18811881- last_dev = gdev->dev;18581858+ /*18591859+ * ARM32 drivers supporting CONFIG_ARM_DMA_USE_IOMMU can declare an18601860+ * identity_domain and it will automatically become their default18611861+ * domain. Later on ARM_DMA_USE_IOMMU will install its UNMANAGED domain.18621862+ * Override the selection to IDENTITY.18631863+ */18641864+ if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)) {18651865+ static_assert(!(IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU) &&18661866+ IS_ENABLED(CONFIG_IOMMU_DMA)));18671867+ driver_type = IOMMU_DOMAIN_IDENTITY;18821868 }18831883- return best_type;18691869+18701870+ for_each_group_device(group, gdev) {18711871+ driver_type = iommu_get_def_domain_type(group, gdev->dev,18721872+ driver_type);18731873+18741874+ if (dev_is_pci(gdev->dev) && to_pci_dev(gdev->dev)->untrusted) {18751875+ /*18761876+ * No ARM32 using systems will set untrusted, it cannot18771877+ * work.18781878+ */18791879+ if (WARN_ON(IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)))18801880+ return -1;18811881+ untrusted = gdev->dev;18821882+ }18831883+ }18841884+18851885+ /*18861886+ * If the common dma ops are not selected in kconfig then we cannot use18871887+ * IOMMU_DOMAIN_DMA at all. Force IDENTITY if nothing else has been18881888+ * selected.18891889+ */18901890+ if (!IS_ENABLED(CONFIG_IOMMU_DMA)) {18911891+ if (WARN_ON(driver_type == IOMMU_DOMAIN_DMA))18921892+ return -1;18931893+ if (!driver_type)18941894+ driver_type = IOMMU_DOMAIN_IDENTITY;18951895+ }18961896+18971897+ if (untrusted) {18981898+ if (driver_type && driver_type != IOMMU_DOMAIN_DMA) {18991899+ dev_err_ratelimited(19001900+ untrusted,19011901+ "Device is not trusted, but driver is overriding group %u to %s, refusing to probe.\n",19021902+ group->id, iommu_domain_type_str(driver_type));19031903+ return -1;19041904+ }19051905+ driver_type = IOMMU_DOMAIN_DMA;19061906+ }19071907+19081908+ if (target_type) {19091909+ if (driver_type && target_type != driver_type)19101910+ return -1;19111911+ return target_type;19121912+ }19131913+ return driver_type;18841914}1885191518861916static void iommu_group_do_probe_finalize(struct device *dev)···20781970}20791971EXPORT_SYMBOL_GPL(iommu_set_fault_handler);2080197220812081-static struct iommu_domain *__iommu_domain_alloc(const struct bus_type *bus,20822082- unsigned type)19731973+static struct iommu_domain *__iommu_domain_alloc(const struct iommu_ops *ops,19741974+ struct device *dev,19751975+ unsigned int type)20831976{20841977 struct iommu_domain *domain;20851978 unsigned int alloc_type = type & IOMMU_DOMAIN_ALLOC_FLAGS;2086197920872087- if (bus == NULL || bus->iommu_ops == NULL)19801980+ if (alloc_type == IOMMU_DOMAIN_IDENTITY && ops->identity_domain)19811981+ return ops->identity_domain;19821982+ else if (alloc_type == IOMMU_DOMAIN_BLOCKED && ops->blocked_domain)19831983+ return ops->blocked_domain;19841984+ else if (type & __IOMMU_DOMAIN_PAGING && ops->domain_alloc_paging)19851985+ domain = ops->domain_alloc_paging(dev);19861986+ else if (ops->domain_alloc)19871987+ domain = ops->domain_alloc(alloc_type);19881988+ else20881989 return NULL;2089199020902090- domain = bus->iommu_ops->domain_alloc(alloc_type);20911991 if (!domain)20921992 return NULL;20931993···21051989 * may override this later21061990 */21071991 if (!domain->pgsize_bitmap)21082108- domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;19921992+ domain->pgsize_bitmap = ops->pgsize_bitmap;2109199321101994 if (!domain->ops)21112111- domain->ops = bus->iommu_ops->default_domain_ops;19951995+ domain->ops = ops->default_domain_ops;2112199621131997 if (iommu_is_dma_domain(domain) && iommu_get_dma_cookie(domain)) {21141998 iommu_domain_free(domain);···21172001 return domain;21182002}2119200320042004+static struct iommu_domain *20052005+__iommu_group_domain_alloc(struct iommu_group *group, unsigned int type)20062006+{20072007+ struct device *dev =20082008+ list_first_entry(&group->devices, struct group_device, list)20092009+ ->dev;20102010+20112011+ return __iommu_domain_alloc(group_iommu_ops(group), dev, type);20122012+}20132013+21202014struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus)21212015{21222122- return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);20162016+ if (bus == NULL || bus->iommu_ops == NULL)20172017+ return NULL;20182018+ return __iommu_domain_alloc(bus->iommu_ops, NULL,20192019+ IOMMU_DOMAIN_UNMANAGED);21232020}21242021EXPORT_SYMBOL_GPL(iommu_domain_alloc);21252022···21412012 if (domain->type == IOMMU_DOMAIN_SVA)21422013 mmdrop(domain->mm);21432014 iommu_put_dma_cookie(domain);21442144- domain->ops->free(domain);20152015+ if (domain->ops->free)20162016+ domain->ops->free(domain);21452017}21462018EXPORT_SYMBOL_GPL(iommu_domain_free);21472019···21922062 */21932063int iommu_attach_device(struct iommu_domain *domain, struct device *dev)21942064{21952195- struct iommu_group *group;20652065+ /* Caller must be a probed driver on dev */20662066+ struct iommu_group *group = dev->iommu_group;21962067 int ret;2197206821982198- group = iommu_group_get(dev);21992069 if (!group)22002070 return -ENODEV;22012071···2212208222132083out_unlock:22142084 mutex_unlock(&group->mutex);22152215- iommu_group_put(group);22162216-22172085 return ret;22182086}22192087EXPORT_SYMBOL_GPL(iommu_attach_device);···2226209822272099void iommu_detach_device(struct iommu_domain *domain, struct device *dev)22282100{22292229- struct iommu_group *group;21012101+ /* Caller must be a probed driver on dev */21022102+ struct iommu_group *group = dev->iommu_group;2230210322312231- group = iommu_group_get(dev);22322104 if (!group)22332105 return;22342106···2240211222412113out_unlock:22422114 mutex_unlock(&group->mutex);22432243- iommu_group_put(group);22442115}22452116EXPORT_SYMBOL_GPL(iommu_detach_device);2246211722472118struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)22482119{22492249- struct iommu_domain *domain;22502250- struct iommu_group *group;21202120+ /* Caller must be a probed driver on dev */21212121+ struct iommu_group *group = dev->iommu_group;2251212222522252- group = iommu_group_get(dev);22532123 if (!group)22542124 return NULL;2255212522562256- domain = group->domain;22572257-22582258- iommu_group_put(group);22592259-22602260- return domain;21262126+ return group->domain;22612127}22622128EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);22632129···23972275 if (group->domain == new_domain)23982276 return 0;2399227724002400- /*24012401- * New drivers should support default domains, so set_platform_dma()24022402- * op will never be called. Otherwise the NULL domain represents some24032403- * platform specific behavior.24042404- */24052405- if (!new_domain) {24062406- for_each_group_device(group, gdev) {24072407- const struct iommu_ops *ops = dev_iommu_ops(gdev->dev);24082408-24092409- if (!WARN_ON(!ops->set_platform_dma_ops))24102410- ops->set_platform_dma_ops(gdev->dev);24112411- }24122412- group->domain = NULL;24132413- return 0;24142414- }22782278+ if (WARN_ON(!new_domain))22792279+ return -EINVAL;2415228024162281 /*24172282 * Changing the domain is done by calling attach_dev() on the new···24342325 */24352326 last_gdev = gdev;24362327 for_each_group_device(group, gdev) {24372437- const struct iommu_ops *ops = dev_iommu_ops(gdev->dev);24382438-24392328 /*24402440- * If set_platform_dma_ops is not present a NULL domain can24412441- * happen only for first probe, in which case we leave24422442- * group->domain as NULL and let release clean everything up.23292329+ * A NULL domain can happen only for first probe, in which case23302330+ * we leave group->domain as NULL and let release clean23312331+ * everything up.24432332 */24442333 if (group->domain)24452334 WARN_ON(__iommu_device_set_domain(24462335 group, gdev->dev, group->domain,24472336 IOMMU_SET_DOMAIN_MUST_SUCCEED));24482448- else if (ops->set_platform_dma_ops)24492449- ops->set_platform_dma_ops(gdev->dev);24502337 if (gdev == last_gdev)24512338 break;24522339 }···25232418 return pgsize;25242419}2525242025262526-static int __iommu_map_pages(struct iommu_domain *domain, unsigned long iova,25272527- phys_addr_t paddr, size_t size, int prot,25282528- gfp_t gfp, size_t *mapped)25292529-{25302530- const struct iommu_domain_ops *ops = domain->ops;25312531- size_t pgsize, count;25322532- int ret;25332533-25342534- pgsize = iommu_pgsize(domain, iova, paddr, size, &count);25352535-25362536- pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx count %zu\n",25372537- iova, &paddr, pgsize, count);25382538-25392539- if (ops->map_pages) {25402540- ret = ops->map_pages(domain, iova, paddr, pgsize, count, prot,25412541- gfp, mapped);25422542- } else {25432543- ret = ops->map(domain, iova, paddr, pgsize, prot, gfp);25442544- *mapped = ret ? 0 : pgsize;25452545- }25462546-25472547- return ret;25482548-}25492549-25502421static int __iommu_map(struct iommu_domain *domain, unsigned long iova,25512422 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)25522423{···25332452 phys_addr_t orig_paddr = paddr;25342453 int ret = 0;2535245425362536- if (unlikely(!(ops->map || ops->map_pages) ||25372537- domain->pgsize_bitmap == 0UL))25382538- return -ENODEV;25392539-25402455 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))25412456 return -EINVAL;24572457+24582458+ if (WARN_ON(!ops->map_pages || domain->pgsize_bitmap == 0UL))24592459+ return -ENODEV;2542246025432461 /* find out the minimum page size supported */25442462 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);···25562476 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);2557247725582478 while (size) {25592559- size_t mapped = 0;24792479+ size_t pgsize, count, mapped = 0;2560248025612561- ret = __iommu_map_pages(domain, iova, paddr, size, prot, gfp,25622562- &mapped);24812481+ pgsize = iommu_pgsize(domain, iova, paddr, size, &count);24822482+24832483+ pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx count %zu\n",24842484+ iova, &paddr, pgsize, count);24852485+ ret = ops->map_pages(domain, iova, paddr, pgsize, count, prot,24862486+ gfp, &mapped);25632487 /*25642488 * Some pages may have been mapped, even if an error occurred,25652489 * so we should account for those so they can be unmapped.···26002516 return -EINVAL;2601251726022518 ret = __iommu_map(domain, iova, paddr, size, prot, gfp);26032603- if (ret == 0 && ops->iotlb_sync_map)26042604- ops->iotlb_sync_map(domain, iova, size);25192519+ if (ret == 0 && ops->iotlb_sync_map) {25202520+ ret = ops->iotlb_sync_map(domain, iova, size);25212521+ if (ret)25222522+ goto out_err;25232523+ }25242524+25252525+ return ret;25262526+25272527+out_err:25282528+ /* undo mappings already done */25292529+ iommu_unmap(domain, iova, size);2605253026062531 return ret;26072532}26082533EXPORT_SYMBOL_GPL(iommu_map);26092609-26102610-static size_t __iommu_unmap_pages(struct iommu_domain *domain,26112611- unsigned long iova, size_t size,26122612- struct iommu_iotlb_gather *iotlb_gather)26132613-{26142614- const struct iommu_domain_ops *ops = domain->ops;26152615- size_t pgsize, count;26162616-26172617- pgsize = iommu_pgsize(domain, iova, iova, size, &count);26182618- return ops->unmap_pages ?26192619- ops->unmap_pages(domain, iova, pgsize, count, iotlb_gather) :26202620- ops->unmap(domain, iova, pgsize, iotlb_gather);26212621-}2622253426232535static size_t __iommu_unmap(struct iommu_domain *domain,26242536 unsigned long iova, size_t size,···26252545 unsigned long orig_iova = iova;26262546 unsigned int min_pagesz;2627254726282628- if (unlikely(!(ops->unmap || ops->unmap_pages) ||26292629- domain->pgsize_bitmap == 0UL))25482548+ if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))26302549 return 0;2631255026322632- if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))25512551+ if (WARN_ON(!ops->unmap_pages || domain->pgsize_bitmap == 0UL))26332552 return 0;2634255326352554 /* find out the minimum page size supported */···26522573 * or we hit an area that isn't mapped.26532574 */26542575 while (unmapped < size) {26552655- unmapped_page = __iommu_unmap_pages(domain, iova,26562656- size - unmapped,26572657- iotlb_gather);25762576+ size_t pgsize, count;25772577+25782578+ pgsize = iommu_pgsize(domain, iova, iova, size - unmapped, &count);25792579+ unmapped_page = ops->unmap_pages(domain, iova, pgsize, count, iotlb_gather);26582580 if (!unmapped_page)26592581 break;26602582···27382658 sg = sg_next(sg);27392659 }2740266027412741- if (ops->iotlb_sync_map)27422742- ops->iotlb_sync_map(domain, iova, mapped);26612661+ if (ops->iotlb_sync_map) {26622662+ ret = ops->iotlb_sync_map(domain, iova, mapped);26632663+ if (ret)26642664+ goto out_err;26652665+ }27432666 return mapped;2744266727452668out_err:···30402957 if (req_type < 0)30412958 return -EINVAL;3042295930433043- /*30443044- * There are still some drivers which don't support default domains, so30453045- * we ignore the failure and leave group->default_domain NULL.30463046- *30473047- * We assume that the iommu driver starts up the device in30483048- * 'set_platform_dma_ops' mode if it does not support default domains.30493049- */30502960 dom = iommu_group_alloc_default_domain(group, req_type);30513051- if (!dom) {30523052- /* Once in default_domain mode we never leave */30533053- if (group->default_domain)30543054- return -ENODEV;30553055- group->default_domain = NULL;30563056- return 0;30573057- }29612961+ if (!dom)29622962+ return -ENODEV;3058296330592964 if (group->default_domain == dom)30602965 return 0;···31853114 return ret ?: count;31863115}3187311631883188-static bool iommu_is_default_domain(struct iommu_group *group)31893189-{31903190- if (group->domain == group->default_domain)31913191- return true;31923192-31933193- /*31943194- * If the default domain was set to identity and it is still an identity31953195- * domain then we consider this a pass. This happens because of31963196- * amd_iommu_init_device() replacing the default idenytity domain with an31973197- * identity domain that has a different configuration for AMDGPU.31983198- */31993199- if (group->default_domain &&32003200- group->default_domain->type == IOMMU_DOMAIN_IDENTITY &&32013201- group->domain && group->domain->type == IOMMU_DOMAIN_IDENTITY)32023202- return true;32033203- return false;32043204-}32053205-32063117/**32073118 * iommu_device_use_default_domain() - Device driver wants to handle device32083119 * DMA through the kernel DMA API.···31953142 */31963143int iommu_device_use_default_domain(struct device *dev)31973144{31983198- struct iommu_group *group = iommu_group_get(dev);31453145+ /* Caller is the driver core during the pre-probe path */31463146+ struct iommu_group *group = dev->iommu_group;31993147 int ret = 0;3200314832013149 if (!group)···3204315032053151 mutex_lock(&group->mutex);32063152 if (group->owner_cnt) {32073207- if (group->owner || !iommu_is_default_domain(group) ||31533153+ if (group->domain != group->default_domain || group->owner ||32083154 !xa_empty(&group->pasid_array)) {32093155 ret = -EBUSY;32103156 goto unlock_out;···3215316132163162unlock_out:32173163 mutex_unlock(&group->mutex);32183218- iommu_group_put(group);32193219-32203164 return ret;32213165}32223166···32283176 */32293177void iommu_device_unuse_default_domain(struct device *dev)32303178{32313231- struct iommu_group *group = iommu_group_get(dev);31793179+ /* Caller is the driver core during the post-probe path */31803180+ struct iommu_group *group = dev->iommu_group;3232318132333182 if (!group)32343183 return;···32393186 group->owner_cnt--;3240318732413188 mutex_unlock(&group->mutex);32423242- iommu_group_put(group);32433189}3244319032453191static int __iommu_group_alloc_blocking_domain(struct iommu_group *group)32463192{32473247- struct group_device *dev =32483248- list_first_entry(&group->devices, struct group_device, list);32493249-32503193 if (group->blocking_domain)32513194 return 0;3252319532533196 group->blocking_domain =32543254- __iommu_domain_alloc(dev->dev->bus, IOMMU_DOMAIN_BLOCKED);31973197+ __iommu_group_domain_alloc(group, IOMMU_DOMAIN_BLOCKED);32553198 if (!group->blocking_domain) {32563199 /*32573200 * For drivers that do not yet understand IOMMU_DOMAIN_BLOCKED32583201 * create an empty domain instead.32593202 */32603260- group->blocking_domain = __iommu_domain_alloc(32613261- dev->dev->bus, IOMMU_DOMAIN_UNMANAGED);32033203+ group->blocking_domain = __iommu_group_domain_alloc(32043204+ group, IOMMU_DOMAIN_UNMANAGED);32623205 if (!group->blocking_domain)32633206 return -EINVAL;32643207 }···33223273 */33233274int iommu_device_claim_dma_owner(struct device *dev, void *owner)33243275{33253325- struct iommu_group *group;32763276+ /* Caller must be a probed driver on dev */32773277+ struct iommu_group *group = dev->iommu_group;33263278 int ret = 0;3327327933283280 if (WARN_ON(!owner))33293281 return -EINVAL;3330328233313331- group = iommu_group_get(dev);33323283 if (!group)33333284 return -ENODEV;33343285···33453296 ret = __iommu_take_dma_ownership(group, owner);33463297unlock_out:33473298 mutex_unlock(&group->mutex);33483348- iommu_group_put(group);33493349-33503299 return ret;33513300}33523301EXPORT_SYMBOL_GPL(iommu_device_claim_dma_owner);···33823335 */33833336void iommu_device_release_dma_owner(struct device *dev)33843337{33853385- struct iommu_group *group = iommu_group_get(dev);33383338+ /* Caller must be a probed driver on dev */33393339+ struct iommu_group *group = dev->iommu_group;3386334033873341 mutex_lock(&group->mutex);33883342 if (group->owner_cnt > 1)···33913343 else33923344 __iommu_release_dma_ownership(group);33933345 mutex_unlock(&group->mutex);33943394- iommu_group_put(group);33953346}33963347EXPORT_SYMBOL_GPL(iommu_device_release_dma_owner);33973348···34513404int iommu_attach_device_pasid(struct iommu_domain *domain,34523405 struct device *dev, ioasid_t pasid)34533406{34543454- struct iommu_group *group;34073407+ /* Caller must be a probed driver on dev */34083408+ struct iommu_group *group = dev->iommu_group;34553409 void *curr;34563410 int ret;3457341134583412 if (!domain->ops->set_dev_pasid)34593413 return -EOPNOTSUPP;3460341434613461- group = iommu_group_get(dev);34623415 if (!group)34633416 return -ENODEV;34643417···34763429 }34773430out_unlock:34783431 mutex_unlock(&group->mutex);34793479- iommu_group_put(group);34803480-34813432 return ret;34823433}34833434EXPORT_SYMBOL_GPL(iommu_attach_device_pasid);···34923447void iommu_detach_device_pasid(struct iommu_domain *domain, struct device *dev,34933448 ioasid_t pasid)34943449{34953495- struct iommu_group *group = iommu_group_get(dev);34503450+ /* Caller must be a probed driver on dev */34513451+ struct iommu_group *group = dev->iommu_group;3496345234973453 mutex_lock(&group->mutex);34983454 __iommu_remove_group_pasid(group, pasid);34993455 WARN_ON(xa_erase(&group->pasid_array, pasid) != domain);35003456 mutex_unlock(&group->mutex);35013501-35023502- iommu_group_put(group);35033457}35043458EXPORT_SYMBOL_GPL(iommu_detach_device_pasid);35053459···35203476 ioasid_t pasid,35213477 unsigned int type)35223478{34793479+ /* Caller must be a probed driver on dev */34803480+ struct iommu_group *group = dev->iommu_group;35233481 struct iommu_domain *domain;35243524- struct iommu_group *group;3525348235263526- group = iommu_group_get(dev);35273483 if (!group)35283484 return NULL;35293485···35323488 if (type && domain && domain->type != type)35333489 domain = ERR_PTR(-EBUSY);35343490 xa_unlock(&group->pasid_array);35353535- iommu_group_put(group);3536349135373492 return domain;35383493}
+8-22
drivers/iommu/iommufd/selftest.c
···111111 };112112};113113114114-static void mock_domain_blocking_free(struct iommu_domain *domain)115115-{116116-}117117-118114static int mock_domain_nop_attach(struct iommu_domain *domain,119115 struct device *dev)120116{···118122}119123120124static const struct iommu_domain_ops mock_blocking_ops = {121121- .free = mock_domain_blocking_free,122125 .attach_dev = mock_domain_nop_attach,123126};124127···141146 return info;142147}143148144144-static struct iommu_domain *mock_domain_alloc(unsigned int iommu_domain_type)149149+static struct iommu_domain *mock_domain_alloc_paging(struct device *dev)145150{146151 struct mock_iommu_domain *mock;147147-148148- if (iommu_domain_type == IOMMU_DOMAIN_BLOCKED)149149- return &mock_blocking_domain;150150-151151- if (iommu_domain_type != IOMMU_DOMAIN_UNMANAGED)152152- return NULL;153152154153 mock = kzalloc(sizeof(*mock), GFP_KERNEL);155154 if (!mock)···275286 return cap == IOMMU_CAP_CACHE_COHERENCY;276287}277288278278-static void mock_domain_set_plaform_dma_ops(struct device *dev)279279-{280280- /*281281- * mock doesn't setup default domains because we can't hook into the282282- * normal probe path283283- */284284-}285285-286289static struct iommu_device mock_iommu_device = {287290};288291···284303}285304286305static const struct iommu_ops mock_ops = {306306+ /*307307+ * IOMMU_DOMAIN_BLOCKED cannot be returned from def_domain_type()308308+ * because it is zero.309309+ */310310+ .default_domain = &mock_blocking_domain,311311+ .blocked_domain = &mock_blocking_domain,287312 .owner = THIS_MODULE,288313 .pgsize_bitmap = MOCK_IO_PAGE_SIZE,289314 .hw_info = mock_domain_hw_info,290290- .domain_alloc = mock_domain_alloc,315315+ .domain_alloc_paging = mock_domain_alloc_paging,291316 .capable = mock_domain_capable,292292- .set_platform_dma_ops = mock_domain_set_plaform_dma_ops,293317 .device_group = generic_device_group,294318 .probe_device = mock_probe_device,295319 .default_domain_ops =
+65-30
drivers/iommu/iova.c
···1111#include <linux/smp.h>1212#include <linux/bitops.h>1313#include <linux/cpu.h>1414+#include <linux/workqueue.h>14151516/* The anchor node sits above the top of the usable address space */1617#define IOVA_ANCHOR ~0UL···623622/*624623 * As kmalloc's buffer size is fixed to power of 2, 127 is chosen to625624 * assure size of 'iova_magazine' to be 1024 bytes, so that no memory626626- * will be wasted.625625+ * will be wasted. Since only full magazines are inserted into the depot,626626+ * we don't need to waste PFN capacity on a separate list head either.627627 */628628#define IOVA_MAG_SIZE 127629629-#define MAX_GLOBAL_MAGS 32 /* magazines per bin */629629+630630+#define IOVA_DEPOT_DELAY msecs_to_jiffies(100)630631631632struct iova_magazine {632632- unsigned long size;633633+ union {634634+ unsigned long size;635635+ struct iova_magazine *next;636636+ };633637 unsigned long pfns[IOVA_MAG_SIZE];634638};639639+static_assert(!(sizeof(struct iova_magazine) & (sizeof(struct iova_magazine) - 1)));635640636641struct iova_cpu_rcache {637642 spinlock_t lock;···647640648641struct iova_rcache {649642 spinlock_t lock;650650- unsigned long depot_size;651651- struct iova_magazine *depot[MAX_GLOBAL_MAGS];643643+ unsigned int depot_size;644644+ struct iova_magazine *depot;652645 struct iova_cpu_rcache __percpu *cpu_rcaches;646646+ struct iova_domain *iovad;647647+ struct delayed_work work;653648};654649655650static struct iova_magazine *iova_magazine_alloc(gfp_t flags)···726717 mag->pfns[mag->size++] = pfn;727718}728719720720+static struct iova_magazine *iova_depot_pop(struct iova_rcache *rcache)721721+{722722+ struct iova_magazine *mag = rcache->depot;723723+724724+ rcache->depot = mag->next;725725+ mag->size = IOVA_MAG_SIZE;726726+ rcache->depot_size--;727727+ return mag;728728+}729729+730730+static void iova_depot_push(struct iova_rcache *rcache, struct iova_magazine *mag)731731+{732732+ mag->next = rcache->depot;733733+ rcache->depot = mag;734734+ rcache->depot_size++;735735+}736736+737737+static void iova_depot_work_func(struct work_struct *work)738738+{739739+ struct iova_rcache *rcache = container_of(work, typeof(*rcache), work.work);740740+ struct iova_magazine *mag = NULL;741741+ unsigned long flags;742742+743743+ spin_lock_irqsave(&rcache->lock, flags);744744+ if (rcache->depot_size > num_online_cpus())745745+ mag = iova_depot_pop(rcache);746746+ spin_unlock_irqrestore(&rcache->lock, flags);747747+748748+ if (mag) {749749+ iova_magazine_free_pfns(mag, rcache->iovad);750750+ iova_magazine_free(mag);751751+ schedule_delayed_work(&rcache->work, IOVA_DEPOT_DELAY);752752+ }753753+}754754+729755int iova_domain_init_rcaches(struct iova_domain *iovad)730756{731757 unsigned int cpu;···778734779735 rcache = &iovad->rcaches[i];780736 spin_lock_init(&rcache->lock);781781- rcache->depot_size = 0;737737+ rcache->iovad = iovad;738738+ INIT_DELAYED_WORK(&rcache->work, iova_depot_work_func);782739 rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache),783740 cache_line_size());784741 if (!rcache->cpu_rcaches) {···821776 struct iova_rcache *rcache,822777 unsigned long iova_pfn)823778{824824- struct iova_magazine *mag_to_free = NULL;825779 struct iova_cpu_rcache *cpu_rcache;826780 bool can_insert = false;827781 unsigned long flags;···838794839795 if (new_mag) {840796 spin_lock(&rcache->lock);841841- if (rcache->depot_size < MAX_GLOBAL_MAGS) {842842- rcache->depot[rcache->depot_size++] =843843- cpu_rcache->loaded;844844- } else {845845- mag_to_free = cpu_rcache->loaded;846846- }797797+ iova_depot_push(rcache, cpu_rcache->loaded);847798 spin_unlock(&rcache->lock);799799+ schedule_delayed_work(&rcache->work, IOVA_DEPOT_DELAY);848800849801 cpu_rcache->loaded = new_mag;850802 can_insert = true;···851811 iova_magazine_push(cpu_rcache->loaded, iova_pfn);852812853813 spin_unlock_irqrestore(&cpu_rcache->lock, flags);854854-855855- if (mag_to_free) {856856- iova_magazine_free_pfns(mag_to_free, iovad);857857- iova_magazine_free(mag_to_free);858858- }859814860815 return can_insert;861816}···889854 has_pfn = true;890855 } else {891856 spin_lock(&rcache->lock);892892- if (rcache->depot_size > 0) {857857+ if (rcache->depot) {893858 iova_magazine_free(cpu_rcache->loaded);894894- cpu_rcache->loaded = rcache->depot[--rcache->depot_size];859859+ cpu_rcache->loaded = iova_depot_pop(rcache);895860 has_pfn = true;896861 }897862 spin_unlock(&rcache->lock);···930895 struct iova_rcache *rcache;931896 struct iova_cpu_rcache *cpu_rcache;932897 unsigned int cpu;933933- int i, j;934898935935- for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {899899+ for (int i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {936900 rcache = &iovad->rcaches[i];937901 if (!rcache->cpu_rcaches)938902 break;···941907 iova_magazine_free(cpu_rcache->prev);942908 }943909 free_percpu(rcache->cpu_rcaches);944944- for (j = 0; j < rcache->depot_size; ++j)945945- iova_magazine_free(rcache->depot[j]);910910+ cancel_delayed_work_sync(&rcache->work);911911+ while (rcache->depot)912912+ iova_magazine_free(iova_depot_pop(rcache));946913 }947914948915 kfree(iovad->rcaches);···977942{978943 struct iova_rcache *rcache;979944 unsigned long flags;980980- int i, j;981945982982- for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {946946+ for (int i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {983947 rcache = &iovad->rcaches[i];984948 spin_lock_irqsave(&rcache->lock, flags);985985- for (j = 0; j < rcache->depot_size; ++j) {986986- iova_magazine_free_pfns(rcache->depot[j], iovad);987987- iova_magazine_free(rcache->depot[j]);949949+ while (rcache->depot) {950950+ struct iova_magazine *mag = iova_depot_pop(rcache);951951+952952+ iova_magazine_free_pfns(mag, iovad);953953+ iova_magazine_free(mag);988954 }989989- rcache->depot_size = 0;990955 spin_unlock_irqrestore(&rcache->lock, flags);991956 }992957}
+50-22
drivers/iommu/ipmmu-vmsa.c
···6464 struct ipmmu_vmsa_domain *domains[IPMMU_CTX_MAX];6565 s8 utlb_ctx[IPMMU_UTLB_MAX];66666767- struct iommu_group *group;6867 struct dma_iommu_mapping *mapping;6968};7069···292293 ipmmu_imuctr_write(mmu, utlb, IMUCTR_TTSEL_MMU(domain->context_id) |293294 IMUCTR_FLUSH | IMUCTR_MMUEN);294295 mmu->utlb_ctx[utlb] = domain->context_id;296296+}297297+298298+/*299299+ * Disable MMU translation for the microTLB.300300+ */301301+static void ipmmu_utlb_disable(struct ipmmu_vmsa_domain *domain,302302+ unsigned int utlb)303303+{304304+ struct ipmmu_vmsa_device *mmu = domain->mmu;305305+306306+ ipmmu_imuctr_write(mmu, utlb, 0);307307+ mmu->utlb_ctx[utlb] = IPMMU_CTX_INVALID;295308}296309297310static void ipmmu_tlb_flush_all(void *cookie)···562551 * IOMMU Operations563552 */564553565565-static struct iommu_domain *ipmmu_domain_alloc(unsigned type)554554+static struct iommu_domain *ipmmu_domain_alloc_paging(struct device *dev)566555{567556 struct ipmmu_vmsa_domain *domain;568568-569569- if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA)570570- return NULL;571557572558 domain = kzalloc(sizeof(*domain), GFP_KERNEL);573559 if (!domain)···634626635627 return 0;636628}629629+630630+static int ipmmu_iommu_identity_attach(struct iommu_domain *identity_domain,631631+ struct device *dev)632632+{633633+ struct iommu_domain *io_domain = iommu_get_domain_for_dev(dev);634634+ struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);635635+ struct ipmmu_vmsa_domain *domain;636636+ unsigned int i;637637+638638+ if (io_domain == identity_domain || !io_domain)639639+ return 0;640640+641641+ domain = to_vmsa_domain(io_domain);642642+ for (i = 0; i < fwspec->num_ids; ++i)643643+ ipmmu_utlb_disable(domain, fwspec->ids[i]);644644+645645+ /*646646+ * TODO: Optimize by disabling the context when no device is attached.647647+ */648648+ return 0;649649+}650650+651651+static struct iommu_domain_ops ipmmu_iommu_identity_ops = {652652+ .attach_dev = ipmmu_iommu_identity_attach,653653+};654654+655655+static struct iommu_domain ipmmu_iommu_identity_domain = {656656+ .type = IOMMU_DOMAIN_IDENTITY,657657+ .ops = &ipmmu_iommu_identity_ops,658658+};637659638660static int ipmmu_map(struct iommu_domain *io_domain, unsigned long iova,639661 phys_addr_t paddr, size_t pgsize, size_t pgcount,···871833 arm_iommu_release_mapping(mmu->mapping);872834}873835874874-static struct iommu_group *ipmmu_find_group(struct device *dev)875875-{876876- struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);877877- struct iommu_group *group;878878-879879- if (mmu->group)880880- return iommu_group_ref_get(mmu->group);881881-882882- group = iommu_group_alloc();883883- if (!IS_ERR(group))884884- mmu->group = group;885885-886886- return group;887887-}888888-889836static const struct iommu_ops ipmmu_ops = {890890- .domain_alloc = ipmmu_domain_alloc,837837+ .identity_domain = &ipmmu_iommu_identity_domain,838838+ .domain_alloc_paging = ipmmu_domain_alloc_paging,891839 .probe_device = ipmmu_probe_device,892840 .release_device = ipmmu_release_device,893841 .probe_finalize = ipmmu_probe_finalize,842842+ /*843843+ * FIXME: The device grouping is a fixed property of the hardware's844844+ * ability to isolate and control DMA, it should not depend on kconfig.845845+ */894846 .device_group = IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_IOMMU_DMA)895895- ? generic_device_group : ipmmu_find_group,847847+ ? generic_device_group : generic_single_device_group,896848 .pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K,897849 .of_xlate = ipmmu_of_xlate,898850 .default_domain_ops = &(const struct iommu_domain_ops) {
···688688 return 0;689689}690690691691-static int tegra20_mc_suspend(struct tegra_mc *mc)692692-{693693- int err;694694-695695- if (IS_ENABLED(CONFIG_TEGRA_IOMMU_GART) && mc->gart) {696696- err = tegra_gart_suspend(mc->gart);697697- if (err < 0)698698- return err;699699- }700700-701701- return 0;702702-}703703-704704-static int tegra20_mc_resume(struct tegra_mc *mc)705705-{706706- int err;707707-708708- if (IS_ENABLED(CONFIG_TEGRA_IOMMU_GART) && mc->gart) {709709- err = tegra_gart_resume(mc->gart);710710- if (err < 0)711711- return err;712712- }713713-714714- return 0;715715-}716716-717691static irqreturn_t tegra20_mc_handle_irq(int irq, void *data)718692{719693 struct tegra_mc *mc = data;···763789764790static const struct tegra_mc_ops tegra20_mc_ops = {765791 .probe = tegra20_mc_probe,766766- .suspend = tegra20_mc_suspend,767767- .resume = tegra20_mc_resume,768792 .handle_irq = tegra20_mc_handle_irq,769793};770794
-120
include/linux/amd-iommu.h
···33333434extern int amd_iommu_detect(void);35353636-/**3737- * amd_iommu_init_device() - Init device for use with IOMMUv2 driver3838- * @pdev: The PCI device to initialize3939- * @pasids: Number of PASIDs to support for this device4040- *4141- * This function does all setup for the device pdev so that it can be4242- * used with IOMMUv2.4343- * Returns 0 on success or negative value on error.4444- */4545-extern int amd_iommu_init_device(struct pci_dev *pdev, int pasids);4646-4747-/**4848- * amd_iommu_free_device() - Free all IOMMUv2 related device resources4949- * and disable IOMMUv2 usage for this device5050- * @pdev: The PCI device to disable IOMMUv2 usage for'5151- */5252-extern void amd_iommu_free_device(struct pci_dev *pdev);5353-5454-/**5555- * amd_iommu_bind_pasid() - Bind a given task to a PASID on a device5656- * @pdev: The PCI device to bind the task to5757- * @pasid: The PASID on the device the task should be bound to5858- * @task: the task to bind5959- *6060- * The function returns 0 on success or a negative value on error.6161- */6262-extern int amd_iommu_bind_pasid(struct pci_dev *pdev, u32 pasid,6363- struct task_struct *task);6464-6565-/**6666- * amd_iommu_unbind_pasid() - Unbind a PASID from its task on6767- * a device6868- * @pdev: The device of the PASID6969- * @pasid: The PASID to unbind7070- *7171- * When this function returns the device is no longer using the PASID7272- * and the PASID is no longer bound to its task.7373- */7474-extern void amd_iommu_unbind_pasid(struct pci_dev *pdev, u32 pasid);7575-7676-/**7777- * amd_iommu_set_invalid_ppr_cb() - Register a call-back for failed7878- * PRI requests7979- * @pdev: The PCI device the call-back should be registered for8080- * @cb: The call-back function8181- *8282- * The IOMMUv2 driver invokes this call-back when it is unable to8383- * successfully handle a PRI request. The device driver can then decide8484- * which PRI response the device should see. Possible return values for8585- * the call-back are:8686- *8787- * - AMD_IOMMU_INV_PRI_RSP_SUCCESS - Send SUCCESS back to the device8888- * - AMD_IOMMU_INV_PRI_RSP_INVALID - Send INVALID back to the device8989- * - AMD_IOMMU_INV_PRI_RSP_FAIL - Send Failure back to the device,9090- * the device is required to disable9191- * PRI when it receives this response9292- *9393- * The function returns 0 on success or negative value on error.9494- */9595-#define AMD_IOMMU_INV_PRI_RSP_SUCCESS 09696-#define AMD_IOMMU_INV_PRI_RSP_INVALID 19797-#define AMD_IOMMU_INV_PRI_RSP_FAIL 29898-9999-typedef int (*amd_iommu_invalid_ppr_cb)(struct pci_dev *pdev,100100- u32 pasid,101101- unsigned long address,102102- u16);103103-104104-extern int amd_iommu_set_invalid_ppr_cb(struct pci_dev *pdev,105105- amd_iommu_invalid_ppr_cb cb);106106-107107-#define PPR_FAULT_EXEC (1 << 1)108108-#define PPR_FAULT_READ (1 << 2)109109-#define PPR_FAULT_WRITE (1 << 5)110110-#define PPR_FAULT_USER (1 << 6)111111-#define PPR_FAULT_RSVD (1 << 7)112112-#define PPR_FAULT_GN (1 << 8)113113-114114-/**115115- * amd_iommu_device_info() - Get information about IOMMUv2 support of a116116- * PCI device117117- * @pdev: PCI device to query information from118118- * @info: A pointer to an amd_iommu_device_info structure which will contain119119- * the information about the PCI device120120- *121121- * Returns 0 on success, negative value on error122122- */123123-124124-#define AMD_IOMMU_DEVICE_FLAG_ATS_SUP 0x1 /* ATS feature supported */125125-#define AMD_IOMMU_DEVICE_FLAG_PRI_SUP 0x2 /* PRI feature supported */126126-#define AMD_IOMMU_DEVICE_FLAG_PASID_SUP 0x4 /* PASID context supported */127127-#define AMD_IOMMU_DEVICE_FLAG_EXEC_SUP 0x8 /* Device may request execution128128- on memory pages */129129-#define AMD_IOMMU_DEVICE_FLAG_PRIV_SUP 0x10 /* Device may request130130- super-user privileges */131131-132132-struct amd_iommu_device_info {133133- int max_pasids;134134- u32 flags;135135-};136136-137137-extern int amd_iommu_device_info(struct pci_dev *pdev,138138- struct amd_iommu_device_info *info);139139-140140-/**141141- * amd_iommu_set_invalidate_ctx_cb() - Register a call-back for invalidating142142- * a pasid context. This call-back is143143- * invoked when the IOMMUv2 driver needs to144144- * invalidate a PASID context, for example145145- * because the task that is bound to that146146- * context is about to exit.147147- *148148- * @pdev: The PCI device the call-back should be registered for149149- * @cb: The call-back function150150- */151151-152152-typedef void (*amd_iommu_invalidate_ctx)(struct pci_dev *pdev, u32 pasid);153153-154154-extern int amd_iommu_set_invalidate_ctx_cb(struct pci_dev *pdev,155155- amd_iommu_invalidate_ctx cb);15636#else /* CONFIG_AMD_IOMMU */1573715838static inline int amd_iommu_detect(void) { return -ENODEV; }
+25-13
include/linux/iommu.h
···6464#define __IOMMU_DOMAIN_DMA_FQ (1U << 3) /* DMA-API uses flush queue */65656666#define __IOMMU_DOMAIN_SVA (1U << 4) /* Shared process address space */6767+#define __IOMMU_DOMAIN_PLATFORM (1U << 5)67686869#define IOMMU_DOMAIN_ALLOC_FLAGS ~__IOMMU_DOMAIN_DMA_FQ6970/*···8281 * invalidation.8382 * IOMMU_DOMAIN_SVA - DMA addresses are shared process addresses8483 * represented by mm_struct's.8484+ * IOMMU_DOMAIN_PLATFORM - Legacy domain for drivers that do their own8585+ * dma_api stuff. Do not use in new drivers.8586 */8687#define IOMMU_DOMAIN_BLOCKED (0U)8788#define IOMMU_DOMAIN_IDENTITY (__IOMMU_DOMAIN_PT)···9491 __IOMMU_DOMAIN_DMA_API | \9592 __IOMMU_DOMAIN_DMA_FQ)9693#define IOMMU_DOMAIN_SVA (__IOMMU_DOMAIN_SVA)9494+#define IOMMU_DOMAIN_PLATFORM (__IOMMU_DOMAIN_PLATFORM)97959896struct iommu_domain {9997 unsigned type;···239235 * use. The information type is one of enum iommu_hw_info_type defined240236 * in include/uapi/linux/iommufd.h.241237 * @domain_alloc: allocate iommu domain238238+ * @domain_alloc_paging: Allocate an iommu_domain that can be used for239239+ * UNMANAGED, DMA, and DMA_FQ domain types.242240 * @probe_device: Add device to iommu driver handling243241 * @release_device: Remove device from iommu driver handling244242 * @probe_finalize: Do final setup work after the device is added to an IOMMU245243 * group and attached to the groups domain246246- * @set_platform_dma_ops: Returning control back to the platform DMA ops. This op247247- * is to support old IOMMU drivers, new drivers should use248248- * default domains, and the common IOMMU DMA ops.249244 * @device_group: find iommu group for a particular device250245 * @get_resv_regions: Request list of reserved regions for a device251246 * @of_xlate: add OF master IDs to iommu grouping···263260 * will be blocked by the hardware.264261 * @pgsize_bitmap: bitmap of all possible supported page sizes265262 * @owner: Driver module providing these ops263263+ * @identity_domain: An always available, always attachable identity264264+ * translation.265265+ * @blocked_domain: An always available, always attachable blocking266266+ * translation.267267+ * @default_domain: If not NULL this will always be set as the default domain.268268+ * This should be an IDENTITY/BLOCKED/PLATFORM domain.269269+ * Do not use in new drivers.266270 */267271struct iommu_ops {268272 bool (*capable)(struct device *dev, enum iommu_cap);···277267278268 /* Domain allocation and freeing by the iommu driver */279269 struct iommu_domain *(*domain_alloc)(unsigned iommu_domain_type);270270+ struct iommu_domain *(*domain_alloc_paging)(struct device *dev);280271281272 struct iommu_device *(*probe_device)(struct device *dev);282273 void (*release_device)(struct device *dev);283274 void (*probe_finalize)(struct device *dev);284284- void (*set_platform_dma_ops)(struct device *dev);285275 struct iommu_group *(*device_group)(struct device *dev);286276287277 /* Request/Free a list of reserved regions for a device */···304294 const struct iommu_domain_ops *default_domain_ops;305295 unsigned long pgsize_bitmap;306296 struct module *owner;297297+ struct iommu_domain *identity_domain;298298+ struct iommu_domain *blocked_domain;299299+ struct iommu_domain *default_domain;307300};308301309302/**···325312 * * ENODEV - device specific errors, not able to be attached326313 * * <others> - treated as ENODEV by the caller. Use is discouraged327314 * @set_dev_pasid: set an iommu domain to a pasid of device328328- * @map: map a physically contiguous memory region to an iommu domain329315 * @map_pages: map a physically contiguous set of pages of the same size to330316 * an iommu domain.331331- * @unmap: unmap a physically contiguous memory region from an iommu domain332317 * @unmap_pages: unmap a number of pages of the same size from an iommu domain333318 * @flush_iotlb_all: Synchronously flush all hardware TLBs for this domain334319 * @iotlb_sync_map: Sync mappings created recently using @map to the hardware···345334 int (*set_dev_pasid)(struct iommu_domain *domain, struct device *dev,346335 ioasid_t pasid);347336348348- int (*map)(struct iommu_domain *domain, unsigned long iova,349349- phys_addr_t paddr, size_t size, int prot, gfp_t gfp);350337 int (*map_pages)(struct iommu_domain *domain, unsigned long iova,351338 phys_addr_t paddr, size_t pgsize, size_t pgcount,352339 int prot, gfp_t gfp, size_t *mapped);353353- size_t (*unmap)(struct iommu_domain *domain, unsigned long iova,354354- size_t size, struct iommu_iotlb_gather *iotlb_gather);355340 size_t (*unmap_pages)(struct iommu_domain *domain, unsigned long iova,356341 size_t pgsize, size_t pgcount,357342 struct iommu_iotlb_gather *iotlb_gather);358343359344 void (*flush_iotlb_all)(struct iommu_domain *domain);360360- void (*iotlb_sync_map)(struct iommu_domain *domain, unsigned long iova,361361- size_t size);345345+ int (*iotlb_sync_map)(struct iommu_domain *domain, unsigned long iova,346346+ size_t size);362347 void (*iotlb_sync)(struct iommu_domain *domain,363348 struct iommu_iotlb_gather *iotlb_gather);364349···375368 * @list: Used by the iommu-core to keep a list of registered iommus376369 * @ops: iommu-ops for talking to this iommu377370 * @dev: struct device for sysfs handling371371+ * @singleton_group: Used internally for drivers that have only one group378372 * @max_pasids: number of supported PASIDs379373 */380374struct iommu_device {···383375 const struct iommu_ops *ops;384376 struct fwnode_handle *fwnode;385377 struct device *dev;378378+ struct iommu_group *singleton_group;386379 u32 max_pasids;387380};388381···427418 * @attach_deferred: the dma domain attachment is deferred428419 * @pci_32bit_workaround: Limit DMA allocations to 32-bit IOVAs429420 * @require_direct: device requires IOMMU_RESV_DIRECT regions421421+ * @shadow_on_flush: IOTLB flushes are used to sync shadow tables430422 *431423 * TODO: migrate other per device data pointers under iommu_dev_data, e.g.432424 * struct iommu_group *iommu_group;···443433 u32 attach_deferred:1;444434 u32 pci_32bit_workaround:1;445435 u32 require_direct:1;436436+ u32 shadow_on_flush:1;446437};447438448439int iommu_device_register(struct iommu_device *iommu,···649638extern struct iommu_group *generic_device_group(struct device *dev);650639/* FSL-MC device grouping function */651640struct iommu_group *fsl_mc_device_group(struct device *dev);641641+extern struct iommu_group *generic_single_device_group(struct device *dev);652642653643/**654644 * struct iommu_fwspec - per-device IOMMU instance data···11211109 * Creates a mapping at @iova for the buffer described by a scatterlist11221110 * stored in the given sg_table object in the provided IOMMU domain.11231111 */11241124-static inline size_t iommu_map_sgtable(struct iommu_domain *domain,11121112+static inline ssize_t iommu_map_sgtable(struct iommu_domain *domain,11251113 unsigned long iova, struct sg_table *sgt, int prot)11261114{11271115 return iommu_map_sg(domain, iova, sgt->sgl, sgt->orig_nents, prot,