xref: /dragonfly/sys/dev/drm/linux_iomapping.c (revision 65867155)
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 <linux/bug.h>
34 #include <asm/page.h>
35 #include <asm/io.h>
36 
37 SLIST_HEAD(iomap_list_head, iomap) iomap_list = SLIST_HEAD_INITIALIZER(iomap_list);
38 
39 struct iomap * __ioremap_common(unsigned long phys_addr, unsigned long size)
40 {
41 	struct iomap *imp;
42 
43 	/* Ensure mappings are page-aligned */
44 	BUG_ON(phys_addr & PAGE_MASK);
45 	BUG_ON(size & PAGE_MASK);
46 
47 	imp = kmalloc(sizeof(struct iomap), M_DRM, M_WAITOK);
48 	imp->paddr = phys_addr;
49 	imp->npages = size / PAGE_SIZE;
50 	SLIST_INSERT_HEAD(&iomap_list, imp, im_iomaps);
51 
52 	return imp;
53 }
54 
55 void iounmap(void __iomem *ptr)
56 {
57 	struct iomap *imp, *tmp_imp;
58 	int found = 0;
59 	int indx;
60 	vm_paddr_t paddr_end;
61 
62 	SLIST_FOREACH_MUTABLE(imp, &iomap_list, im_iomaps, tmp_imp) {
63 		if (imp->pmap_addr == ptr) {
64 			found = 1;
65 			break;
66 		}
67 	}
68 
69 	if (!found) {
70 		kprintf("iounmap: invalid address %p\n", ptr);
71 		return;
72 	}
73 
74 	paddr_end = imp->paddr + (imp->npages * PAGE_SIZE) - 1;
75 	/* Is this address space range backed by regular memory ? */
76 	for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
77 		vm_paddr_t range_start = phys_avail[indx];
78 		vm_paddr_t size = phys_avail[indx + 1] - phys_avail[indx];
79 		vm_paddr_t range_end = range_start + size - 1;
80 
81 		if ((imp->paddr >= range_start) && (paddr_end <= range_end)) {
82 			/* Yes, change page caching attributes */
83 			pmap_change_attr(imp->paddr, imp->npages, PAT_WRITE_BACK);
84 			break;
85 		}
86 
87 	}
88 
89 	pmap_unmapdev((vm_offset_t)imp->pmap_addr, imp->npages * PAGE_SIZE);
90 
91 	SLIST_REMOVE(&iomap_list, imp, iomap, im_iomaps);
92 	kfree(imp);
93 }
94