mem_detect.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright IBM Corp. 2008, 2009
  3. *
  4. * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <asm/ipl.h>
  9. #include <asm/sclp.h>
  10. #include <asm/setup.h>
  11. #define ADDR2G (1ULL << 31)
  12. static void find_memory_chunks(struct mem_chunk chunk[])
  13. {
  14. unsigned long long memsize, rnmax, rzm;
  15. unsigned long addr = 0, size;
  16. int i = 0, type;
  17. rzm = sclp_get_rzm();
  18. rnmax = sclp_get_rnmax();
  19. memsize = rzm * rnmax;
  20. if (!rzm)
  21. rzm = 1ULL << 17;
  22. if (sizeof(long) == 4) {
  23. rzm = min(ADDR2G, rzm);
  24. memsize = memsize ? min(ADDR2G, memsize) : ADDR2G;
  25. }
  26. do {
  27. size = 0;
  28. type = tprot(addr);
  29. do {
  30. size += rzm;
  31. if (memsize && addr + size >= memsize)
  32. break;
  33. } while (type == tprot(addr + size));
  34. if (type == CHUNK_READ_WRITE || type == CHUNK_READ_ONLY) {
  35. chunk[i].addr = addr;
  36. chunk[i].size = size;
  37. chunk[i].type = type;
  38. i++;
  39. }
  40. addr += size;
  41. } while (addr < memsize && i < MEMORY_CHUNKS);
  42. }
  43. void detect_memory_layout(struct mem_chunk chunk[])
  44. {
  45. unsigned long flags, cr0;
  46. memset(chunk, 0, MEMORY_CHUNKS * sizeof(struct mem_chunk));
  47. /* Disable IRQs, DAT and low address protection so tprot does the
  48. * right thing and we don't get scheduled away with low address
  49. * protection disabled.
  50. */
  51. flags = __arch_local_irq_stnsm(0xf8);
  52. __ctl_store(cr0, 0, 0);
  53. __ctl_clear_bit(0, 28);
  54. find_memory_chunks(chunk);
  55. __ctl_load(cr0, 0, 0);
  56. arch_local_irq_restore(flags);
  57. }
  58. EXPORT_SYMBOL(detect_memory_layout);