Loading...
1// SPDX-License-Identifier: GPL-2.0
2#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;
54
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) {
60 printk(KERN_ERR "CYCLIC DEPENDENCY FOUND! %pS depends on %pS and vice-versa. BREAKING IT.\n",
61 p->detect, q->detect);
62 /* Heavy handed way..*/
63 x->depend = NULL;
64 }
65 }
66
67 for (p = start; p < finish; p++) {
68 q = find_dependents_of(p, finish, p);
69 if (q && q > p) {
70 printk(KERN_ERR "EXECUTION ORDER INVALID! %pS should be called before %pS!\n",
71 p->detect, q->detect);
72 }
73 }
74}
75#else
76void __init check_iommu_entries(struct iommu_table_entry *start,
77 struct iommu_table_entry *finish)
78{
79}
80#endif
1#include <linux/dma-mapping.h>
2#include <asm/iommu_table.h>
3#include <linux/string.h>
4#include <linux/kallsyms.h>
5
6
7#define DEBUG 1
8
9static struct iommu_table_entry * __init
10find_dependents_of(struct iommu_table_entry *start,
11 struct iommu_table_entry *finish,
12 struct iommu_table_entry *q)
13{
14 struct iommu_table_entry *p;
15
16 if (!q)
17 return NULL;
18
19 for (p = start; p < finish; p++)
20 if (p->detect == q->depend)
21 return p;
22
23 return NULL;
24}
25
26
27void __init sort_iommu_table(struct iommu_table_entry *start,
28 struct iommu_table_entry *finish) {
29
30 struct iommu_table_entry *p, *q, tmp;
31
32 for (p = start; p < finish; p++) {
33again:
34 q = find_dependents_of(start, finish, p);
35 /* We are bit sneaky here. We use the memory address to figure
36 * out if the node we depend on is past our point, if so, swap.
37 */
38 if (q > p) {
39 tmp = *p;
40 memmove(p, q, sizeof(*p));
41 *q = tmp;
42 goto again;
43 }
44 }
45
46}
47
48#ifdef DEBUG
49void __init check_iommu_entries(struct iommu_table_entry *start,
50 struct iommu_table_entry *finish)
51{
52 struct iommu_table_entry *p, *q, *x;
53
54 /* Simple cyclic dependency checker. */
55 for (p = start; p < finish; p++) {
56 q = find_dependents_of(start, finish, p);
57 x = find_dependents_of(start, finish, q);
58 if (p == x) {
59 printk(KERN_ERR "CYCLIC DEPENDENCY FOUND! %pS depends on %pS and vice-versa. BREAKING IT.\n",
60 p->detect, q->detect);
61 /* Heavy handed way..*/
62 x->depend = 0;
63 }
64 }
65
66 for (p = start; p < finish; p++) {
67 q = find_dependents_of(p, finish, p);
68 if (q && q > p) {
69 printk(KERN_ERR "EXECUTION ORDER INVALID! %pS should be called before %pS!\n",
70 p->detect, q->detect);
71 }
72 }
73}
74#else
75void __init check_iommu_entries(struct iommu_table_entry *start,
76 struct iommu_table_entry *finish)
77{
78}
79#endif