e820_64.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /*
  2. * Handle the memory map.
  3. * The functions here do the job until bootmem takes over.
  4. *
  5. * Getting sanitize_e820_map() in sync with i386 version by applying change:
  6. * - Provisions for empty E820 memory regions (reported by certain BIOSes).
  7. * Alex Achenbach <xela@slit.de>, December 2002.
  8. * Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/types.h>
  13. #include <linux/init.h>
  14. #include <linux/bootmem.h>
  15. #include <linux/ioport.h>
  16. #include <linux/string.h>
  17. #include <linux/kexec.h>
  18. #include <linux/module.h>
  19. #include <linux/mm.h>
  20. #include <linux/suspend.h>
  21. #include <linux/pfn.h>
  22. #include <asm/pgtable.h>
  23. #include <asm/page.h>
  24. #include <asm/e820.h>
  25. #include <asm/proto.h>
  26. #include <asm/setup.h>
  27. #include <asm/sections.h>
  28. struct e820map e820;
  29. /*
  30. * PFN of last memory page.
  31. */
  32. unsigned long end_pfn;
  33. EXPORT_SYMBOL(end_pfn);
  34. /*
  35. * end_pfn only includes RAM, while end_pfn_map includes all e820 entries.
  36. * The direct mapping extends to end_pfn_map, so that we can directly access
  37. * apertures, ACPI and other tables without having to play with fixmaps.
  38. */
  39. unsigned long end_pfn_map;
  40. /*
  41. * Last pfn which the user wants to use.
  42. */
  43. static unsigned long __initdata end_user_pfn = MAXMEM>>PAGE_SHIFT;
  44. extern struct resource code_resource, data_resource, bss_resource;
  45. /* Check for some hardcoded bad areas that early boot is not allowed to touch */
  46. static inline int bad_addr(unsigned long *addrp, unsigned long size)
  47. {
  48. unsigned long addr = *addrp, last = addr + size;
  49. /* various gunk below that needed for SMP startup */
  50. if (addr < 0x8000) {
  51. *addrp = PAGE_ALIGN(0x8000);
  52. return 1;
  53. }
  54. /* direct mapping tables of the kernel */
  55. if (last >= table_start<<PAGE_SHIFT && addr < table_end<<PAGE_SHIFT) {
  56. *addrp = PAGE_ALIGN(table_end << PAGE_SHIFT);
  57. return 1;
  58. }
  59. /* initrd */
  60. #ifdef CONFIG_BLK_DEV_INITRD
  61. if (boot_params.hdr.type_of_loader && boot_params.hdr.ramdisk_image) {
  62. unsigned long ramdisk_image = boot_params.hdr.ramdisk_image;
  63. unsigned long ramdisk_size = boot_params.hdr.ramdisk_size;
  64. unsigned long ramdisk_end = ramdisk_image+ramdisk_size;
  65. if (last >= ramdisk_image && addr < ramdisk_end) {
  66. *addrp = PAGE_ALIGN(ramdisk_end);
  67. return 1;
  68. }
  69. }
  70. #endif
  71. /* kernel code */
  72. if (last >= __pa_symbol(&_text) && addr < __pa_symbol(&_end)) {
  73. *addrp = PAGE_ALIGN(__pa_symbol(&_end));
  74. return 1;
  75. }
  76. if (last >= ebda_addr && addr < ebda_addr + ebda_size) {
  77. *addrp = PAGE_ALIGN(ebda_addr + ebda_size);
  78. return 1;
  79. }
  80. #ifdef CONFIG_NUMA
  81. /* NUMA memory to node map */
  82. if (last >= nodemap_addr && addr < nodemap_addr + nodemap_size) {
  83. *addrp = nodemap_addr + nodemap_size;
  84. return 1;
  85. }
  86. #endif
  87. /* XXX ramdisk image here? */
  88. return 0;
  89. }
  90. /*
  91. * This function checks if any part of the range <start,end> is mapped
  92. * with type.
  93. */
  94. int
  95. e820_any_mapped(unsigned long start, unsigned long end, unsigned type)
  96. {
  97. int i;
  98. for (i = 0; i < e820.nr_map; i++) {
  99. struct e820entry *ei = &e820.map[i];
  100. if (type && ei->type != type)
  101. continue;
  102. if (ei->addr >= end || ei->addr + ei->size <= start)
  103. continue;
  104. return 1;
  105. }
  106. return 0;
  107. }
  108. EXPORT_SYMBOL_GPL(e820_any_mapped);
  109. /*
  110. * This function checks if the entire range <start,end> is mapped with type.
  111. *
  112. * Note: this function only works correct if the e820 table is sorted and
  113. * not-overlapping, which is the case
  114. */
  115. int __init e820_all_mapped(unsigned long start, unsigned long end, unsigned type)
  116. {
  117. int i;
  118. for (i = 0; i < e820.nr_map; i++) {
  119. struct e820entry *ei = &e820.map[i];
  120. if (type && ei->type != type)
  121. continue;
  122. /* is the region (part) in overlap with the current region ?*/
  123. if (ei->addr >= end || ei->addr + ei->size <= start)
  124. continue;
  125. /* if the region is at the beginning of <start,end> we move
  126. * start to the end of the region since it's ok until there
  127. */
  128. if (ei->addr <= start)
  129. start = ei->addr + ei->size;
  130. /* if start is now at or beyond end, we're done, full coverage */
  131. if (start >= end)
  132. return 1; /* we're done */
  133. }
  134. return 0;
  135. }
  136. /*
  137. * Find a free area in a specific range.
  138. */
  139. unsigned long __init find_e820_area(unsigned long start, unsigned long end, unsigned size)
  140. {
  141. int i;
  142. for (i = 0; i < e820.nr_map; i++) {
  143. struct e820entry *ei = &e820.map[i];
  144. unsigned long addr = ei->addr, last;
  145. if (ei->type != E820_RAM)
  146. continue;
  147. if (addr < start)
  148. addr = start;
  149. if (addr > ei->addr + ei->size)
  150. continue;
  151. while (bad_addr(&addr, size) && addr+size <= ei->addr+ei->size)
  152. ;
  153. last = PAGE_ALIGN(addr) + size;
  154. if (last > ei->addr + ei->size)
  155. continue;
  156. if (last > end)
  157. continue;
  158. return addr;
  159. }
  160. return -1UL;
  161. }
  162. /*
  163. * Find the highest page frame number we have available
  164. */
  165. unsigned long __init e820_end_of_ram(void)
  166. {
  167. unsigned long end_pfn = 0;
  168. end_pfn = find_max_pfn_with_active_regions();
  169. if (end_pfn > end_pfn_map)
  170. end_pfn_map = end_pfn;
  171. if (end_pfn_map > MAXMEM>>PAGE_SHIFT)
  172. end_pfn_map = MAXMEM>>PAGE_SHIFT;
  173. if (end_pfn > end_user_pfn)
  174. end_pfn = end_user_pfn;
  175. if (end_pfn > end_pfn_map)
  176. end_pfn = end_pfn_map;
  177. printk("end_pfn_map = %lu\n", end_pfn_map);
  178. return end_pfn;
  179. }
  180. /*
  181. * Mark e820 reserved areas as busy for the resource manager.
  182. */
  183. void __init e820_reserve_resources(void)
  184. {
  185. int i;
  186. for (i = 0; i < e820.nr_map; i++) {
  187. struct resource *res;
  188. res = alloc_bootmem_low(sizeof(struct resource));
  189. switch (e820.map[i].type) {
  190. case E820_RAM: res->name = "System RAM"; break;
  191. case E820_ACPI: res->name = "ACPI Tables"; break;
  192. case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
  193. default: res->name = "reserved";
  194. }
  195. res->start = e820.map[i].addr;
  196. res->end = res->start + e820.map[i].size - 1;
  197. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  198. request_resource(&iomem_resource, res);
  199. if (e820.map[i].type == E820_RAM) {
  200. /*
  201. * We don't know which RAM region contains kernel data,
  202. * so we try it repeatedly and let the resource manager
  203. * test it.
  204. */
  205. request_resource(res, &code_resource);
  206. request_resource(res, &data_resource);
  207. request_resource(res, &bss_resource);
  208. #ifdef CONFIG_KEXEC
  209. if (crashk_res.start != crashk_res.end)
  210. request_resource(res, &crashk_res);
  211. #endif
  212. }
  213. }
  214. }
  215. /*
  216. * Find the ranges of physical addresses that do not correspond to
  217. * e820 RAM areas and mark the corresponding pages as nosave for software
  218. * suspend and suspend to RAM.
  219. *
  220. * This function requires the e820 map to be sorted and without any
  221. * overlapping entries and assumes the first e820 area to be RAM.
  222. */
  223. void __init e820_mark_nosave_regions(void)
  224. {
  225. int i;
  226. unsigned long paddr;
  227. paddr = round_down(e820.map[0].addr + e820.map[0].size, PAGE_SIZE);
  228. for (i = 1; i < e820.nr_map; i++) {
  229. struct e820entry *ei = &e820.map[i];
  230. if (paddr < ei->addr)
  231. register_nosave_region(PFN_DOWN(paddr),
  232. PFN_UP(ei->addr));
  233. paddr = round_down(ei->addr + ei->size, PAGE_SIZE);
  234. if (ei->type != E820_RAM)
  235. register_nosave_region(PFN_UP(ei->addr),
  236. PFN_DOWN(paddr));
  237. if (paddr >= (end_pfn << PAGE_SHIFT))
  238. break;
  239. }
  240. }
  241. /*
  242. * Finds an active region in the address range from start_pfn to end_pfn and
  243. * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
  244. */
  245. static int __init e820_find_active_region(const struct e820entry *ei,
  246. unsigned long start_pfn,
  247. unsigned long end_pfn,
  248. unsigned long *ei_startpfn,
  249. unsigned long *ei_endpfn)
  250. {
  251. *ei_startpfn = round_up(ei->addr, PAGE_SIZE) >> PAGE_SHIFT;
  252. *ei_endpfn = round_down(ei->addr + ei->size, PAGE_SIZE) >> PAGE_SHIFT;
  253. /* Skip map entries smaller than a page */
  254. if (*ei_startpfn >= *ei_endpfn)
  255. return 0;
  256. /* Check if end_pfn_map should be updated */
  257. if (ei->type != E820_RAM && *ei_endpfn > end_pfn_map)
  258. end_pfn_map = *ei_endpfn;
  259. /* Skip if map is outside the node */
  260. if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
  261. *ei_startpfn >= end_pfn)
  262. return 0;
  263. /* Check for overlaps */
  264. if (*ei_startpfn < start_pfn)
  265. *ei_startpfn = start_pfn;
  266. if (*ei_endpfn > end_pfn)
  267. *ei_endpfn = end_pfn;
  268. /* Obey end_user_pfn to save on memmap */
  269. if (*ei_startpfn >= end_user_pfn)
  270. return 0;
  271. if (*ei_endpfn > end_user_pfn)
  272. *ei_endpfn = end_user_pfn;
  273. return 1;
  274. }
  275. /* Walk the e820 map and register active regions within a node */
  276. void __init
  277. e820_register_active_regions(int nid, unsigned long start_pfn,
  278. unsigned long end_pfn)
  279. {
  280. unsigned long ei_startpfn;
  281. unsigned long ei_endpfn;
  282. int i;
  283. for (i = 0; i < e820.nr_map; i++)
  284. if (e820_find_active_region(&e820.map[i],
  285. start_pfn, end_pfn,
  286. &ei_startpfn, &ei_endpfn))
  287. add_active_range(nid, ei_startpfn, ei_endpfn);
  288. }
  289. /*
  290. * Add a memory region to the kernel e820 map.
  291. */
  292. void __init add_memory_region(unsigned long start, unsigned long size, int type)
  293. {
  294. int x = e820.nr_map;
  295. if (x == E820MAX) {
  296. printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
  297. return;
  298. }
  299. e820.map[x].addr = start;
  300. e820.map[x].size = size;
  301. e820.map[x].type = type;
  302. e820.nr_map++;
  303. }
  304. /*
  305. * Find the hole size (in bytes) in the memory range.
  306. * @start: starting address of the memory range to scan
  307. * @end: ending address of the memory range to scan
  308. */
  309. unsigned long __init e820_hole_size(unsigned long start, unsigned long end)
  310. {
  311. unsigned long start_pfn = start >> PAGE_SHIFT;
  312. unsigned long end_pfn = end >> PAGE_SHIFT;
  313. unsigned long ei_startpfn;
  314. unsigned long ei_endpfn;
  315. unsigned long ram = 0;
  316. int i;
  317. for (i = 0; i < e820.nr_map; i++) {
  318. if (e820_find_active_region(&e820.map[i],
  319. start_pfn, end_pfn,
  320. &ei_startpfn, &ei_endpfn))
  321. ram += ei_endpfn - ei_startpfn;
  322. }
  323. return end - start - (ram << PAGE_SHIFT);
  324. }
  325. void __init e820_print_map(char *who)
  326. {
  327. int i;
  328. for (i = 0; i < e820.nr_map; i++) {
  329. printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
  330. (unsigned long long) e820.map[i].addr,
  331. (unsigned long long) (e820.map[i].addr + e820.map[i].size));
  332. switch (e820.map[i].type) {
  333. case E820_RAM: printk("(usable)\n");
  334. break;
  335. case E820_RESERVED:
  336. printk("(reserved)\n");
  337. break;
  338. case E820_ACPI:
  339. printk("(ACPI data)\n");
  340. break;
  341. case E820_NVS:
  342. printk("(ACPI NVS)\n");
  343. break;
  344. default: printk("type %u\n", e820.map[i].type);
  345. break;
  346. }
  347. }
  348. }
  349. /*
  350. * Sanitize the BIOS e820 map.
  351. *
  352. * Some e820 responses include overlapping entries. The following
  353. * replaces the original e820 map with a new one, removing overlaps.
  354. *
  355. */
  356. static int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
  357. {
  358. struct change_member {
  359. struct e820entry *pbios; /* pointer to original bios entry */
  360. unsigned long long addr; /* address for this change point */
  361. };
  362. static struct change_member change_point_list[2*E820MAX] __initdata;
  363. static struct change_member *change_point[2*E820MAX] __initdata;
  364. static struct e820entry *overlap_list[E820MAX] __initdata;
  365. static struct e820entry new_bios[E820MAX] __initdata;
  366. struct change_member *change_tmp;
  367. unsigned long current_type, last_type;
  368. unsigned long long last_addr;
  369. int chgidx, still_changing;
  370. int overlap_entries;
  371. int new_bios_entry;
  372. int old_nr, new_nr, chg_nr;
  373. int i;
  374. /*
  375. Visually we're performing the following (1,2,3,4 = memory types)...
  376. Sample memory map (w/overlaps):
  377. ____22__________________
  378. ______________________4_
  379. ____1111________________
  380. _44_____________________
  381. 11111111________________
  382. ____________________33__
  383. ___________44___________
  384. __________33333_________
  385. ______________22________
  386. ___________________2222_
  387. _________111111111______
  388. _____________________11_
  389. _________________4______
  390. Sanitized equivalent (no overlap):
  391. 1_______________________
  392. _44_____________________
  393. ___1____________________
  394. ____22__________________
  395. ______11________________
  396. _________1______________
  397. __________3_____________
  398. ___________44___________
  399. _____________33_________
  400. _______________2________
  401. ________________1_______
  402. _________________4______
  403. ___________________2____
  404. ____________________33__
  405. ______________________4_
  406. */
  407. /* if there's only one memory region, don't bother */
  408. if (*pnr_map < 2)
  409. return -1;
  410. old_nr = *pnr_map;
  411. /* bail out if we find any unreasonable addresses in bios map */
  412. for (i=0; i<old_nr; i++)
  413. if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
  414. return -1;
  415. /* create pointers for initial change-point information (for sorting) */
  416. for (i=0; i < 2*old_nr; i++)
  417. change_point[i] = &change_point_list[i];
  418. /* record all known change-points (starting and ending addresses),
  419. omitting those that are for empty memory regions */
  420. chgidx = 0;
  421. for (i=0; i < old_nr; i++) {
  422. if (biosmap[i].size != 0) {
  423. change_point[chgidx]->addr = biosmap[i].addr;
  424. change_point[chgidx++]->pbios = &biosmap[i];
  425. change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
  426. change_point[chgidx++]->pbios = &biosmap[i];
  427. }
  428. }
  429. chg_nr = chgidx;
  430. /* sort change-point list by memory addresses (low -> high) */
  431. still_changing = 1;
  432. while (still_changing) {
  433. still_changing = 0;
  434. for (i=1; i < chg_nr; i++) {
  435. /* if <current_addr> > <last_addr>, swap */
  436. /* or, if current=<start_addr> & last=<end_addr>, swap */
  437. if ((change_point[i]->addr < change_point[i-1]->addr) ||
  438. ((change_point[i]->addr == change_point[i-1]->addr) &&
  439. (change_point[i]->addr == change_point[i]->pbios->addr) &&
  440. (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
  441. )
  442. {
  443. change_tmp = change_point[i];
  444. change_point[i] = change_point[i-1];
  445. change_point[i-1] = change_tmp;
  446. still_changing=1;
  447. }
  448. }
  449. }
  450. /* create a new bios memory map, removing overlaps */
  451. overlap_entries=0; /* number of entries in the overlap table */
  452. new_bios_entry=0; /* index for creating new bios map entries */
  453. last_type = 0; /* start with undefined memory type */
  454. last_addr = 0; /* start with 0 as last starting address */
  455. /* loop through change-points, determining affect on the new bios map */
  456. for (chgidx=0; chgidx < chg_nr; chgidx++)
  457. {
  458. /* keep track of all overlapping bios entries */
  459. if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
  460. {
  461. /* add map entry to overlap list (> 1 entry implies an overlap) */
  462. overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
  463. }
  464. else
  465. {
  466. /* remove entry from list (order independent, so swap with last) */
  467. for (i=0; i<overlap_entries; i++)
  468. {
  469. if (overlap_list[i] == change_point[chgidx]->pbios)
  470. overlap_list[i] = overlap_list[overlap_entries-1];
  471. }
  472. overlap_entries--;
  473. }
  474. /* if there are overlapping entries, decide which "type" to use */
  475. /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
  476. current_type = 0;
  477. for (i=0; i<overlap_entries; i++)
  478. if (overlap_list[i]->type > current_type)
  479. current_type = overlap_list[i]->type;
  480. /* continue building up new bios map based on this information */
  481. if (current_type != last_type) {
  482. if (last_type != 0) {
  483. new_bios[new_bios_entry].size =
  484. change_point[chgidx]->addr - last_addr;
  485. /* move forward only if the new size was non-zero */
  486. if (new_bios[new_bios_entry].size != 0)
  487. if (++new_bios_entry >= E820MAX)
  488. break; /* no more space left for new bios entries */
  489. }
  490. if (current_type != 0) {
  491. new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
  492. new_bios[new_bios_entry].type = current_type;
  493. last_addr=change_point[chgidx]->addr;
  494. }
  495. last_type = current_type;
  496. }
  497. }
  498. new_nr = new_bios_entry; /* retain count for new bios entries */
  499. /* copy new bios mapping into original location */
  500. memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
  501. *pnr_map = new_nr;
  502. return 0;
  503. }
  504. /*
  505. * Copy the BIOS e820 map into a safe place.
  506. *
  507. * Sanity-check it while we're at it..
  508. *
  509. * If we're lucky and live on a modern system, the setup code
  510. * will have given us a memory map that we can use to properly
  511. * set up memory. If we aren't, we'll fake a memory map.
  512. */
  513. static int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
  514. {
  515. /* Only one memory region (or negative)? Ignore it */
  516. if (nr_map < 2)
  517. return -1;
  518. do {
  519. unsigned long start = biosmap->addr;
  520. unsigned long size = biosmap->size;
  521. unsigned long end = start + size;
  522. unsigned long type = biosmap->type;
  523. /* Overflow in 64 bits? Ignore the memory map. */
  524. if (start > end)
  525. return -1;
  526. add_memory_region(start, size, type);
  527. } while (biosmap++,--nr_map);
  528. return 0;
  529. }
  530. void early_panic(char *msg)
  531. {
  532. early_printk(msg);
  533. panic(msg);
  534. }
  535. void __init setup_memory_region(void)
  536. {
  537. /*
  538. * Try to copy the BIOS-supplied E820-map.
  539. *
  540. * Otherwise fake a memory map; one section from 0k->640k,
  541. * the next section from 1mb->appropriate_mem_k
  542. */
  543. sanitize_e820_map(boot_params.e820_map, &boot_params.e820_entries);
  544. if (copy_e820_map(boot_params.e820_map, boot_params.e820_entries) < 0)
  545. early_panic("Cannot find a valid memory map");
  546. printk(KERN_INFO "BIOS-provided physical RAM map:\n");
  547. e820_print_map("BIOS-e820");
  548. }
  549. static int __init parse_memopt(char *p)
  550. {
  551. if (!p)
  552. return -EINVAL;
  553. end_user_pfn = memparse(p, &p);
  554. end_user_pfn >>= PAGE_SHIFT;
  555. return 0;
  556. }
  557. early_param("mem", parse_memopt);
  558. static int userdef __initdata;
  559. static int __init parse_memmap_opt(char *p)
  560. {
  561. char *oldp;
  562. unsigned long long start_at, mem_size;
  563. if (!strcmp(p, "exactmap")) {
  564. #ifdef CONFIG_CRASH_DUMP
  565. /* If we are doing a crash dump, we
  566. * still need to know the real mem
  567. * size before original memory map is
  568. * reset.
  569. */
  570. e820_register_active_regions(0, 0, -1UL);
  571. saved_max_pfn = e820_end_of_ram();
  572. remove_all_active_ranges();
  573. #endif
  574. end_pfn_map = 0;
  575. e820.nr_map = 0;
  576. userdef = 1;
  577. return 0;
  578. }
  579. oldp = p;
  580. mem_size = memparse(p, &p);
  581. if (p == oldp)
  582. return -EINVAL;
  583. if (*p == '@') {
  584. start_at = memparse(p+1, &p);
  585. add_memory_region(start_at, mem_size, E820_RAM);
  586. } else if (*p == '#') {
  587. start_at = memparse(p+1, &p);
  588. add_memory_region(start_at, mem_size, E820_ACPI);
  589. } else if (*p == '$') {
  590. start_at = memparse(p+1, &p);
  591. add_memory_region(start_at, mem_size, E820_RESERVED);
  592. } else {
  593. end_user_pfn = (mem_size >> PAGE_SHIFT);
  594. }
  595. return *p == '\0' ? 0 : -EINVAL;
  596. }
  597. early_param("memmap", parse_memmap_opt);
  598. void __init finish_e820_parsing(void)
  599. {
  600. if (userdef) {
  601. printk(KERN_INFO "user-defined physical RAM map:\n");
  602. e820_print_map("user");
  603. }
  604. }
  605. unsigned long pci_mem_start = 0xaeedbabe;
  606. EXPORT_SYMBOL(pci_mem_start);
  607. /*
  608. * Search for the biggest gap in the low 32 bits of the e820
  609. * memory space. We pass this space to PCI to assign MMIO resources
  610. * for hotplug or unconfigured devices in.
  611. * Hopefully the BIOS let enough space left.
  612. */
  613. __init void e820_setup_gap(void)
  614. {
  615. unsigned long gapstart, gapsize, round;
  616. unsigned long last;
  617. int i;
  618. int found = 0;
  619. last = 0x100000000ull;
  620. gapstart = 0x10000000;
  621. gapsize = 0x400000;
  622. i = e820.nr_map;
  623. while (--i >= 0) {
  624. unsigned long long start = e820.map[i].addr;
  625. unsigned long long end = start + e820.map[i].size;
  626. /*
  627. * Since "last" is at most 4GB, we know we'll
  628. * fit in 32 bits if this condition is true
  629. */
  630. if (last > end) {
  631. unsigned long gap = last - end;
  632. if (gap > gapsize) {
  633. gapsize = gap;
  634. gapstart = end;
  635. found = 1;
  636. }
  637. }
  638. if (start < last)
  639. last = start;
  640. }
  641. if (!found) {
  642. gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
  643. printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit address range\n"
  644. KERN_ERR "PCI: Unassigned devices with 32bit resource registers may break!\n");
  645. }
  646. /*
  647. * See how much we want to round up: start off with
  648. * rounding to the next 1MB area.
  649. */
  650. round = 0x100000;
  651. while ((gapsize >> 4) > round)
  652. round += round;
  653. /* Fun with two's complement */
  654. pci_mem_start = (gapstart + round) & -round;
  655. printk(KERN_INFO "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
  656. pci_mem_start, gapstart, gapsize);
  657. }
  658. int __init arch_get_ram_range(int slot, u64 *addr, u64 *size)
  659. {
  660. int i;
  661. if (slot < 0 || slot >= e820.nr_map)
  662. return -1;
  663. for (i = slot; i < e820.nr_map; i++) {
  664. if (e820.map[i].type != E820_RAM)
  665. continue;
  666. break;
  667. }
  668. if (i == e820.nr_map || e820.map[i].addr > (max_pfn << PAGE_SHIFT))
  669. return -1;
  670. *addr = e820.map[i].addr;
  671. *size = min_t(u64, e820.map[i].size + e820.map[i].addr,
  672. max_pfn << PAGE_SHIFT) - *addr;
  673. return i + 1;
  674. }