2 * Copyright (c) 2014-2019 François Tigeot <ftigeot@wolfpond.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
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.
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.
27 #include <machine/pmap.h>
31 #include <linux/kernel.h>
32 #include <linux/slab.h>
33 #include <linux/bug.h>
37 struct lock iomap_lock = LOCK_INITIALIZER("dlioml", 0, LK_CANRECURSE);
39 SLIST_HEAD(iomap_list_head, iomap) iomap_list = SLIST_HEAD_INITIALIZER(iomap_list);
42 __ioremap_common(unsigned long phys_addr, unsigned long size, int cache_mode)
46 /* Ensure mappings are page-aligned */
47 BUG_ON(phys_addr & PAGE_MASK);
48 BUG_ON(size & PAGE_MASK);
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);
58 return imp->pmap_addr;
61 void iounmap(void __iomem *ptr)
63 struct iomap *imp, *tmp_imp;
68 SLIST_FOREACH_MUTABLE(imp, &iomap_list, im_iomaps, tmp_imp) {
69 if (imp->pmap_addr == ptr) {
76 kprintf("iounmap: invalid address %p\n", ptr);
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;
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);
96 pmap_unmapdev((vm_offset_t)imp->pmap_addr, imp->npages * PAGE_SIZE);
98 lockmgr(&iomap_lock, LK_EXCLUSIVE);
99 SLIST_REMOVE(&iomap_list, imp, iomap, im_iomaps);
100 lockmgr(&iomap_lock, LK_RELEASE);
105 #include <sys/memrange.h>
108 arch_io_reserve_memtype_wc(resource_size_t start, resource_size_t size)
111 struct mem_range_desc mrdesc;
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);