1 /* 2 * Copyright (c) 2014-2016 François Tigeot 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions, and the following 10 * disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <machine/pmap.h> 28 #include <vm/pmap.h> 29 #include <vm/vm.h> 30 31 #include <linux/kernel.h> 32 #include <linux/slab.h> 33 #include <asm/page.h> 34 #include <asm/io.h> 35 36 SLIST_HEAD(iomap_list_head, iomap) iomap_list = SLIST_HEAD_INITIALIZER(iomap_list); 37 38 struct iomap * __ioremap_common(unsigned long phys_addr, unsigned long size) 39 { 40 struct iomap *imp; 41 42 /* Ensure mappings are page-aligned */ 43 BUG_ON(phys_addr & PAGE_MASK); 44 BUG_ON(size & PAGE_MASK); 45 46 imp = kmalloc(sizeof(struct iomap), M_DRM, M_WAITOK); 47 imp->paddr = phys_addr; 48 imp->npages = size / PAGE_SIZE; 49 SLIST_INSERT_HEAD(&iomap_list, imp, im_iomaps); 50 51 return imp; 52 } 53 54 void iounmap(void __iomem *ptr) 55 { 56 struct iomap *imp, *tmp_imp; 57 int found = 0; 58 int indx; 59 vm_paddr_t paddr_end; 60 61 SLIST_FOREACH_MUTABLE(imp, &iomap_list, im_iomaps, tmp_imp) { 62 if (imp->pmap_addr == ptr) { 63 found = 1; 64 break; 65 } 66 } 67 68 if (!found) { 69 kprintf("iounmap: invalid address %p\n", ptr); 70 return; 71 } 72 73 paddr_end = imp->paddr + (imp->npages * PAGE_SIZE) - 1; 74 /* Is this address space range backed by regular memory ? */ 75 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) { 76 vm_paddr_t range_start = phys_avail[indx]; 77 vm_paddr_t size = phys_avail[indx + 1] - phys_avail[indx]; 78 vm_paddr_t range_end = range_start + size - 1; 79 80 if ((imp->paddr >= range_start) && (paddr_end <= range_end)) { 81 /* Yes, change page caching attributes */ 82 pmap_change_attr(imp->paddr, imp->npages, PAT_WRITE_BACK); 83 break; 84 } 85 86 } 87 88 pmap_unmapdev((vm_offset_t)imp->pmap_addr, imp->npages * PAGE_SIZE); 89 90 SLIST_REMOVE(&iomap_list, imp, iomap, im_iomaps); 91 kfree(imp); 92 } 93