spapr.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. /*
  2. * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
  3. *
  4. * Copyright (c) 2004-2007 Fabrice Bellard
  5. * Copyright (c) 2007 Jocelyn Mayer
  6. * Copyright (c) 2010 David Gibson, IBM Corporation.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "sysemu.h"
  28. #include "hw.h"
  29. #include "elf.h"
  30. #include "net.h"
  31. #include "blockdev.h"
  32. #include "cpus.h"
  33. #include "kvm.h"
  34. #include "kvm_ppc.h"
  35. #include "hw/boards.h"
  36. #include "hw/ppc.h"
  37. #include "hw/loader.h"
  38. #include "hw/spapr.h"
  39. #include "hw/spapr_vio.h"
  40. #include "hw/spapr_pci.h"
  41. #include "hw/xics.h"
  42. #include "hw/pci/msi.h"
  43. #include "kvm.h"
  44. #include "kvm_ppc.h"
  45. #include "pci/pci.h"
  46. #include "exec-memory.h"
  47. #include "hw/usb.h"
  48. #include <libfdt.h>
  49. /* SLOF memory layout:
  50. *
  51. * SLOF raw image loaded at 0, copies its romfs right below the flat
  52. * device-tree, then position SLOF itself 31M below that
  53. *
  54. * So we set FW_OVERHEAD to 40MB which should account for all of that
  55. * and more
  56. *
  57. * We load our kernel at 4M, leaving space for SLOF initial image
  58. */
  59. #define FDT_MAX_SIZE 0x10000
  60. #define RTAS_MAX_SIZE 0x10000
  61. #define FW_MAX_SIZE 0x400000
  62. #define FW_FILE_NAME "slof.bin"
  63. #define FW_OVERHEAD 0x2800000
  64. #define KERNEL_LOAD_ADDR FW_MAX_SIZE
  65. #define MIN_RMA_SLOF 128UL
  66. #define TIMEBASE_FREQ 512000000ULL
  67. #define MAX_CPUS 256
  68. #define XICS_IRQS 1024
  69. #define SPAPR_PCI_BUID 0x800000020000001ULL
  70. #define SPAPR_PCI_MEM_WIN_ADDR (0x10000000000ULL + 0xA0000000)
  71. #define SPAPR_PCI_MEM_WIN_SIZE 0x20000000
  72. #define SPAPR_PCI_IO_WIN_ADDR (0x10000000000ULL + 0x80000000)
  73. #define SPAPR_PCI_MSI_WIN_ADDR (0x10000000000ULL + 0x90000000)
  74. #define PHANDLE_XICP 0x00001111
  75. #define HTAB_SIZE(spapr) (1ULL << ((spapr)->htab_shift))
  76. sPAPREnvironment *spapr;
  77. int spapr_allocate_irq(int hint, bool lsi)
  78. {
  79. int irq;
  80. if (hint) {
  81. irq = hint;
  82. /* FIXME: we should probably check for collisions somehow */
  83. } else {
  84. irq = spapr->next_irq++;
  85. }
  86. /* Configure irq type */
  87. if (!xics_get_qirq(spapr->icp, irq)) {
  88. return 0;
  89. }
  90. xics_set_irq_type(spapr->icp, irq, lsi);
  91. return irq;
  92. }
  93. /* Allocate block of consequtive IRQs, returns a number of the first */
  94. int spapr_allocate_irq_block(int num, bool lsi)
  95. {
  96. int first = -1;
  97. int i;
  98. for (i = 0; i < num; ++i) {
  99. int irq;
  100. irq = spapr_allocate_irq(0, lsi);
  101. if (!irq) {
  102. return -1;
  103. }
  104. if (0 == i) {
  105. first = irq;
  106. }
  107. /* If the above doesn't create a consecutive block then that's
  108. * an internal bug */
  109. assert(irq == (first + i));
  110. }
  111. return first;
  112. }
  113. static int spapr_fixup_cpu_dt(void *fdt, sPAPREnvironment *spapr)
  114. {
  115. int ret = 0, offset;
  116. CPUPPCState *env;
  117. char cpu_model[32];
  118. int smt = kvmppc_smt_threads();
  119. uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
  120. assert(spapr->cpu_model);
  121. for (env = first_cpu; env != NULL; env = env->next_cpu) {
  122. uint32_t associativity[] = {cpu_to_be32(0x5),
  123. cpu_to_be32(0x0),
  124. cpu_to_be32(0x0),
  125. cpu_to_be32(0x0),
  126. cpu_to_be32(env->numa_node),
  127. cpu_to_be32(env->cpu_index)};
  128. if ((env->cpu_index % smt) != 0) {
  129. continue;
  130. }
  131. snprintf(cpu_model, 32, "/cpus/%s@%x", spapr->cpu_model,
  132. env->cpu_index);
  133. offset = fdt_path_offset(fdt, cpu_model);
  134. if (offset < 0) {
  135. return offset;
  136. }
  137. if (nb_numa_nodes > 1) {
  138. ret = fdt_setprop(fdt, offset, "ibm,associativity", associativity,
  139. sizeof(associativity));
  140. if (ret < 0) {
  141. return ret;
  142. }
  143. }
  144. ret = fdt_setprop(fdt, offset, "ibm,pft-size",
  145. pft_size_prop, sizeof(pft_size_prop));
  146. if (ret < 0) {
  147. return ret;
  148. }
  149. }
  150. return ret;
  151. }
  152. static size_t create_page_sizes_prop(CPUPPCState *env, uint32_t *prop,
  153. size_t maxsize)
  154. {
  155. size_t maxcells = maxsize / sizeof(uint32_t);
  156. int i, j, count;
  157. uint32_t *p = prop;
  158. for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
  159. struct ppc_one_seg_page_size *sps = &env->sps.sps[i];
  160. if (!sps->page_shift) {
  161. break;
  162. }
  163. for (count = 0; count < PPC_PAGE_SIZES_MAX_SZ; count++) {
  164. if (sps->enc[count].page_shift == 0) {
  165. break;
  166. }
  167. }
  168. if ((p - prop) >= (maxcells - 3 - count * 2)) {
  169. break;
  170. }
  171. *(p++) = cpu_to_be32(sps->page_shift);
  172. *(p++) = cpu_to_be32(sps->slb_enc);
  173. *(p++) = cpu_to_be32(count);
  174. for (j = 0; j < count; j++) {
  175. *(p++) = cpu_to_be32(sps->enc[j].page_shift);
  176. *(p++) = cpu_to_be32(sps->enc[j].pte_enc);
  177. }
  178. }
  179. return (p - prop) * sizeof(uint32_t);
  180. }
  181. #define _FDT(exp) \
  182. do { \
  183. int ret = (exp); \
  184. if (ret < 0) { \
  185. fprintf(stderr, "qemu: error creating device tree: %s: %s\n", \
  186. #exp, fdt_strerror(ret)); \
  187. exit(1); \
  188. } \
  189. } while (0)
  190. static void *spapr_create_fdt_skel(const char *cpu_model,
  191. hwaddr initrd_base,
  192. hwaddr initrd_size,
  193. hwaddr kernel_size,
  194. const char *boot_device,
  195. const char *kernel_cmdline,
  196. uint32_t epow_irq)
  197. {
  198. void *fdt;
  199. CPUPPCState *env;
  200. uint32_t start_prop = cpu_to_be32(initrd_base);
  201. uint32_t end_prop = cpu_to_be32(initrd_base + initrd_size);
  202. char hypertas_prop[] = "hcall-pft\0hcall-term\0hcall-dabr\0hcall-interrupt"
  203. "\0hcall-tce\0hcall-vio\0hcall-splpar\0hcall-bulk";
  204. char qemu_hypertas_prop[] = "hcall-memop1";
  205. uint32_t refpoints[] = {cpu_to_be32(0x4), cpu_to_be32(0x4)};
  206. uint32_t interrupt_server_ranges_prop[] = {0, cpu_to_be32(smp_cpus)};
  207. char *modelname;
  208. int i, smt = kvmppc_smt_threads();
  209. unsigned char vec5[] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x80};
  210. fdt = g_malloc0(FDT_MAX_SIZE);
  211. _FDT((fdt_create(fdt, FDT_MAX_SIZE)));
  212. if (kernel_size) {
  213. _FDT((fdt_add_reservemap_entry(fdt, KERNEL_LOAD_ADDR, kernel_size)));
  214. }
  215. if (initrd_size) {
  216. _FDT((fdt_add_reservemap_entry(fdt, initrd_base, initrd_size)));
  217. }
  218. _FDT((fdt_finish_reservemap(fdt)));
  219. /* Root node */
  220. _FDT((fdt_begin_node(fdt, "")));
  221. _FDT((fdt_property_string(fdt, "device_type", "chrp")));
  222. _FDT((fdt_property_string(fdt, "model", "IBM pSeries (emulated by qemu)")));
  223. _FDT((fdt_property_cell(fdt, "#address-cells", 0x2)));
  224. _FDT((fdt_property_cell(fdt, "#size-cells", 0x2)));
  225. /* /chosen */
  226. _FDT((fdt_begin_node(fdt, "chosen")));
  227. /* Set Form1_affinity */
  228. _FDT((fdt_property(fdt, "ibm,architecture-vec-5", vec5, sizeof(vec5))));
  229. _FDT((fdt_property_string(fdt, "bootargs", kernel_cmdline)));
  230. _FDT((fdt_property(fdt, "linux,initrd-start",
  231. &start_prop, sizeof(start_prop))));
  232. _FDT((fdt_property(fdt, "linux,initrd-end",
  233. &end_prop, sizeof(end_prop))));
  234. if (kernel_size) {
  235. uint64_t kprop[2] = { cpu_to_be64(KERNEL_LOAD_ADDR),
  236. cpu_to_be64(kernel_size) };
  237. _FDT((fdt_property(fdt, "qemu,boot-kernel", &kprop, sizeof(kprop))));
  238. }
  239. _FDT((fdt_property_string(fdt, "qemu,boot-device", boot_device)));
  240. _FDT((fdt_property_cell(fdt, "qemu,graphic-width", graphic_width)));
  241. _FDT((fdt_property_cell(fdt, "qemu,graphic-height", graphic_height)));
  242. _FDT((fdt_property_cell(fdt, "qemu,graphic-depth", graphic_depth)));
  243. _FDT((fdt_end_node(fdt)));
  244. /* cpus */
  245. _FDT((fdt_begin_node(fdt, "cpus")));
  246. _FDT((fdt_property_cell(fdt, "#address-cells", 0x1)));
  247. _FDT((fdt_property_cell(fdt, "#size-cells", 0x0)));
  248. modelname = g_strdup(cpu_model);
  249. for (i = 0; i < strlen(modelname); i++) {
  250. modelname[i] = toupper(modelname[i]);
  251. }
  252. /* This is needed during FDT finalization */
  253. spapr->cpu_model = g_strdup(modelname);
  254. for (env = first_cpu; env != NULL; env = env->next_cpu) {
  255. int index = env->cpu_index;
  256. uint32_t servers_prop[smp_threads];
  257. uint32_t gservers_prop[smp_threads * 2];
  258. char *nodename;
  259. uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40),
  260. 0xffffffff, 0xffffffff};
  261. uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq() : TIMEBASE_FREQ;
  262. uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000;
  263. uint32_t page_sizes_prop[64];
  264. size_t page_sizes_prop_size;
  265. if ((index % smt) != 0) {
  266. continue;
  267. }
  268. if (asprintf(&nodename, "%s@%x", modelname, index) < 0) {
  269. fprintf(stderr, "Allocation failure\n");
  270. exit(1);
  271. }
  272. _FDT((fdt_begin_node(fdt, nodename)));
  273. free(nodename);
  274. _FDT((fdt_property_cell(fdt, "reg", index)));
  275. _FDT((fdt_property_string(fdt, "device_type", "cpu")));
  276. _FDT((fdt_property_cell(fdt, "cpu-version", env->spr[SPR_PVR])));
  277. _FDT((fdt_property_cell(fdt, "dcache-block-size",
  278. env->dcache_line_size)));
  279. _FDT((fdt_property_cell(fdt, "icache-block-size",
  280. env->icache_line_size)));
  281. _FDT((fdt_property_cell(fdt, "timebase-frequency", tbfreq)));
  282. _FDT((fdt_property_cell(fdt, "clock-frequency", cpufreq)));
  283. _FDT((fdt_property_cell(fdt, "ibm,slb-size", env->slb_nr)));
  284. _FDT((fdt_property_string(fdt, "status", "okay")));
  285. _FDT((fdt_property(fdt, "64-bit", NULL, 0)));
  286. /* Build interrupt servers and gservers properties */
  287. for (i = 0; i < smp_threads; i++) {
  288. servers_prop[i] = cpu_to_be32(index + i);
  289. /* Hack, direct the group queues back to cpu 0 */
  290. gservers_prop[i*2] = cpu_to_be32(index + i);
  291. gservers_prop[i*2 + 1] = 0;
  292. }
  293. _FDT((fdt_property(fdt, "ibm,ppc-interrupt-server#s",
  294. servers_prop, sizeof(servers_prop))));
  295. _FDT((fdt_property(fdt, "ibm,ppc-interrupt-gserver#s",
  296. gservers_prop, sizeof(gservers_prop))));
  297. if (env->mmu_model & POWERPC_MMU_1TSEG) {
  298. _FDT((fdt_property(fdt, "ibm,processor-segment-sizes",
  299. segs, sizeof(segs))));
  300. }
  301. /* Advertise VMX/VSX (vector extensions) if available
  302. * 0 / no property == no vector extensions
  303. * 1 == VMX / Altivec available
  304. * 2 == VSX available */
  305. if (env->insns_flags & PPC_ALTIVEC) {
  306. uint32_t vmx = (env->insns_flags2 & PPC2_VSX) ? 2 : 1;
  307. _FDT((fdt_property_cell(fdt, "ibm,vmx", vmx)));
  308. }
  309. /* Advertise DFP (Decimal Floating Point) if available
  310. * 0 / no property == no DFP
  311. * 1 == DFP available */
  312. if (env->insns_flags2 & PPC2_DFP) {
  313. _FDT((fdt_property_cell(fdt, "ibm,dfp", 1)));
  314. }
  315. page_sizes_prop_size = create_page_sizes_prop(env, page_sizes_prop,
  316. sizeof(page_sizes_prop));
  317. if (page_sizes_prop_size) {
  318. _FDT((fdt_property(fdt, "ibm,segment-page-sizes",
  319. page_sizes_prop, page_sizes_prop_size)));
  320. }
  321. _FDT((fdt_end_node(fdt)));
  322. }
  323. g_free(modelname);
  324. _FDT((fdt_end_node(fdt)));
  325. /* RTAS */
  326. _FDT((fdt_begin_node(fdt, "rtas")));
  327. _FDT((fdt_property(fdt, "ibm,hypertas-functions", hypertas_prop,
  328. sizeof(hypertas_prop))));
  329. _FDT((fdt_property(fdt, "qemu,hypertas-functions", qemu_hypertas_prop,
  330. sizeof(qemu_hypertas_prop))));
  331. _FDT((fdt_property(fdt, "ibm,associativity-reference-points",
  332. refpoints, sizeof(refpoints))));
  333. _FDT((fdt_property_cell(fdt, "rtas-error-log-max", RTAS_ERROR_LOG_MAX)));
  334. _FDT((fdt_end_node(fdt)));
  335. /* interrupt controller */
  336. _FDT((fdt_begin_node(fdt, "interrupt-controller")));
  337. _FDT((fdt_property_string(fdt, "device_type",
  338. "PowerPC-External-Interrupt-Presentation")));
  339. _FDT((fdt_property_string(fdt, "compatible", "IBM,ppc-xicp")));
  340. _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0)));
  341. _FDT((fdt_property(fdt, "ibm,interrupt-server-ranges",
  342. interrupt_server_ranges_prop,
  343. sizeof(interrupt_server_ranges_prop))));
  344. _FDT((fdt_property_cell(fdt, "#interrupt-cells", 2)));
  345. _FDT((fdt_property_cell(fdt, "linux,phandle", PHANDLE_XICP)));
  346. _FDT((fdt_property_cell(fdt, "phandle", PHANDLE_XICP)));
  347. _FDT((fdt_end_node(fdt)));
  348. /* vdevice */
  349. _FDT((fdt_begin_node(fdt, "vdevice")));
  350. _FDT((fdt_property_string(fdt, "device_type", "vdevice")));
  351. _FDT((fdt_property_string(fdt, "compatible", "IBM,vdevice")));
  352. _FDT((fdt_property_cell(fdt, "#address-cells", 0x1)));
  353. _FDT((fdt_property_cell(fdt, "#size-cells", 0x0)));
  354. _FDT((fdt_property_cell(fdt, "#interrupt-cells", 0x2)));
  355. _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0)));
  356. _FDT((fdt_end_node(fdt)));
  357. /* event-sources */
  358. spapr_events_fdt_skel(fdt, epow_irq);
  359. _FDT((fdt_end_node(fdt))); /* close root node */
  360. _FDT((fdt_finish(fdt)));
  361. return fdt;
  362. }
  363. static int spapr_populate_memory(sPAPREnvironment *spapr, void *fdt)
  364. {
  365. uint32_t associativity[] = {cpu_to_be32(0x4), cpu_to_be32(0x0),
  366. cpu_to_be32(0x0), cpu_to_be32(0x0),
  367. cpu_to_be32(0x0)};
  368. char mem_name[32];
  369. hwaddr node0_size, mem_start;
  370. uint64_t mem_reg_property[2];
  371. int i, off;
  372. /* memory node(s) */
  373. node0_size = (nb_numa_nodes > 1) ? node_mem[0] : ram_size;
  374. if (spapr->rma_size > node0_size) {
  375. spapr->rma_size = node0_size;
  376. }
  377. /* RMA */
  378. mem_reg_property[0] = 0;
  379. mem_reg_property[1] = cpu_to_be64(spapr->rma_size);
  380. off = fdt_add_subnode(fdt, 0, "memory@0");
  381. _FDT(off);
  382. _FDT((fdt_setprop_string(fdt, off, "device_type", "memory")));
  383. _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property,
  384. sizeof(mem_reg_property))));
  385. _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity,
  386. sizeof(associativity))));
  387. /* RAM: Node 0 */
  388. if (node0_size > spapr->rma_size) {
  389. mem_reg_property[0] = cpu_to_be64(spapr->rma_size);
  390. mem_reg_property[1] = cpu_to_be64(node0_size - spapr->rma_size);
  391. sprintf(mem_name, "memory@" TARGET_FMT_lx, spapr->rma_size);
  392. off = fdt_add_subnode(fdt, 0, mem_name);
  393. _FDT(off);
  394. _FDT((fdt_setprop_string(fdt, off, "device_type", "memory")));
  395. _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property,
  396. sizeof(mem_reg_property))));
  397. _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity,
  398. sizeof(associativity))));
  399. }
  400. /* RAM: Node 1 and beyond */
  401. mem_start = node0_size;
  402. for (i = 1; i < nb_numa_nodes; i++) {
  403. mem_reg_property[0] = cpu_to_be64(mem_start);
  404. mem_reg_property[1] = cpu_to_be64(node_mem[i]);
  405. associativity[3] = associativity[4] = cpu_to_be32(i);
  406. sprintf(mem_name, "memory@" TARGET_FMT_lx, mem_start);
  407. off = fdt_add_subnode(fdt, 0, mem_name);
  408. _FDT(off);
  409. _FDT((fdt_setprop_string(fdt, off, "device_type", "memory")));
  410. _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property,
  411. sizeof(mem_reg_property))));
  412. _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity,
  413. sizeof(associativity))));
  414. mem_start += node_mem[i];
  415. }
  416. return 0;
  417. }
  418. static void spapr_finalize_fdt(sPAPREnvironment *spapr,
  419. hwaddr fdt_addr,
  420. hwaddr rtas_addr,
  421. hwaddr rtas_size)
  422. {
  423. int ret;
  424. void *fdt;
  425. sPAPRPHBState *phb;
  426. fdt = g_malloc(FDT_MAX_SIZE);
  427. /* open out the base tree into a temp buffer for the final tweaks */
  428. _FDT((fdt_open_into(spapr->fdt_skel, fdt, FDT_MAX_SIZE)));
  429. ret = spapr_populate_memory(spapr, fdt);
  430. if (ret < 0) {
  431. fprintf(stderr, "couldn't setup memory nodes in fdt\n");
  432. exit(1);
  433. }
  434. ret = spapr_populate_vdevice(spapr->vio_bus, fdt);
  435. if (ret < 0) {
  436. fprintf(stderr, "couldn't setup vio devices in fdt\n");
  437. exit(1);
  438. }
  439. QLIST_FOREACH(phb, &spapr->phbs, list) {
  440. ret = spapr_populate_pci_dt(phb, PHANDLE_XICP, fdt);
  441. }
  442. if (ret < 0) {
  443. fprintf(stderr, "couldn't setup PCI devices in fdt\n");
  444. exit(1);
  445. }
  446. /* RTAS */
  447. ret = spapr_rtas_device_tree_setup(fdt, rtas_addr, rtas_size);
  448. if (ret < 0) {
  449. fprintf(stderr, "Couldn't set up RTAS device tree properties\n");
  450. }
  451. /* Advertise NUMA via ibm,associativity */
  452. ret = spapr_fixup_cpu_dt(fdt, spapr);
  453. if (ret < 0) {
  454. fprintf(stderr, "Couldn't finalize CPU device tree properties\n");
  455. }
  456. if (!spapr->has_graphics) {
  457. spapr_populate_chosen_stdout(fdt, spapr->vio_bus);
  458. }
  459. _FDT((fdt_pack(fdt)));
  460. if (fdt_totalsize(fdt) > FDT_MAX_SIZE) {
  461. hw_error("FDT too big ! 0x%x bytes (max is 0x%x)\n",
  462. fdt_totalsize(fdt), FDT_MAX_SIZE);
  463. exit(1);
  464. }
  465. cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt));
  466. g_free(fdt);
  467. }
  468. static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
  469. {
  470. return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR;
  471. }
  472. static void emulate_spapr_hypercall(PowerPCCPU *cpu)
  473. {
  474. CPUPPCState *env = &cpu->env;
  475. if (msr_pr) {
  476. hcall_dprintf("Hypercall made with MSR[PR]=1\n");
  477. env->gpr[3] = H_PRIVILEGE;
  478. } else {
  479. env->gpr[3] = spapr_hypercall(cpu, env->gpr[3], &env->gpr[4]);
  480. }
  481. }
  482. static void spapr_reset_htab(sPAPREnvironment *spapr)
  483. {
  484. long shift;
  485. /* allocate hash page table. For now we always make this 16mb,
  486. * later we should probably make it scale to the size of guest
  487. * RAM */
  488. shift = kvmppc_reset_htab(spapr->htab_shift);
  489. if (shift > 0) {
  490. /* Kernel handles htab, we don't need to allocate one */
  491. spapr->htab_shift = shift;
  492. } else {
  493. if (!spapr->htab) {
  494. /* Allocate an htab if we don't yet have one */
  495. spapr->htab = qemu_memalign(HTAB_SIZE(spapr), HTAB_SIZE(spapr));
  496. }
  497. /* And clear it */
  498. memset(spapr->htab, 0, HTAB_SIZE(spapr));
  499. }
  500. /* Update the RMA size if necessary */
  501. if (spapr->vrma_adjust) {
  502. spapr->rma_size = kvmppc_rma_size(ram_size, spapr->htab_shift);
  503. }
  504. }
  505. static void ppc_spapr_reset(void)
  506. {
  507. /* Reset the hash table & recalc the RMA */
  508. spapr_reset_htab(spapr);
  509. qemu_devices_reset();
  510. /* Load the fdt */
  511. spapr_finalize_fdt(spapr, spapr->fdt_addr, spapr->rtas_addr,
  512. spapr->rtas_size);
  513. /* Set up the entry state */
  514. first_cpu->gpr[3] = spapr->fdt_addr;
  515. first_cpu->gpr[5] = 0;
  516. first_cpu->halted = 0;
  517. first_cpu->nip = spapr->entry_point;
  518. }
  519. static void spapr_cpu_reset(void *opaque)
  520. {
  521. PowerPCCPU *cpu = opaque;
  522. CPUPPCState *env = &cpu->env;
  523. cpu_reset(CPU(cpu));
  524. /* All CPUs start halted. CPU0 is unhalted from the machine level
  525. * reset code and the rest are explicitly started up by the guest
  526. * using an RTAS call */
  527. env->halted = 1;
  528. env->spr[SPR_HIOR] = 0;
  529. env->external_htab = spapr->htab;
  530. env->htab_base = -1;
  531. env->htab_mask = HTAB_SIZE(spapr) - 1;
  532. env->spr[SPR_SDR1] = (unsigned long)spapr->htab |
  533. (spapr->htab_shift - 18);
  534. }
  535. /* Returns whether we want to use VGA or not */
  536. static int spapr_vga_init(PCIBus *pci_bus)
  537. {
  538. switch (vga_interface_type) {
  539. case VGA_NONE:
  540. case VGA_STD:
  541. return pci_vga_init(pci_bus) != NULL;
  542. default:
  543. fprintf(stderr, "This vga model is not supported,"
  544. "currently it only supports -vga std\n");
  545. exit(0);
  546. break;
  547. }
  548. }
  549. /* pSeries LPAR / sPAPR hardware init */
  550. static void ppc_spapr_init(QEMUMachineInitArgs *args)
  551. {
  552. ram_addr_t ram_size = args->ram_size;
  553. const char *cpu_model = args->cpu_model;
  554. const char *kernel_filename = args->kernel_filename;
  555. const char *kernel_cmdline = args->kernel_cmdline;
  556. const char *initrd_filename = args->initrd_filename;
  557. const char *boot_device = args->boot_device;
  558. PowerPCCPU *cpu;
  559. CPUPPCState *env;
  560. PCIHostState *phb;
  561. int i;
  562. MemoryRegion *sysmem = get_system_memory();
  563. MemoryRegion *ram = g_new(MemoryRegion, 1);
  564. hwaddr rma_alloc_size;
  565. uint32_t initrd_base = 0;
  566. long kernel_size = 0, initrd_size = 0;
  567. long load_limit, rtas_limit, fw_size;
  568. char *filename;
  569. msi_supported = true;
  570. spapr = g_malloc0(sizeof(*spapr));
  571. QLIST_INIT(&spapr->phbs);
  572. cpu_ppc_hypercall = emulate_spapr_hypercall;
  573. /* Allocate RMA if necessary */
  574. rma_alloc_size = kvmppc_alloc_rma("ppc_spapr.rma", sysmem);
  575. if (rma_alloc_size == -1) {
  576. hw_error("qemu: Unable to create RMA\n");
  577. exit(1);
  578. }
  579. if (rma_alloc_size && (rma_alloc_size < ram_size)) {
  580. spapr->rma_size = rma_alloc_size;
  581. } else {
  582. spapr->rma_size = ram_size;
  583. /* With KVM, we don't actually know whether KVM supports an
  584. * unbounded RMA (PR KVM) or is limited by the hash table size
  585. * (HV KVM using VRMA), so we always assume the latter
  586. *
  587. * In that case, we also limit the initial allocations for RTAS
  588. * etc... to 256M since we have no way to know what the VRMA size
  589. * is going to be as it depends on the size of the hash table
  590. * isn't determined yet.
  591. */
  592. if (kvm_enabled()) {
  593. spapr->vrma_adjust = 1;
  594. spapr->rma_size = MIN(spapr->rma_size, 0x10000000);
  595. }
  596. }
  597. /* We place the device tree and RTAS just below either the top of the RMA,
  598. * or just below 2GB, whichever is lowere, so that it can be
  599. * processed with 32-bit real mode code if necessary */
  600. rtas_limit = MIN(spapr->rma_size, 0x80000000);
  601. spapr->rtas_addr = rtas_limit - RTAS_MAX_SIZE;
  602. spapr->fdt_addr = spapr->rtas_addr - FDT_MAX_SIZE;
  603. load_limit = spapr->fdt_addr - FW_OVERHEAD;
  604. /* We aim for a hash table of size 1/128 the size of RAM. The
  605. * normal rule of thumb is 1/64 the size of RAM, but that's much
  606. * more than needed for the Linux guests we support. */
  607. spapr->htab_shift = 18; /* Minimum architected size */
  608. while (spapr->htab_shift <= 46) {
  609. if ((1ULL << (spapr->htab_shift + 7)) >= ram_size) {
  610. break;
  611. }
  612. spapr->htab_shift++;
  613. }
  614. /* init CPUs */
  615. if (cpu_model == NULL) {
  616. cpu_model = kvm_enabled() ? "host" : "POWER7";
  617. }
  618. for (i = 0; i < smp_cpus; i++) {
  619. cpu = cpu_ppc_init(cpu_model);
  620. if (cpu == NULL) {
  621. fprintf(stderr, "Unable to find PowerPC CPU definition\n");
  622. exit(1);
  623. }
  624. env = &cpu->env;
  625. /* Set time-base frequency to 512 MHz */
  626. cpu_ppc_tb_init(env, TIMEBASE_FREQ);
  627. /* PAPR always has exception vectors in RAM not ROM */
  628. env->hreset_excp_prefix = 0;
  629. /* Tell KVM that we're in PAPR mode */
  630. if (kvm_enabled()) {
  631. kvmppc_set_papr(env);
  632. }
  633. qemu_register_reset(spapr_cpu_reset, cpu);
  634. }
  635. /* allocate RAM */
  636. spapr->ram_limit = ram_size;
  637. if (spapr->ram_limit > rma_alloc_size) {
  638. ram_addr_t nonrma_base = rma_alloc_size;
  639. ram_addr_t nonrma_size = spapr->ram_limit - rma_alloc_size;
  640. memory_region_init_ram(ram, "ppc_spapr.ram", nonrma_size);
  641. vmstate_register_ram_global(ram);
  642. memory_region_add_subregion(sysmem, nonrma_base, ram);
  643. }
  644. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "spapr-rtas.bin");
  645. spapr->rtas_size = load_image_targphys(filename, spapr->rtas_addr,
  646. rtas_limit - spapr->rtas_addr);
  647. if (spapr->rtas_size < 0) {
  648. hw_error("qemu: could not load LPAR rtas '%s'\n", filename);
  649. exit(1);
  650. }
  651. if (spapr->rtas_size > RTAS_MAX_SIZE) {
  652. hw_error("RTAS too big ! 0x%lx bytes (max is 0x%x)\n",
  653. spapr->rtas_size, RTAS_MAX_SIZE);
  654. exit(1);
  655. }
  656. g_free(filename);
  657. /* Set up Interrupt Controller */
  658. spapr->icp = xics_system_init(XICS_IRQS);
  659. spapr->next_irq = 16;
  660. /* Set up EPOW events infrastructure */
  661. spapr_events_init(spapr);
  662. /* Set up IOMMU */
  663. spapr_iommu_init();
  664. /* Set up VIO bus */
  665. spapr->vio_bus = spapr_vio_bus_init();
  666. for (i = 0; i < MAX_SERIAL_PORTS; i++) {
  667. if (serial_hds[i]) {
  668. spapr_vty_create(spapr->vio_bus, serial_hds[i]);
  669. }
  670. }
  671. /* Set up PCI */
  672. spapr_pci_rtas_init();
  673. spapr_create_phb(spapr, "pci", SPAPR_PCI_BUID,
  674. SPAPR_PCI_MEM_WIN_ADDR,
  675. SPAPR_PCI_MEM_WIN_SIZE,
  676. SPAPR_PCI_IO_WIN_ADDR,
  677. SPAPR_PCI_MSI_WIN_ADDR);
  678. phb = PCI_HOST_BRIDGE(QLIST_FIRST(&spapr->phbs));
  679. for (i = 0; i < nb_nics; i++) {
  680. NICInfo *nd = &nd_table[i];
  681. if (!nd->model) {
  682. nd->model = g_strdup("ibmveth");
  683. }
  684. if (strcmp(nd->model, "ibmveth") == 0) {
  685. spapr_vlan_create(spapr->vio_bus, nd);
  686. } else {
  687. pci_nic_init_nofail(&nd_table[i], nd->model, NULL);
  688. }
  689. }
  690. for (i = 0; i <= drive_get_max_bus(IF_SCSI); i++) {
  691. spapr_vscsi_create(spapr->vio_bus);
  692. }
  693. /* Graphics */
  694. if (spapr_vga_init(phb->bus)) {
  695. spapr->has_graphics = true;
  696. }
  697. if (usb_enabled(spapr->has_graphics)) {
  698. pci_create_simple(phb->bus, -1, "pci-ohci");
  699. if (spapr->has_graphics) {
  700. usbdevice_create("keyboard");
  701. usbdevice_create("mouse");
  702. }
  703. }
  704. if (spapr->rma_size < (MIN_RMA_SLOF << 20)) {
  705. fprintf(stderr, "qemu: pSeries SLOF firmware requires >= "
  706. "%ldM guest RMA (Real Mode Area memory)\n", MIN_RMA_SLOF);
  707. exit(1);
  708. }
  709. if (kernel_filename) {
  710. uint64_t lowaddr = 0;
  711. kernel_size = load_elf(kernel_filename, translate_kernel_address, NULL,
  712. NULL, &lowaddr, NULL, 1, ELF_MACHINE, 0);
  713. if (kernel_size < 0) {
  714. kernel_size = load_image_targphys(kernel_filename,
  715. KERNEL_LOAD_ADDR,
  716. load_limit - KERNEL_LOAD_ADDR);
  717. }
  718. if (kernel_size < 0) {
  719. fprintf(stderr, "qemu: could not load kernel '%s'\n",
  720. kernel_filename);
  721. exit(1);
  722. }
  723. /* load initrd */
  724. if (initrd_filename) {
  725. /* Try to locate the initrd in the gap between the kernel
  726. * and the firmware. Add a bit of space just in case
  727. */
  728. initrd_base = (KERNEL_LOAD_ADDR + kernel_size + 0x1ffff) & ~0xffff;
  729. initrd_size = load_image_targphys(initrd_filename, initrd_base,
  730. load_limit - initrd_base);
  731. if (initrd_size < 0) {
  732. fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
  733. initrd_filename);
  734. exit(1);
  735. }
  736. } else {
  737. initrd_base = 0;
  738. initrd_size = 0;
  739. }
  740. }
  741. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, FW_FILE_NAME);
  742. fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE);
  743. if (fw_size < 0) {
  744. hw_error("qemu: could not load LPAR rtas '%s'\n", filename);
  745. exit(1);
  746. }
  747. g_free(filename);
  748. spapr->entry_point = 0x100;
  749. /* Prepare the device tree */
  750. spapr->fdt_skel = spapr_create_fdt_skel(cpu_model,
  751. initrd_base, initrd_size,
  752. kernel_size,
  753. boot_device, kernel_cmdline,
  754. spapr->epow_irq);
  755. assert(spapr->fdt_skel != NULL);
  756. }
  757. static QEMUMachine spapr_machine = {
  758. .name = "pseries",
  759. .desc = "pSeries Logical Partition (PAPR compliant)",
  760. .init = ppc_spapr_init,
  761. .reset = ppc_spapr_reset,
  762. .max_cpus = MAX_CPUS,
  763. .no_parallel = 1,
  764. .use_scsi = 1,
  765. };
  766. static void spapr_machine_init(void)
  767. {
  768. qemu_register_machine(&spapr_machine);
  769. }
  770. machine_init(spapr_machine_init);