mem_detect.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 int memory_fast_detect(struct mem_chunk *chunk)
  11. {
  12. unsigned long val0 = 0;
  13. unsigned long val1 = 0xc;
  14. int rc = -EOPNOTSUPP;
  15. if (ipl_flags & IPL_NSS_VALID)
  16. return -EOPNOTSUPP;
  17. asm volatile(
  18. " diag %1,%2,0x260\n"
  19. "0: lhi %0,0\n"
  20. "1:\n"
  21. EX_TABLE(0b,1b)
  22. : "+d" (rc), "+d" (val0), "+d" (val1) : : "cc");
  23. if (rc || val0 != val1)
  24. return -EOPNOTSUPP;
  25. chunk->size = val0 + 1;
  26. return 0;
  27. }
  28. static inline int tprot(unsigned long addr)
  29. {
  30. int rc = -EFAULT;
  31. asm volatile(
  32. " tprot 0(%1),0\n"
  33. "0: ipm %0\n"
  34. " srl %0,28\n"
  35. "1:\n"
  36. EX_TABLE(0b,1b)
  37. : "+d" (rc) : "a" (addr) : "cc");
  38. return rc;
  39. }
  40. #define ADDR2G (1ULL << 31)
  41. static void find_memory_chunks(struct mem_chunk chunk[])
  42. {
  43. unsigned long long memsize, rnmax, rzm;
  44. unsigned long addr = 0, size;
  45. int i = 0, type;
  46. rzm = sclp_get_rzm();
  47. rnmax = sclp_get_rnmax();
  48. memsize = rzm * rnmax;
  49. if (!rzm)
  50. rzm = 1ULL << 17;
  51. if (sizeof(long) == 4) {
  52. rzm = min(ADDR2G, rzm);
  53. memsize = memsize ? min(ADDR2G, memsize) : ADDR2G;
  54. }
  55. do {
  56. size = 0;
  57. type = tprot(addr);
  58. do {
  59. size += rzm;
  60. if (memsize && addr + size >= memsize)
  61. break;
  62. } while (type == tprot(addr + size));
  63. if (type == CHUNK_READ_WRITE || type == CHUNK_READ_ONLY) {
  64. chunk[i].addr = addr;
  65. chunk[i].size = size;
  66. chunk[i].type = type;
  67. i++;
  68. }
  69. addr += size;
  70. } while (addr < memsize && i < MEMORY_CHUNKS);
  71. }
  72. void detect_memory_layout(struct mem_chunk chunk[])
  73. {
  74. unsigned long flags, cr0;
  75. memset(chunk, 0, MEMORY_CHUNKS * sizeof(struct mem_chunk));
  76. if (memory_fast_detect(&chunk[0]) == 0)
  77. return;
  78. /* Disable IRQs, DAT and low address protection so tprot does the
  79. * right thing and we don't get scheduled away with low address
  80. * protection disabled.
  81. */
  82. flags = __raw_local_irq_stnsm(0xf8);
  83. __ctl_store(cr0, 0, 0);
  84. __ctl_clear_bit(0, 28);
  85. find_memory_chunks(chunk);
  86. __ctl_load(cr0, 0, 0);
  87. __raw_local_irq_ssm(flags);
  88. }
  89. EXPORT_SYMBOL(detect_memory_layout);