···11+/*22+ * Meta version derived from arch/powerpc/lib/dma-noncoherent.c33+ * Copyright (C) 2008 Imagination Technologies Ltd.44+ *55+ * PowerPC version derived from arch/arm/mm/consistent.c66+ * Copyright (C) 2001 Dan Malek (dmalek@jlc.net)77+ *88+ * Copyright (C) 2000 Russell King99+ *1010+ * Consistent memory allocators. Used for DMA devices that want to1111+ * share uncached memory with the processor core. The function return1212+ * is the virtual address and 'dma_handle' is the physical address.1313+ * Mostly stolen from the ARM port, with some changes for PowerPC.1414+ * -- Dan1515+ *1616+ * Reorganized to get rid of the arch-specific consistent_* functions1717+ * and provide non-coherent implementations for the DMA API. -Matt1818+ *1919+ * Added in_interrupt() safe dma_alloc_coherent()/dma_free_coherent()2020+ * implementation. This is pulled straight from ARM and barely2121+ * modified. -Matt2222+ *2323+ * This program is free software; you can redistribute it and/or modify2424+ * it under the terms of the GNU General Public License version 2 as2525+ * published by the Free Software Foundation.2626+ */2727+2828+#include <linux/sched.h>2929+#include <linux/kernel.h>3030+#include <linux/errno.h>3131+#include <linux/export.h>3232+#include <linux/string.h>3333+#include <linux/types.h>3434+#include <linux/highmem.h>3535+#include <linux/dma-mapping.h>3636+#include <linux/slab.h>3737+3838+#include <asm/tlbflush.h>3939+#include <asm/mmu.h>4040+4141+#define CONSISTENT_OFFSET(x) (((unsigned long)(x) - CONSISTENT_START) \4242+ >> PAGE_SHIFT)4343+4444+static u64 get_coherent_dma_mask(struct device *dev)4545+{4646+ u64 mask = ~0ULL;4747+4848+ if (dev) {4949+ mask = dev->coherent_dma_mask;5050+5151+ /*5252+ * Sanity check the DMA mask - it must be non-zero, and5353+ * must be able to be satisfied by a DMA allocation.5454+ */5555+ if (mask == 0) {5656+ dev_warn(dev, "coherent DMA mask is unset\n");5757+ return 0;5858+ }5959+ }6060+6161+ return mask;6262+}6363+/*6464+ * This is the page table (2MB) covering uncached, DMA consistent allocations6565+ */6666+static pte_t *consistent_pte;6767+static DEFINE_SPINLOCK(consistent_lock);6868+6969+/*7070+ * VM region handling support.7171+ *7272+ * This should become something generic, handling VM region allocations for7373+ * vmalloc and similar (ioremap, module space, etc).7474+ *7575+ * I envisage vmalloc()'s supporting vm_struct becoming:7676+ *7777+ * struct vm_struct {7878+ * struct metag_vm_region region;7979+ * unsigned long flags;8080+ * struct page **pages;8181+ * unsigned int nr_pages;8282+ * unsigned long phys_addr;8383+ * };8484+ *8585+ * get_vm_area() would then call metag_vm_region_alloc with an appropriate8686+ * struct metag_vm_region head (eg):8787+ *8888+ * struct metag_vm_region vmalloc_head = {8989+ * .vm_list = LIST_HEAD_INIT(vmalloc_head.vm_list),9090+ * .vm_start = VMALLOC_START,9191+ * .vm_end = VMALLOC_END,9292+ * };9393+ *9494+ * However, vmalloc_head.vm_start is variable (typically, it is dependent on9595+ * the amount of RAM found at boot time.) I would imagine that get_vm_area()9696+ * would have to initialise this each time prior to calling9797+ * metag_vm_region_alloc().9898+ */9999+struct metag_vm_region {100100+ struct list_head vm_list;101101+ unsigned long vm_start;102102+ unsigned long vm_end;103103+ struct page *vm_pages;104104+ int vm_active;105105+};106106+107107+static struct metag_vm_region consistent_head = {108108+ .vm_list = LIST_HEAD_INIT(consistent_head.vm_list),109109+ .vm_start = CONSISTENT_START,110110+ .vm_end = CONSISTENT_END,111111+};112112+113113+static struct metag_vm_region *metag_vm_region_alloc(struct metag_vm_region114114+ *head, size_t size,115115+ gfp_t gfp)116116+{117117+ unsigned long addr = head->vm_start, end = head->vm_end - size;118118+ unsigned long flags;119119+ struct metag_vm_region *c, *new;120120+121121+ new = kmalloc(sizeof(struct metag_vm_region), gfp);122122+ if (!new)123123+ goto out;124124+125125+ spin_lock_irqsave(&consistent_lock, flags);126126+127127+ list_for_each_entry(c, &head->vm_list, vm_list) {128128+ if ((addr + size) < addr)129129+ goto nospc;130130+ if ((addr + size) <= c->vm_start)131131+ goto found;132132+ addr = c->vm_end;133133+ if (addr > end)134134+ goto nospc;135135+ }136136+137137+found:138138+ /*139139+ * Insert this entry _before_ the one we found.140140+ */141141+ list_add_tail(&new->vm_list, &c->vm_list);142142+ new->vm_start = addr;143143+ new->vm_end = addr + size;144144+ new->vm_active = 1;145145+146146+ spin_unlock_irqrestore(&consistent_lock, flags);147147+ return new;148148+149149+nospc:150150+ spin_unlock_irqrestore(&consistent_lock, flags);151151+ kfree(new);152152+out:153153+ return NULL;154154+}155155+156156+static struct metag_vm_region *metag_vm_region_find(struct metag_vm_region157157+ *head, unsigned long addr)158158+{159159+ struct metag_vm_region *c;160160+161161+ list_for_each_entry(c, &head->vm_list, vm_list) {162162+ if (c->vm_active && c->vm_start == addr)163163+ goto out;164164+ }165165+ c = NULL;166166+out:167167+ return c;168168+}169169+170170+/*171171+ * Allocate DMA-coherent memory space and return both the kernel remapped172172+ * virtual and bus address for that space.173173+ */174174+void *dma_alloc_coherent(struct device *dev, size_t size,175175+ dma_addr_t *handle, gfp_t gfp)176176+{177177+ struct page *page;178178+ struct metag_vm_region *c;179179+ unsigned long order;180180+ u64 mask = get_coherent_dma_mask(dev);181181+ u64 limit;182182+183183+ if (!consistent_pte) {184184+ pr_err("%s: not initialised\n", __func__);185185+ dump_stack();186186+ return NULL;187187+ }188188+189189+ if (!mask)190190+ goto no_page;191191+ size = PAGE_ALIGN(size);192192+ limit = (mask + 1) & ~mask;193193+ if ((limit && size >= limit)194194+ || size >= (CONSISTENT_END - CONSISTENT_START)) {195195+ pr_warn("coherent allocation too big (requested %#x mask %#Lx)\n",196196+ size, mask);197197+ return NULL;198198+ }199199+200200+ order = get_order(size);201201+202202+ if (mask != 0xffffffff)203203+ gfp |= GFP_DMA;204204+205205+ page = alloc_pages(gfp, order);206206+ if (!page)207207+ goto no_page;208208+209209+ /*210210+ * Invalidate any data that might be lurking in the211211+ * kernel direct-mapped region for device DMA.212212+ */213213+ {214214+ void *kaddr = page_address(page);215215+ memset(kaddr, 0, size);216216+ flush_dcache_region(kaddr, size);217217+ }218218+219219+ /*220220+ * Allocate a virtual address in the consistent mapping region.221221+ */222222+ c = metag_vm_region_alloc(&consistent_head, size,223223+ gfp & ~(__GFP_DMA | __GFP_HIGHMEM));224224+ if (c) {225225+ unsigned long vaddr = c->vm_start;226226+ pte_t *pte = consistent_pte + CONSISTENT_OFFSET(vaddr);227227+ struct page *end = page + (1 << order);228228+229229+ c->vm_pages = page;230230+ split_page(page, order);231231+232232+ /*233233+ * Set the "dma handle"234234+ */235235+ *handle = page_to_bus(page);236236+237237+ do {238238+ BUG_ON(!pte_none(*pte));239239+240240+ SetPageReserved(page);241241+ set_pte_at(&init_mm, vaddr,242242+ pte, mk_pte(page,243243+ pgprot_writecombine244244+ (PAGE_KERNEL)));245245+ page++;246246+ pte++;247247+ vaddr += PAGE_SIZE;248248+ } while (size -= PAGE_SIZE);249249+250250+ /*251251+ * Free the otherwise unused pages.252252+ */253253+ while (page < end) {254254+ __free_page(page);255255+ page++;256256+ }257257+258258+ return (void *)c->vm_start;259259+ }260260+261261+ if (page)262262+ __free_pages(page, order);263263+no_page:264264+ return NULL;265265+}266266+EXPORT_SYMBOL(dma_alloc_coherent);267267+268268+/*269269+ * free a page as defined by the above mapping.270270+ */271271+void dma_free_coherent(struct device *dev, size_t size,272272+ void *vaddr, dma_addr_t dma_handle)273273+{274274+ struct metag_vm_region *c;275275+ unsigned long flags, addr;276276+ pte_t *ptep;277277+278278+ size = PAGE_ALIGN(size);279279+280280+ spin_lock_irqsave(&consistent_lock, flags);281281+282282+ c = metag_vm_region_find(&consistent_head, (unsigned long)vaddr);283283+ if (!c)284284+ goto no_area;285285+286286+ c->vm_active = 0;287287+ if ((c->vm_end - c->vm_start) != size) {288288+ pr_err("%s: freeing wrong coherent size (%ld != %d)\n",289289+ __func__, c->vm_end - c->vm_start, size);290290+ dump_stack();291291+ size = c->vm_end - c->vm_start;292292+ }293293+294294+ ptep = consistent_pte + CONSISTENT_OFFSET(c->vm_start);295295+ addr = c->vm_start;296296+ do {297297+ pte_t pte = ptep_get_and_clear(&init_mm, addr, ptep);298298+ unsigned long pfn;299299+300300+ ptep++;301301+ addr += PAGE_SIZE;302302+303303+ if (!pte_none(pte) && pte_present(pte)) {304304+ pfn = pte_pfn(pte);305305+306306+ if (pfn_valid(pfn)) {307307+ struct page *page = pfn_to_page(pfn);308308+ ClearPageReserved(page);309309+310310+ __free_page(page);311311+ continue;312312+ }313313+ }314314+315315+ pr_crit("%s: bad page in kernel page table\n",316316+ __func__);317317+ } while (size -= PAGE_SIZE);318318+319319+ flush_tlb_kernel_range(c->vm_start, c->vm_end);320320+321321+ list_del(&c->vm_list);322322+323323+ spin_unlock_irqrestore(&consistent_lock, flags);324324+325325+ kfree(c);326326+ return;327327+328328+no_area:329329+ spin_unlock_irqrestore(&consistent_lock, flags);330330+ pr_err("%s: trying to free invalid coherent area: %p\n",331331+ __func__, vaddr);332332+ dump_stack();333333+}334334+EXPORT_SYMBOL(dma_free_coherent);335335+336336+337337+static int dma_mmap(struct device *dev, struct vm_area_struct *vma,338338+ void *cpu_addr, dma_addr_t dma_addr, size_t size)339339+{340340+ int ret = -ENXIO;341341+342342+ unsigned long flags, user_size, kern_size;343343+ struct metag_vm_region *c;344344+345345+ user_size = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;346346+347347+ spin_lock_irqsave(&consistent_lock, flags);348348+ c = metag_vm_region_find(&consistent_head, (unsigned long)cpu_addr);349349+ spin_unlock_irqrestore(&consistent_lock, flags);350350+351351+ if (c) {352352+ unsigned long off = vma->vm_pgoff;353353+354354+ kern_size = (c->vm_end - c->vm_start) >> PAGE_SHIFT;355355+356356+ if (off < kern_size &&357357+ user_size <= (kern_size - off)) {358358+ ret = remap_pfn_range(vma, vma->vm_start,359359+ page_to_pfn(c->vm_pages) + off,360360+ user_size << PAGE_SHIFT,361361+ vma->vm_page_prot);362362+ }363363+ }364364+365365+366366+ return ret;367367+}368368+369369+int dma_mmap_coherent(struct device *dev, struct vm_area_struct *vma,370370+ void *cpu_addr, dma_addr_t dma_addr, size_t size)371371+{372372+ vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);373373+ return dma_mmap(dev, vma, cpu_addr, dma_addr, size);374374+}375375+EXPORT_SYMBOL(dma_mmap_coherent);376376+377377+int dma_mmap_writecombine(struct device *dev, struct vm_area_struct *vma,378378+ void *cpu_addr, dma_addr_t dma_addr, size_t size)379379+{380380+ vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);381381+ return dma_mmap(dev, vma, cpu_addr, dma_addr, size);382382+}383383+EXPORT_SYMBOL(dma_mmap_writecombine);384384+385385+386386+387387+388388+/*389389+ * Initialise the consistent memory allocation.390390+ */391391+static int __init dma_alloc_init(void)392392+{393393+ pgd_t *pgd, *pgd_k;394394+ pud_t *pud, *pud_k;395395+ pmd_t *pmd, *pmd_k;396396+ pte_t *pte;397397+ int ret = 0;398398+399399+ do {400400+ int offset = pgd_index(CONSISTENT_START);401401+ pgd = pgd_offset(&init_mm, CONSISTENT_START);402402+ pud = pud_alloc(&init_mm, pgd, CONSISTENT_START);403403+ pmd = pmd_alloc(&init_mm, pud, CONSISTENT_START);404404+ if (!pmd) {405405+ pr_err("%s: no pmd tables\n", __func__);406406+ ret = -ENOMEM;407407+ break;408408+ }409409+ WARN_ON(!pmd_none(*pmd));410410+411411+ pte = pte_alloc_kernel(pmd, CONSISTENT_START);412412+ if (!pte) {413413+ pr_err("%s: no pte tables\n", __func__);414414+ ret = -ENOMEM;415415+ break;416416+ }417417+418418+ pgd_k = ((pgd_t *) mmu_get_base()) + offset;419419+ pud_k = pud_offset(pgd_k, CONSISTENT_START);420420+ pmd_k = pmd_offset(pud_k, CONSISTENT_START);421421+ set_pmd(pmd_k, *pmd);422422+423423+ consistent_pte = pte;424424+ } while (0);425425+426426+ return ret;427427+}428428+early_initcall(dma_alloc_init);429429+430430+/*431431+ * make an area consistent to devices.432432+ */433433+void dma_sync_for_device(void *vaddr, size_t size, int dma_direction)434434+{435435+ /*436436+ * Ensure any writes get through the write combiner. This is necessary437437+ * even with DMA_FROM_DEVICE, or the write may dirty the cache after438438+ * we've invalidated it and get written back during the DMA.439439+ */440440+441441+ barrier();442442+443443+ switch (dma_direction) {444444+ case DMA_BIDIRECTIONAL:445445+ /*446446+ * Writeback to ensure the device can see our latest changes and447447+ * so that we have no dirty lines, and invalidate the cache448448+ * lines too in preparation for receiving the buffer back449449+ * (dma_sync_for_cpu) later.450450+ */451451+ flush_dcache_region(vaddr, size);452452+ break;453453+ case DMA_TO_DEVICE:454454+ /*455455+ * Writeback to ensure the device can see our latest changes.456456+ * There's no need to invalidate as the device shouldn't write457457+ * to the buffer.458458+ */459459+ writeback_dcache_region(vaddr, size);460460+ break;461461+ case DMA_FROM_DEVICE:462462+ /*463463+ * Invalidate to ensure we have no dirty lines that could get464464+ * written back during the DMA. It's also safe to flush465465+ * (writeback) here if necessary.466466+ */467467+ invalidate_dcache_region(vaddr, size);468468+ break;469469+ case DMA_NONE:470470+ BUG();471471+ }472472+473473+ wmb();474474+}475475+EXPORT_SYMBOL(dma_sync_for_device);476476+477477+/*478478+ * make an area consistent to the core.479479+ */480480+void dma_sync_for_cpu(void *vaddr, size_t size, int dma_direction)481481+{482482+ /*483483+ * Hardware L2 cache prefetch doesn't occur across 4K physical484484+ * boundaries, however according to Documentation/DMA-API-HOWTO.txt485485+ * kmalloc'd memory is DMA'able, so accesses in nearby memory could486486+ * trigger a cache fill in the DMA buffer.487487+ *488488+ * This should never cause dirty lines, so a flush or invalidate should489489+ * be safe to allow us to see data from the device.490490+ */491491+ if (_meta_l2c_pf_is_enabled()) {492492+ switch (dma_direction) {493493+ case DMA_BIDIRECTIONAL:494494+ case DMA_FROM_DEVICE:495495+ invalidate_dcache_region(vaddr, size);496496+ break;497497+ case DMA_TO_DEVICE:498498+ /* The device shouldn't have written to the buffer */499499+ break;500500+ case DMA_NONE:501501+ BUG();502502+ }503503+ }504504+505505+ rmb();506506+}507507+EXPORT_SYMBOL(dma_sync_for_cpu);