htab.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PS3 pagetable management routines.
  4. *
  5. * Copyright (C) 2006 Sony Computer Entertainment Inc.
  6. * Copyright 2006, 2007 Sony Corporation
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/memblock.h>
  10. #include <asm/machdep.h>
  11. #include <asm/udbg.h>
  12. #include <asm/lv1call.h>
  13. #include <asm/ps3fb.h>
  14. #define PS3_VERBOSE_RESULT
  15. #include "platform.h"
  16. /**
  17. * enum lpar_vas_id - id of LPAR virtual address space.
  18. * @lpar_vas_id_current: Current selected virtual address space
  19. *
  20. * Identify the target LPAR address space.
  21. */
  22. enum ps3_lpar_vas_id {
  23. PS3_LPAR_VAS_ID_CURRENT = 0,
  24. };
  25. static DEFINE_SPINLOCK(ps3_htab_lock);
  26. static long ps3_hpte_insert(unsigned long hpte_group, unsigned long vpn,
  27. unsigned long pa, unsigned long rflags, unsigned long vflags,
  28. int psize, int apsize, int ssize)
  29. {
  30. int result;
  31. u64 hpte_v, hpte_r;
  32. u64 inserted_index;
  33. u64 evicted_v, evicted_r;
  34. u64 hpte_v_array[4], hpte_rs;
  35. unsigned long flags;
  36. long ret = -1;
  37. /*
  38. * lv1_insert_htab_entry() will search for victim
  39. * entry in both primary and secondary pte group
  40. */
  41. vflags &= ~HPTE_V_SECONDARY;
  42. hpte_v = hpte_encode_v(vpn, psize, apsize, ssize) | vflags | HPTE_V_VALID;
  43. hpte_r = hpte_encode_r(ps3_mm_phys_to_lpar(pa), psize, apsize) | rflags;
  44. spin_lock_irqsave(&ps3_htab_lock, flags);
  45. /* talk hvc to replace entries BOLTED == 0 */
  46. result = lv1_insert_htab_entry(PS3_LPAR_VAS_ID_CURRENT, hpte_group,
  47. hpte_v, hpte_r,
  48. HPTE_V_BOLTED, 0,
  49. &inserted_index,
  50. &evicted_v, &evicted_r);
  51. if (result) {
  52. /* all entries bolted !*/
  53. pr_info("%s:result=%s vpn=%lx pa=%lx ix=%lx v=%llx r=%llx\n",
  54. __func__, ps3_result(result), vpn, pa, hpte_group,
  55. hpte_v, hpte_r);
  56. BUG();
  57. }
  58. /*
  59. * see if the entry is inserted into secondary pteg
  60. */
  61. result = lv1_read_htab_entries(PS3_LPAR_VAS_ID_CURRENT,
  62. inserted_index & ~0x3UL,
  63. &hpte_v_array[0], &hpte_v_array[1],
  64. &hpte_v_array[2], &hpte_v_array[3],
  65. &hpte_rs);
  66. BUG_ON(result);
  67. if (hpte_v_array[inserted_index % 4] & HPTE_V_SECONDARY)
  68. ret = (inserted_index & 7) | (1 << 3);
  69. else
  70. ret = inserted_index & 7;
  71. spin_unlock_irqrestore(&ps3_htab_lock, flags);
  72. return ret;
  73. }
  74. static long ps3_hpte_remove(unsigned long hpte_group)
  75. {
  76. panic("ps3_hpte_remove() not implemented");
  77. return 0;
  78. }
  79. static long ps3_hpte_updatepp(unsigned long slot, unsigned long newpp,
  80. unsigned long vpn, int psize, int apsize,
  81. int ssize, unsigned long inv_flags)
  82. {
  83. int result;
  84. u64 hpte_v, want_v, hpte_rs;
  85. u64 hpte_v_array[4];
  86. unsigned long flags;
  87. long ret;
  88. want_v = hpte_encode_avpn(vpn, psize, ssize);
  89. spin_lock_irqsave(&ps3_htab_lock, flags);
  90. result = lv1_read_htab_entries(PS3_LPAR_VAS_ID_CURRENT, slot & ~0x3UL,
  91. &hpte_v_array[0], &hpte_v_array[1],
  92. &hpte_v_array[2], &hpte_v_array[3],
  93. &hpte_rs);
  94. if (result) {
  95. pr_info("%s: result=%s read vpn=%lx slot=%lx psize=%d\n",
  96. __func__, ps3_result(result), vpn, slot, psize);
  97. BUG();
  98. }
  99. hpte_v = hpte_v_array[slot % 4];
  100. /*
  101. * As lv1_read_htab_entries() does not give us the RPN, we can
  102. * not synthesize the new hpte_r value here, and therefore can
  103. * not update the hpte with lv1_insert_htab_entry(), so we
  104. * instead invalidate it and ask the caller to update it via
  105. * ps3_hpte_insert() by returning a -1 value.
  106. */
  107. if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID)) {
  108. /* not found */
  109. ret = -1;
  110. } else {
  111. /* entry found, just invalidate it */
  112. result = lv1_write_htab_entry(PS3_LPAR_VAS_ID_CURRENT,
  113. slot, 0, 0);
  114. ret = -1;
  115. }
  116. spin_unlock_irqrestore(&ps3_htab_lock, flags);
  117. return ret;
  118. }
  119. static void ps3_hpte_updateboltedpp(unsigned long newpp, unsigned long ea,
  120. int psize, int ssize)
  121. {
  122. panic("ps3_hpte_updateboltedpp() not implemented");
  123. }
  124. static void ps3_hpte_invalidate(unsigned long slot, unsigned long vpn,
  125. int psize, int apsize, int ssize, int local)
  126. {
  127. unsigned long flags;
  128. int result;
  129. spin_lock_irqsave(&ps3_htab_lock, flags);
  130. result = lv1_write_htab_entry(PS3_LPAR_VAS_ID_CURRENT, slot, 0, 0);
  131. if (result) {
  132. pr_info("%s: result=%s vpn=%lx slot=%lx psize=%d\n",
  133. __func__, ps3_result(result), vpn, slot, psize);
  134. BUG();
  135. }
  136. spin_unlock_irqrestore(&ps3_htab_lock, flags);
  137. }
  138. /* Called during kexec sequence with MMU off */
  139. static notrace void ps3_hpte_clear(void)
  140. {
  141. unsigned long hpte_count = (1UL << ppc64_pft_size) >> 4;
  142. u64 i;
  143. for (i = 0; i < hpte_count; i++)
  144. lv1_write_htab_entry(PS3_LPAR_VAS_ID_CURRENT, i, 0, 0);
  145. ps3_mm_shutdown();
  146. ps3_mm_vas_destroy();
  147. }
  148. void __init ps3_hpte_init(unsigned long htab_size)
  149. {
  150. mmu_hash_ops.hpte_invalidate = ps3_hpte_invalidate;
  151. mmu_hash_ops.hpte_updatepp = ps3_hpte_updatepp;
  152. mmu_hash_ops.hpte_updateboltedpp = ps3_hpte_updateboltedpp;
  153. mmu_hash_ops.hpte_insert = ps3_hpte_insert;
  154. mmu_hash_ops.hpte_remove = ps3_hpte_remove;
  155. mmu_hash_ops.hpte_clear_all = ps3_hpte_clear;
  156. ppc64_pft_size = __ilog2(htab_size);
  157. }