mem_detect.c 1.7 KB

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