blob: 2e9006c1e240884b5f5fa28e0b4e442f39d412c4 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Konrad Rzeszutek Wilk5bef80a2010-08-26 13:57:58 -04002#include <linux/dma-mapping.h>
3#include <asm/iommu_table.h>
4#include <linux/string.h>
5#include <linux/kallsyms.h>
6
7
8#define DEBUG 1
9
10static struct iommu_table_entry * __init
11find_dependents_of(struct iommu_table_entry *start,
12 struct iommu_table_entry *finish,
13 struct iommu_table_entry *q)
14{
15 struct iommu_table_entry *p;
16
17 if (!q)
18 return NULL;
19
20 for (p = start; p < finish; p++)
21 if (p->detect == q->depend)
22 return p;
23
24 return NULL;
25}
26
27
28void __init sort_iommu_table(struct iommu_table_entry *start,
29 struct iommu_table_entry *finish) {
30
31 struct iommu_table_entry *p, *q, tmp;
32
33 for (p = start; p < finish; p++) {
34again:
35 q = find_dependents_of(start, finish, p);
36 /* We are bit sneaky here. We use the memory address to figure
37 * out if the node we depend on is past our point, if so, swap.
38 */
39 if (q > p) {
40 tmp = *p;
41 memmove(p, q, sizeof(*p));
42 *q = tmp;
43 goto again;
44 }
45 }
46
47}
48
49#ifdef DEBUG
50void __init check_iommu_entries(struct iommu_table_entry *start,
51 struct iommu_table_entry *finish)
52{
53 struct iommu_table_entry *p, *q, *x;
Konrad Rzeszutek Wilk5bef80a2010-08-26 13:57:58 -040054
55 /* Simple cyclic dependency checker. */
56 for (p = start; p < finish; p++) {
57 q = find_dependents_of(start, finish, p);
58 x = find_dependents_of(start, finish, q);
59 if (p == x) {
Joe Perchese9696872010-11-05 16:12:35 -070060 printk(KERN_ERR "CYCLIC DEPENDENCY FOUND! %pS depends on %pS and vice-versa. BREAKING IT.\n",
61 p->detect, q->detect);
Konrad Rzeszutek Wilk5bef80a2010-08-26 13:57:58 -040062 /* Heavy handed way..*/
Zhong Jiang0b2c1ae2018-07-21 16:33:24 +080063 x->depend = NULL;
Konrad Rzeszutek Wilk5bef80a2010-08-26 13:57:58 -040064 }
65 }
66
67 for (p = start; p < finish; p++) {
68 q = find_dependents_of(p, finish, p);
69 if (q && q > p) {
Joe Perchese9696872010-11-05 16:12:35 -070070 printk(KERN_ERR "EXECUTION ORDER INVALID! %pS should be called before %pS!\n",
71 p->detect, q->detect);
Konrad Rzeszutek Wilk5bef80a2010-08-26 13:57:58 -040072 }
73 }
74}
75#else
Denys Vlasenkoa3819e32016-04-15 19:00:26 +020076void __init check_iommu_entries(struct iommu_table_entry *start,
Konrad Rzeszutek Wilk5bef80a2010-08-26 13:57:58 -040077 struct iommu_table_entry *finish)
78{
79}
80#endif