Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | /* * * Procedures for interfacing to Open Firmware. * * Peter Bergner, IBM Corp. June 2001. * Copyright (C) 2001 Peter Bergner. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. */ #include <linux/config.h> #include <linux/kernel.h> #include <asm/types.h> #include <asm/page.h> #include <asm/prom.h> #include <asm/lmb.h> #include <asm/abs_addr.h> #include <asm/bitops.h> #include <asm/udbg.h> extern unsigned long klimit; extern unsigned long reloc_offset(void); static long lmb_add_region(struct lmb_region *, unsigned long, unsigned long, unsigned long); struct lmb lmb = { 0, 0, {0,0,0,0,{{0,0,0}}}, {0,0,0,0,{{0,0,0}}} }; /* Assumption: base addr of region 1 < base addr of region 2 */ static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1, unsigned long r2) { unsigned long i; rgn->region[r1].size += rgn->region[r2].size; for (i=r2; i < rgn->cnt-1 ;i++) { rgn->region[i].base = rgn->region[i+1].base; rgn->region[i].physbase = rgn->region[i+1].physbase; rgn->region[i].size = rgn->region[i+1].size; rgn->region[i].type = rgn->region[i+1].type; } rgn->cnt--; } /* This routine called with relocation disabled. */ void lmb_init(void) { unsigned long offset = reloc_offset(); struct lmb *_lmb = PTRRELOC(&lmb); /* Create a dummy zero size LMB which will get coalesced away later. * This simplifies the lmb_add() code below... */ _lmb->memory.region[0].base = 0; _lmb->memory.region[0].size = 0; _lmb->memory.region[0].type = LMB_MEMORY_AREA; _lmb->memory.cnt = 1; /* Ditto. */ _lmb->reserved.region[0].base = 0; _lmb->reserved.region[0].size = 0; _lmb->reserved.region[0].type = LMB_MEMORY_AREA; _lmb->reserved.cnt = 1; } /* This is only used here, it doesnt deserve to be in bitops.h */ static __inline__ long cnt_trailing_zeros(unsigned long mask) { long cnt; asm( " addi %0,%1,-1 \n\ andc %0,%0,%1 \n\ cntlzd %0,%0 \n\ subfic %0,%0,64" : "=r" (cnt) : "r" (mask)); return cnt; } /* This routine called with relocation disabled. */ void lmb_analyze(void) { unsigned long i; unsigned long mem_size = 0; unsigned long io_size = 0; unsigned long size_mask = 0; unsigned long offset = reloc_offset(); struct lmb *_lmb = PTRRELOC(&lmb); #ifdef CONFIG_MSCHUNKS unsigned long physbase = 0; #endif for (i=0; i < _lmb->memory.cnt ;i++) { unsigned long lmb_type = _lmb->memory.region[i].type; unsigned long lmb_size; if ( lmb_type != LMB_MEMORY_AREA ) continue; lmb_size = _lmb->memory.region[i].size; #ifdef CONFIG_MSCHUNKS _lmb->memory.region[i].physbase = physbase; physbase += lmb_size; #else _lmb->memory.region[i].physbase = _lmb->memory.region[i].base; #endif mem_size += lmb_size; size_mask |= lmb_size; } #ifdef CONFIG_MSCHUNKS for (i=0; i < _lmb->memory.cnt ;i++) { unsigned long lmb_type = _lmb->memory.region[i].type; unsigned long lmb_size; if ( lmb_type != LMB_IO_AREA ) continue; lmb_size = _lmb->memory.region[i].size; _lmb->memory.region[i].physbase = physbase; physbase += lmb_size; io_size += lmb_size; size_mask |= lmb_size; } #endif /* CONFIG_MSCHUNKS */ _lmb->memory.size = mem_size; _lmb->memory.iosize = io_size; _lmb->memory.lcd_size = (1UL << cnt_trailing_zeros(size_mask)); } /* This routine called with relocation disabled. */ long lmb_add(unsigned long base, unsigned long size) { unsigned long offset = reloc_offset(); struct lmb *_lmb = PTRRELOC(&lmb); struct lmb_region *_rgn = &(_lmb->memory); /* On pSeries LPAR systems, the first LMB is our RMO region. */ if ( base == 0 ) _lmb->rmo_size = size; return lmb_add_region(_rgn, base, size, LMB_MEMORY_AREA); } #ifdef CONFIG_MSCHUNKS /* This routine called with relocation disabled. */ long lmb_add_io(unsigned long base, unsigned long size) { unsigned long offset = reloc_offset(); struct lmb *_lmb = PTRRELOC(&lmb); struct lmb_region *_rgn = &(_lmb->memory); return lmb_add_region(_rgn, base, size, LMB_IO_AREA); } #endif /* CONFIG_MSCHUNKS */ long lmb_reserve(unsigned long base, unsigned long size) { unsigned long offset = reloc_offset(); struct lmb *_lmb = PTRRELOC(&lmb); struct lmb_region *_rgn = &(_lmb->reserved); return lmb_add_region(_rgn, base, size, LMB_MEMORY_AREA); } /* This routine called with relocation disabled. */ static long lmb_add_region(struct lmb_region *rgn, unsigned long base, unsigned long size, unsigned long type) { unsigned long i, coalesced = 0; long adjacent; /* First try and coalesce this LMB with another. */ for (i=0; i < rgn->cnt ;i++) { unsigned long rgnbase = rgn->region[i].base; unsigned long rgnsize = rgn->region[i].size; unsigned long rgntype = rgn->region[i].type; if ( rgntype != type ) continue; adjacent = lmb_addrs_adjacent(base,size,rgnbase,rgnsize); if ( adjacent > 0 ) { rgn->region[i].base -= size; rgn->region[i].physbase -= size; rgn->region[i].size += size; coalesced++; break; } else if ( adjacent < 0 ) { rgn->region[i].size += size; coalesced++; break; } } if ((i < rgn->cnt-1) && lmb_regions_adjacent(rgn, i, i+1) ) { lmb_coalesce_regions(rgn, i, i+1); coalesced++; } if ( coalesced ) { return coalesced; } else if ( rgn->cnt >= MAX_LMB_REGIONS ) { return -1; } /* Couldn't coalesce the LMB, so add it to the sorted table. */ for (i=rgn->cnt-1; i >= 0 ;i--) { if (base < rgn->region[i].base) { rgn->region[i+1].base = rgn->region[i].base; rgn->region[i+1].physbase = rgn->region[i].physbase; rgn->region[i+1].size = rgn->region[i].size; rgn->region[i+1].type = rgn->region[i].type; } else { rgn->region[i+1].base = base; rgn->region[i+1].physbase = lmb_abs_to_phys(base); rgn->region[i+1].size = size; rgn->region[i+1].type = type; break; } } rgn->cnt++; return 0; } long lmb_overlaps_region(struct lmb_region *rgn, unsigned long base, unsigned long size) { unsigned long i; for (i=0; i < rgn->cnt ;i++) { unsigned long rgnbase = rgn->region[i].base; unsigned long rgnsize = rgn->region[i].size; if ( lmb_addrs_overlap(base,size,rgnbase,rgnsize) ) { break; } } return (i < rgn->cnt) ? i : -1; } unsigned long lmb_alloc(unsigned long size, unsigned long align) { return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE); } unsigned long lmb_alloc_base(unsigned long size, unsigned long align, unsigned long max_addr) { long i, j; unsigned long base = 0; unsigned long offset = reloc_offset(); struct lmb *_lmb = PTRRELOC(&lmb); struct lmb_region *_mem = &(_lmb->memory); struct lmb_region *_rsv = &(_lmb->reserved); for (i=_mem->cnt-1; i >= 0 ;i--) { unsigned long lmbbase = _mem->region[i].base; unsigned long lmbsize = _mem->region[i].size; unsigned long lmbtype = _mem->region[i].type; if ( lmbtype != LMB_MEMORY_AREA ) continue; if ( max_addr == LMB_ALLOC_ANYWHERE ) base = _ALIGN_DOWN(lmbbase+lmbsize-size, align); else if ( lmbbase < max_addr ) base = _ALIGN_DOWN(min(lmbbase+lmbsize,max_addr)-size, align); else continue; while ( (lmbbase <= base) && ((j = lmb_overlaps_region(_rsv,base,size)) >= 0) ) { base = _ALIGN_DOWN(_rsv->region[j].base-size, align); } if ( (base != 0) && (lmbbase <= base) ) break; } if ( i < 0 ) return 0; lmb_add_region(_rsv, base, size, LMB_MEMORY_AREA); return base; } unsigned long lmb_phys_mem_size(void) { unsigned long offset = reloc_offset(); struct lmb *_lmb = PTRRELOC(&lmb); #ifdef CONFIG_MSCHUNKS return _lmb->memory.size; #else struct lmb_region *_mem = &(_lmb->memory); unsigned long idx = _mem->cnt-1; unsigned long lastbase = _mem->region[idx].physbase; unsigned long lastsize = _mem->region[idx].size; return (lastbase + lastsize); #endif /* CONFIG_MSCHUNKS */ } unsigned long lmb_end_of_DRAM(void) { unsigned long offset = reloc_offset(); struct lmb *_lmb = PTRRELOC(&lmb); struct lmb_region *_mem = &(_lmb->memory); unsigned long idx; for(idx=_mem->cnt-1; idx >= 0 ;idx--) { if ( _mem->region[idx].type != LMB_MEMORY_AREA ) continue; #ifdef CONFIG_MSCHUNKS return (_mem->region[idx].physbase + _mem->region[idx].size); #else return (_mem->region[idx].base + _mem->region[idx].size); #endif /* CONFIG_MSCHUNKS */ } return 0; } unsigned long lmb_abs_to_phys(unsigned long aa) { unsigned long i, pa = aa; unsigned long offset = reloc_offset(); struct lmb *_lmb = PTRRELOC(&lmb); struct lmb_region *_mem = &(_lmb->memory); for (i=0; i < _mem->cnt ;i++) { unsigned long lmbbase = _mem->region[i].base; unsigned long lmbsize = _mem->region[i].size; if ( lmb_addrs_overlap(aa,1,lmbbase,lmbsize) ) { pa = _mem->region[i].physbase + (aa - lmbbase); break; } } return pa; } void lmb_dump(char *str) { unsigned long i; udbg_printf("\nlmb_dump: %s\n", str); udbg_printf(" debug = %s\n", (lmb.debug) ? "TRUE" : "FALSE"); udbg_printf(" memory.cnt = %d\n", lmb.memory.cnt); udbg_printf(" memory.size = 0x%lx\n", lmb.memory.size); udbg_printf(" memory.lcd_size = 0x%lx\n", lmb.memory.lcd_size); for (i=0; i < lmb.memory.cnt ;i++) { udbg_printf(" memory.region[%d].base = 0x%lx\n", i, lmb.memory.region[i].base); udbg_printf(" .physbase = 0x%lx\n", lmb.memory.region[i].physbase); udbg_printf(" .size = 0x%lx\n", lmb.memory.region[i].size); udbg_printf(" .type = 0x%lx\n", lmb.memory.region[i].type); } udbg_printf("\n"); udbg_printf(" reserved.cnt = %d\n", lmb.reserved.cnt); udbg_printf(" reserved.size = 0x%lx\n", lmb.reserved.size); udbg_printf(" reserved.lcd_size = 0x%lx\n", lmb.reserved.lcd_size); for (i=0; i < lmb.reserved.cnt ;i++) { udbg_printf(" reserved.region[%d].base = 0x%lx\n", i, lmb.reserved.region[i].base); udbg_printf(" .physbase = 0x%lx\n", lmb.reserved.region[i].physbase); udbg_printf(" .size = 0x%lx\n", lmb.reserved.region[i].size); udbg_printf(" .type = 0x%lx\n", lmb.reserved.region[i].type); } } |