book3s_hv_builtin.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License, version 2, as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/kvm_host.h>
  9. #include <linux/preempt.h>
  10. #include <linux/export.h>
  11. #include <linux/sched.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/bootmem.h>
  14. #include <linux/init.h>
  15. #include <asm/cputable.h>
  16. #include <asm/kvm_ppc.h>
  17. #include <asm/kvm_book3s.h>
  18. /*
  19. * This maintains a list of RMAs (real mode areas) for KVM guests to use.
  20. * Each RMA has to be physically contiguous and of a size that the
  21. * hardware supports. PPC970 and POWER7 support 64MB, 128MB and 256MB,
  22. * and other larger sizes. Since we are unlikely to be allocate that
  23. * much physically contiguous memory after the system is up and running,
  24. * we preallocate a set of RMAs in early boot for KVM to use.
  25. */
  26. static unsigned long kvm_rma_size = 64 << 20; /* 64MB */
  27. static unsigned long kvm_rma_count;
  28. static int __init early_parse_rma_size(char *p)
  29. {
  30. if (!p)
  31. return 1;
  32. kvm_rma_size = memparse(p, &p);
  33. return 0;
  34. }
  35. early_param("kvm_rma_size", early_parse_rma_size);
  36. static int __init early_parse_rma_count(char *p)
  37. {
  38. if (!p)
  39. return 1;
  40. kvm_rma_count = simple_strtoul(p, NULL, 0);
  41. return 0;
  42. }
  43. early_param("kvm_rma_count", early_parse_rma_count);
  44. static struct kvmppc_rma_info *rma_info;
  45. static LIST_HEAD(free_rmas);
  46. static DEFINE_SPINLOCK(rma_lock);
  47. /* Work out RMLS (real mode limit selector) field value for a given RMA size.
  48. Assumes POWER7 or PPC970. */
  49. static inline int lpcr_rmls(unsigned long rma_size)
  50. {
  51. switch (rma_size) {
  52. case 32ul << 20: /* 32 MB */
  53. if (cpu_has_feature(CPU_FTR_ARCH_206))
  54. return 8; /* only supported on POWER7 */
  55. return -1;
  56. case 64ul << 20: /* 64 MB */
  57. return 3;
  58. case 128ul << 20: /* 128 MB */
  59. return 7;
  60. case 256ul << 20: /* 256 MB */
  61. return 4;
  62. case 1ul << 30: /* 1 GB */
  63. return 2;
  64. case 16ul << 30: /* 16 GB */
  65. return 1;
  66. case 256ul << 30: /* 256 GB */
  67. return 0;
  68. default:
  69. return -1;
  70. }
  71. }
  72. /*
  73. * Called at boot time while the bootmem allocator is active,
  74. * to allocate contiguous physical memory for the real memory
  75. * areas for guests.
  76. */
  77. void kvm_rma_init(void)
  78. {
  79. unsigned long i;
  80. unsigned long j, npages;
  81. void *rma;
  82. struct page *pg;
  83. /* Only do this on PPC970 in HV mode */
  84. if (!cpu_has_feature(CPU_FTR_HVMODE) ||
  85. !cpu_has_feature(CPU_FTR_ARCH_201))
  86. return;
  87. if (!kvm_rma_size || !kvm_rma_count)
  88. return;
  89. /* Check that the requested size is one supported in hardware */
  90. if (lpcr_rmls(kvm_rma_size) < 0) {
  91. pr_err("RMA size of 0x%lx not supported\n", kvm_rma_size);
  92. return;
  93. }
  94. npages = kvm_rma_size >> PAGE_SHIFT;
  95. rma_info = alloc_bootmem(kvm_rma_count * sizeof(struct kvmppc_rma_info));
  96. for (i = 0; i < kvm_rma_count; ++i) {
  97. rma = alloc_bootmem_align(kvm_rma_size, kvm_rma_size);
  98. pr_info("Allocated KVM RMA at %p (%ld MB)\n", rma,
  99. kvm_rma_size >> 20);
  100. rma_info[i].base_virt = rma;
  101. rma_info[i].base_pfn = __pa(rma) >> PAGE_SHIFT;
  102. rma_info[i].npages = npages;
  103. list_add_tail(&rma_info[i].list, &free_rmas);
  104. atomic_set(&rma_info[i].use_count, 0);
  105. pg = pfn_to_page(rma_info[i].base_pfn);
  106. for (j = 0; j < npages; ++j) {
  107. atomic_inc(&pg->_count);
  108. ++pg;
  109. }
  110. }
  111. }
  112. struct kvmppc_rma_info *kvm_alloc_rma(void)
  113. {
  114. struct kvmppc_rma_info *ri;
  115. ri = NULL;
  116. spin_lock(&rma_lock);
  117. if (!list_empty(&free_rmas)) {
  118. ri = list_first_entry(&free_rmas, struct kvmppc_rma_info, list);
  119. list_del(&ri->list);
  120. atomic_inc(&ri->use_count);
  121. }
  122. spin_unlock(&rma_lock);
  123. return ri;
  124. }
  125. EXPORT_SYMBOL_GPL(kvm_alloc_rma);
  126. void kvm_release_rma(struct kvmppc_rma_info *ri)
  127. {
  128. if (atomic_dec_and_test(&ri->use_count)) {
  129. spin_lock(&rma_lock);
  130. list_add_tail(&ri->list, &free_rmas);
  131. spin_unlock(&rma_lock);
  132. }
  133. }
  134. EXPORT_SYMBOL_GPL(kvm_release_rma);