1 | /* |
---|
2 | * This program is free software; you can redistribute it and/or modify |
---|
3 | * it under the terms of the GNU General Public License as published by |
---|
4 | * the Free Software Foundation; either version 2 of the License, or |
---|
5 | * (at your option) any later version. |
---|
6 | * |
---|
7 | * This program is distributed in the hope that it will be useful, |
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | * GNU General Public License for more details. |
---|
11 | * |
---|
12 | * You should have received a copy of the GNU General Public License |
---|
13 | * along with this program; if not, write to the Free Software |
---|
14 | * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
15 | * |
---|
16 | * Copyright (C) IBM Corp. 2005 |
---|
17 | * |
---|
18 | * Authors: Jimi Xenidis <jimix@watson.ibm.com> |
---|
19 | */ |
---|
20 | |
---|
21 | #include "ofh.h" |
---|
22 | |
---|
23 | struct of_malloc_s { |
---|
24 | u32 ofm_start; |
---|
25 | u32 ofm_end; |
---|
26 | }; |
---|
27 | static struct of_malloc_s claimed[64]; |
---|
28 | |
---|
29 | static s32 |
---|
30 | claim(ulong b, u32 virt, u32 size, u32 align, s32 *baseaddr) |
---|
31 | { |
---|
32 | struct of_malloc_s *cp; |
---|
33 | u32 i; |
---|
34 | s32 e; |
---|
35 | u32 end; |
---|
36 | |
---|
37 | if (align != 0) { |
---|
38 | /* we don't do this now */ |
---|
39 | return OF_FAILURE; |
---|
40 | } |
---|
41 | |
---|
42 | end = virt + size; |
---|
43 | |
---|
44 | /* you cannot claim OF's own space */ |
---|
45 | if (virt >= (u32)ofh_start && end < (u32)_end) { |
---|
46 | return OF_FAILURE; |
---|
47 | } |
---|
48 | |
---|
49 | cp = DRELA(&claimed[0], b); |
---|
50 | /* don't care about speed at the moment */ |
---|
51 | e = -1; |
---|
52 | for (i = 0; i < sizeof (claimed)/sizeof (claimed[0]); i++) { |
---|
53 | if (cp[i].ofm_end == 0) { |
---|
54 | if (e == -1) { |
---|
55 | e = i; |
---|
56 | } |
---|
57 | continue; |
---|
58 | } |
---|
59 | if (virt >= cp[i].ofm_start && virt < cp[i].ofm_end) { |
---|
60 | return OF_FAILURE; |
---|
61 | } |
---|
62 | if (end >= cp[i].ofm_start && end < cp[i].ofm_end) { |
---|
63 | return OF_FAILURE; |
---|
64 | } |
---|
65 | } |
---|
66 | /* e points to the first empty */ |
---|
67 | cp[e].ofm_start = virt; |
---|
68 | cp[e].ofm_end = end; |
---|
69 | *baseaddr = virt; |
---|
70 | return OF_SUCCESS; |
---|
71 | } |
---|
72 | |
---|
73 | s32 |
---|
74 | ofh_claim(u32 nargs, u32 nrets, s32 argp[], s32 retp[], ulong b) |
---|
75 | { |
---|
76 | if (nargs == 3) { |
---|
77 | if (nrets == 1) { |
---|
78 | u32 virt = argp[0]; |
---|
79 | u32 size = argp[1]; |
---|
80 | u32 align = argp[2]; |
---|
81 | s32 *baseaddr = &retp[0]; |
---|
82 | |
---|
83 | return claim(b, virt, size, align, baseaddr); |
---|
84 | } |
---|
85 | } |
---|
86 | return OF_FAILURE; |
---|
87 | } |
---|
88 | |
---|
89 | static s32 |
---|
90 | release(ulong b, u32 virt, u32 size) |
---|
91 | { |
---|
92 | struct of_malloc_s *cp; |
---|
93 | u32 i; |
---|
94 | u32 end; |
---|
95 | |
---|
96 | end = virt + size; |
---|
97 | |
---|
98 | /* you cannot release OF's own space */ |
---|
99 | if (virt >= (u32)ofh_start && end < (u32)_end) { |
---|
100 | return OF_FAILURE; |
---|
101 | } |
---|
102 | |
---|
103 | cp = DRELA(&claimed[0], b); |
---|
104 | /* don't care about speed at the moment */ |
---|
105 | for (i = 0; i < sizeof (claimed)/sizeof (claimed[0]); i++) { |
---|
106 | if (virt == cp[i].ofm_start && end == cp[i].ofm_end) { |
---|
107 | cp[i].ofm_start = 0; |
---|
108 | cp[i].ofm_end = 0; |
---|
109 | return OF_SUCCESS; |
---|
110 | } |
---|
111 | } |
---|
112 | return OF_FAILURE; |
---|
113 | } |
---|
114 | |
---|
115 | s32 |
---|
116 | ofh_release(u32 nargs, u32 nrets, s32 argp[], |
---|
117 | s32 retp[] __attribute__ ((unused)), |
---|
118 | ulong b) |
---|
119 | { |
---|
120 | if (nargs == 2) { |
---|
121 | if (nrets == 0) { |
---|
122 | u32 virt = argp[0]; |
---|
123 | u32 size = argp[1]; |
---|
124 | |
---|
125 | return release(b, virt, size); |
---|
126 | } |
---|
127 | } |
---|
128 | return OF_FAILURE; |
---|
129 | } |
---|