1 /* 2 * Copyright (c) 2014-2019 François Tigeot <ftigeot@wolfpond.org> 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 <linux/bug.h> 34 #include <asm/page.h> 35 #include <asm/io.h> 36 37 struct lock iomap_lock = LOCK_INITIALIZER("dlioml", 0, LK_CANRECURSE); 38 39 SLIST_HEAD(iomap_list_head, iomap) iomap_list = SLIST_HEAD_INITIALIZER(iomap_list); 40 41 void __iomem * 42 __ioremap_common(unsigned long phys_addr, unsigned long size, int cache_mode) 43 { 44 struct iomap *imp; 45 46 /* Ensure mappings are page-aligned */ 47 BUG_ON(phys_addr & PAGE_MASK); 48 BUG_ON(size & PAGE_MASK); 49 50 imp = kmalloc(sizeof(struct iomap), M_DRM, M_WAITOK); 51 imp->paddr = phys_addr; 52 imp->npages = size / PAGE_SIZE; 53 imp->pmap_addr = pmap_mapdev_attr(phys_addr, size, cache_mode); 54 lockmgr(&iomap_lock, LK_EXCLUSIVE); 55 SLIST_INSERT_HEAD(&iomap_list, imp, im_iomaps); 56 lockmgr(&iomap_lock, LK_RELEASE); 57 58 return imp->pmap_addr; 59 } 60 61 void iounmap(void __iomem *ptr) 62 { 63 struct iomap *imp, *tmp_imp; 64 int found = 0; 65 int indx; 66 vm_paddr_t paddr_end; 67 68 SLIST_FOREACH_MUTABLE(imp, &iomap_list, im_iomaps, tmp_imp) { 69 if (imp->pmap_addr == ptr) { 70 found = 1; 71 break; 72 } 73 } 74 75 if (!found) { 76 kprintf("iounmap: invalid address %p\n", ptr); 77 return; 78 } 79 80 paddr_end = imp->paddr + (imp->npages * PAGE_SIZE) - 1; 81 /* Is this address space range backed by regular memory ? */ 82 for (indx = 0; phys_avail[indx].phys_end != 0; ++indx) { 83 vm_paddr_t range_start = phys_avail[indx].phys_beg; 84 vm_paddr_t size = phys_avail[indx].phys_end - 85 phys_avail[indx].phys_beg; 86 vm_paddr_t range_end = range_start + size - 1; 87 88 if ((imp->paddr >= range_start) && (paddr_end <= range_end)) { 89 /* Yes, change page caching attributes */ 90 pmap_change_attr(imp->paddr, imp->npages, PAT_WRITE_BACK); 91 break; 92 } 93 94 } 95 96 pmap_unmapdev((vm_offset_t)imp->pmap_addr, imp->npages * PAGE_SIZE); 97 98 lockmgr(&iomap_lock, LK_EXCLUSIVE); 99 SLIST_REMOVE(&iomap_list, imp, iomap, im_iomaps); 100 lockmgr(&iomap_lock, LK_RELEASE); 101 102 kfree(imp); 103 } 104 105 #include <sys/memrange.h> 106 107 int 108 arch_io_reserve_memtype_wc(resource_size_t start, resource_size_t size) 109 { 110 int act; 111 struct mem_range_desc mrdesc; 112 113 mrdesc.mr_base = start; 114 mrdesc.mr_len = size; 115 mrdesc.mr_flags = MDF_WRITECOMBINE; 116 act = MEMRANGE_SET_UPDATE; 117 strlcpy(mrdesc.mr_owner, "drm", sizeof(mrdesc.mr_owner)); 118 return mem_range_attr_set(&mrdesc, &act); 119 } 120