spapr.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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/msi.h"
  43. #include "kvm.h"
  44. #include "kvm_ppc.h"
  45. #include "pci.h"
  46. #include "vga-pci.h"
  47. #include "exec-memory.h"
  48. #include "hw/usb.h"
  49. #include <libfdt.h>
  50. /* SLOF memory layout:
  51. *
  52. * SLOF raw image loaded at 0, copies its romfs right below the flat
  53. * device-tree, then position SLOF itself 31M below that
  54. *
  55. * So we set FW_OVERHEAD to 40MB which should account for all of that
  56. * and more
  57. *
  58. * We load our kernel at 4M, leaving space for SLOF initial image
  59. */
  60. #define FDT_MAX_SIZE 0x10000
  61. #define RTAS_MAX_SIZE 0x10000
  62. #define FW_MAX_SIZE 0x400000
  63. #define FW_FILE_NAME "slof.bin"
  64. #define FW_OVERHEAD 0x2800000
  65. #define KERNEL_LOAD_ADDR FW_MAX_SIZE
  66. #define MIN_RMA_SLOF 128UL
  67. #define TIMEBASE_FREQ 512000000ULL
  68. #define MAX_CPUS 256
  69. #define XICS_IRQS 1024
  70. #define SPAPR_PCI_BUID 0x800000020000001ULL
  71. #define SPAPR_PCI_MEM_WIN_ADDR (0x10000000000ULL + 0xA0000000)
  72. #define SPAPR_PCI_MEM_WIN_SIZE 0x20000000
  73. #define SPAPR_PCI_IO_WIN_ADDR (0x10000000000ULL + 0x80000000)
  74. #define SPAPR_PCI_MSI_WIN_ADDR (0x10000000000ULL + 0x90000000)
  75. #define PHANDLE_XICP 0x00001111
  76. sPAPREnvironment *spapr;
  77. int spapr_allocate_irq(int hint, enum xics_irq_type type)
  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, type);
  91. return irq;
  92. }
  93. /* Allocate block of consequtive IRQs, returns a number of the first */
  94. int spapr_allocate_irq_block(int num, enum xics_irq_type type)
  95. {
  96. int first = -1;
  97. int i;
  98. for (i = 0; i < num; ++i) {
  99. int irq;
  100. irq = spapr_allocate_irq(0, type);
  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_set_associativity(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. assert(spapr->cpu_model);
  120. for (env = first_cpu; env != NULL; env = env->next_cpu) {
  121. uint32_t associativity[] = {cpu_to_be32(0x5),
  122. cpu_to_be32(0x0),
  123. cpu_to_be32(0x0),
  124. cpu_to_be32(0x0),
  125. cpu_to_be32(env->numa_node),
  126. cpu_to_be32(env->cpu_index)};
  127. if ((env->cpu_index % smt) != 0) {
  128. continue;
  129. }
  130. snprintf(cpu_model, 32, "/cpus/%s@%x", spapr->cpu_model,
  131. env->cpu_index);
  132. offset = fdt_path_offset(fdt, cpu_model);
  133. if (offset < 0) {
  134. return offset;
  135. }
  136. ret = fdt_setprop(fdt, offset, "ibm,associativity", associativity,
  137. sizeof(associativity));
  138. if (ret < 0) {
  139. return ret;
  140. }
  141. }
  142. return ret;
  143. }
  144. static size_t create_page_sizes_prop(CPUPPCState *env, uint32_t *prop,
  145. size_t maxsize)
  146. {
  147. size_t maxcells = maxsize / sizeof(uint32_t);
  148. int i, j, count;
  149. uint32_t *p = prop;
  150. for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
  151. struct ppc_one_seg_page_size *sps = &env->sps.sps[i];
  152. if (!sps->page_shift) {
  153. break;
  154. }
  155. for (count = 0; count < PPC_PAGE_SIZES_MAX_SZ; count++) {
  156. if (sps->enc[count].page_shift == 0) {
  157. break;
  158. }
  159. }
  160. if ((p - prop) >= (maxcells - 3 - count * 2)) {
  161. break;
  162. }
  163. *(p++) = cpu_to_be32(sps->page_shift);
  164. *(p++) = cpu_to_be32(sps->slb_enc);
  165. *(p++) = cpu_to_be32(count);
  166. for (j = 0; j < count; j++) {
  167. *(p++) = cpu_to_be32(sps->enc[j].page_shift);
  168. *(p++) = cpu_to_be32(sps->enc[j].pte_enc);
  169. }
  170. }
  171. return (p - prop) * sizeof(uint32_t);
  172. }
  173. static void *spapr_create_fdt_skel(const char *cpu_model,
  174. target_phys_addr_t rma_size,
  175. target_phys_addr_t initrd_base,
  176. target_phys_addr_t initrd_size,
  177. target_phys_addr_t kernel_size,
  178. const char *boot_device,
  179. const char *kernel_cmdline,
  180. long hash_shift)
  181. {
  182. void *fdt;
  183. CPUPPCState *env;
  184. uint64_t mem_reg_property[2];
  185. uint32_t start_prop = cpu_to_be32(initrd_base);
  186. uint32_t end_prop = cpu_to_be32(initrd_base + initrd_size);
  187. uint32_t pft_size_prop[] = {0, cpu_to_be32(hash_shift)};
  188. char hypertas_prop[] = "hcall-pft\0hcall-term\0hcall-dabr\0hcall-interrupt"
  189. "\0hcall-tce\0hcall-vio\0hcall-splpar\0hcall-bulk";
  190. char qemu_hypertas_prop[] = "hcall-memop1";
  191. uint32_t interrupt_server_ranges_prop[] = {0, cpu_to_be32(smp_cpus)};
  192. int i;
  193. char *modelname;
  194. int smt = kvmppc_smt_threads();
  195. unsigned char vec5[] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x80};
  196. uint32_t refpoints[] = {cpu_to_be32(0x4), cpu_to_be32(0x4)};
  197. uint32_t associativity[] = {cpu_to_be32(0x4), cpu_to_be32(0x0),
  198. cpu_to_be32(0x0), cpu_to_be32(0x0),
  199. cpu_to_be32(0x0)};
  200. char mem_name[32];
  201. target_phys_addr_t node0_size, mem_start;
  202. #define _FDT(exp) \
  203. do { \
  204. int ret = (exp); \
  205. if (ret < 0) { \
  206. fprintf(stderr, "qemu: error creating device tree: %s: %s\n", \
  207. #exp, fdt_strerror(ret)); \
  208. exit(1); \
  209. } \
  210. } while (0)
  211. fdt = g_malloc0(FDT_MAX_SIZE);
  212. _FDT((fdt_create(fdt, FDT_MAX_SIZE)));
  213. if (kernel_size) {
  214. _FDT((fdt_add_reservemap_entry(fdt, KERNEL_LOAD_ADDR, kernel_size)));
  215. }
  216. if (initrd_size) {
  217. _FDT((fdt_add_reservemap_entry(fdt, initrd_base, initrd_size)));
  218. }
  219. _FDT((fdt_finish_reservemap(fdt)));
  220. /* Root node */
  221. _FDT((fdt_begin_node(fdt, "")));
  222. _FDT((fdt_property_string(fdt, "device_type", "chrp")));
  223. _FDT((fdt_property_string(fdt, "model", "IBM pSeries (emulated by qemu)")));
  224. _FDT((fdt_property_cell(fdt, "#address-cells", 0x2)));
  225. _FDT((fdt_property_cell(fdt, "#size-cells", 0x2)));
  226. /* /chosen */
  227. _FDT((fdt_begin_node(fdt, "chosen")));
  228. /* Set Form1_affinity */
  229. _FDT((fdt_property(fdt, "ibm,architecture-vec-5", vec5, sizeof(vec5))));
  230. _FDT((fdt_property_string(fdt, "bootargs", kernel_cmdline)));
  231. _FDT((fdt_property(fdt, "linux,initrd-start",
  232. &start_prop, sizeof(start_prop))));
  233. _FDT((fdt_property(fdt, "linux,initrd-end",
  234. &end_prop, sizeof(end_prop))));
  235. if (kernel_size) {
  236. uint64_t kprop[2] = { cpu_to_be64(KERNEL_LOAD_ADDR),
  237. cpu_to_be64(kernel_size) };
  238. _FDT((fdt_property(fdt, "qemu,boot-kernel", &kprop, sizeof(kprop))));
  239. }
  240. _FDT((fdt_property_string(fdt, "qemu,boot-device", boot_device)));
  241. _FDT((fdt_property_cell(fdt, "qemu,graphic-width", graphic_width)));
  242. _FDT((fdt_property_cell(fdt, "qemu,graphic-height", graphic_height)));
  243. _FDT((fdt_property_cell(fdt, "qemu,graphic-depth", graphic_depth)));
  244. _FDT((fdt_end_node(fdt)));
  245. /* memory node(s) */
  246. node0_size = (nb_numa_nodes > 1) ? node_mem[0] : ram_size;
  247. if (rma_size > node0_size) {
  248. rma_size = node0_size;
  249. }
  250. /* RMA */
  251. mem_reg_property[0] = 0;
  252. mem_reg_property[1] = cpu_to_be64(rma_size);
  253. _FDT((fdt_begin_node(fdt, "memory@0")));
  254. _FDT((fdt_property_string(fdt, "device_type", "memory")));
  255. _FDT((fdt_property(fdt, "reg", mem_reg_property,
  256. sizeof(mem_reg_property))));
  257. _FDT((fdt_property(fdt, "ibm,associativity", associativity,
  258. sizeof(associativity))));
  259. _FDT((fdt_end_node(fdt)));
  260. /* RAM: Node 0 */
  261. if (node0_size > rma_size) {
  262. mem_reg_property[0] = cpu_to_be64(rma_size);
  263. mem_reg_property[1] = cpu_to_be64(node0_size - rma_size);
  264. sprintf(mem_name, "memory@" TARGET_FMT_lx, rma_size);
  265. _FDT((fdt_begin_node(fdt, mem_name)));
  266. _FDT((fdt_property_string(fdt, "device_type", "memory")));
  267. _FDT((fdt_property(fdt, "reg", mem_reg_property,
  268. sizeof(mem_reg_property))));
  269. _FDT((fdt_property(fdt, "ibm,associativity", associativity,
  270. sizeof(associativity))));
  271. _FDT((fdt_end_node(fdt)));
  272. }
  273. /* RAM: Node 1 and beyond */
  274. mem_start = node0_size;
  275. for (i = 1; i < nb_numa_nodes; i++) {
  276. mem_reg_property[0] = cpu_to_be64(mem_start);
  277. mem_reg_property[1] = cpu_to_be64(node_mem[i]);
  278. associativity[3] = associativity[4] = cpu_to_be32(i);
  279. sprintf(mem_name, "memory@" TARGET_FMT_lx, mem_start);
  280. _FDT((fdt_begin_node(fdt, mem_name)));
  281. _FDT((fdt_property_string(fdt, "device_type", "memory")));
  282. _FDT((fdt_property(fdt, "reg", mem_reg_property,
  283. sizeof(mem_reg_property))));
  284. _FDT((fdt_property(fdt, "ibm,associativity", associativity,
  285. sizeof(associativity))));
  286. _FDT((fdt_end_node(fdt)));
  287. mem_start += node_mem[i];
  288. }
  289. /* cpus */
  290. _FDT((fdt_begin_node(fdt, "cpus")));
  291. _FDT((fdt_property_cell(fdt, "#address-cells", 0x1)));
  292. _FDT((fdt_property_cell(fdt, "#size-cells", 0x0)));
  293. modelname = g_strdup(cpu_model);
  294. for (i = 0; i < strlen(modelname); i++) {
  295. modelname[i] = toupper(modelname[i]);
  296. }
  297. /* This is needed during FDT finalization */
  298. spapr->cpu_model = g_strdup(modelname);
  299. for (env = first_cpu; env != NULL; env = env->next_cpu) {
  300. int index = env->cpu_index;
  301. uint32_t servers_prop[smp_threads];
  302. uint32_t gservers_prop[smp_threads * 2];
  303. char *nodename;
  304. uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40),
  305. 0xffffffff, 0xffffffff};
  306. uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq() : TIMEBASE_FREQ;
  307. uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000;
  308. uint32_t page_sizes_prop[64];
  309. size_t page_sizes_prop_size;
  310. if ((index % smt) != 0) {
  311. continue;
  312. }
  313. if (asprintf(&nodename, "%s@%x", modelname, index) < 0) {
  314. fprintf(stderr, "Allocation failure\n");
  315. exit(1);
  316. }
  317. _FDT((fdt_begin_node(fdt, nodename)));
  318. free(nodename);
  319. _FDT((fdt_property_cell(fdt, "reg", index)));
  320. _FDT((fdt_property_string(fdt, "device_type", "cpu")));
  321. _FDT((fdt_property_cell(fdt, "cpu-version", env->spr[SPR_PVR])));
  322. _FDT((fdt_property_cell(fdt, "dcache-block-size",
  323. env->dcache_line_size)));
  324. _FDT((fdt_property_cell(fdt, "icache-block-size",
  325. env->icache_line_size)));
  326. _FDT((fdt_property_cell(fdt, "timebase-frequency", tbfreq)));
  327. _FDT((fdt_property_cell(fdt, "clock-frequency", cpufreq)));
  328. _FDT((fdt_property_cell(fdt, "ibm,slb-size", env->slb_nr)));
  329. _FDT((fdt_property(fdt, "ibm,pft-size",
  330. pft_size_prop, sizeof(pft_size_prop))));
  331. _FDT((fdt_property_string(fdt, "status", "okay")));
  332. _FDT((fdt_property(fdt, "64-bit", NULL, 0)));
  333. /* Build interrupt servers and gservers properties */
  334. for (i = 0; i < smp_threads; i++) {
  335. servers_prop[i] = cpu_to_be32(index + i);
  336. /* Hack, direct the group queues back to cpu 0 */
  337. gservers_prop[i*2] = cpu_to_be32(index + i);
  338. gservers_prop[i*2 + 1] = 0;
  339. }
  340. _FDT((fdt_property(fdt, "ibm,ppc-interrupt-server#s",
  341. servers_prop, sizeof(servers_prop))));
  342. _FDT((fdt_property(fdt, "ibm,ppc-interrupt-gserver#s",
  343. gservers_prop, sizeof(gservers_prop))));
  344. if (env->mmu_model & POWERPC_MMU_1TSEG) {
  345. _FDT((fdt_property(fdt, "ibm,processor-segment-sizes",
  346. segs, sizeof(segs))));
  347. }
  348. /* Advertise VMX/VSX (vector extensions) if available
  349. * 0 / no property == no vector extensions
  350. * 1 == VMX / Altivec available
  351. * 2 == VSX available */
  352. if (env->insns_flags & PPC_ALTIVEC) {
  353. uint32_t vmx = (env->insns_flags2 & PPC2_VSX) ? 2 : 1;
  354. _FDT((fdt_property_cell(fdt, "ibm,vmx", vmx)));
  355. }
  356. /* Advertise DFP (Decimal Floating Point) if available
  357. * 0 / no property == no DFP
  358. * 1 == DFP available */
  359. if (env->insns_flags2 & PPC2_DFP) {
  360. _FDT((fdt_property_cell(fdt, "ibm,dfp", 1)));
  361. }
  362. page_sizes_prop_size = create_page_sizes_prop(env, page_sizes_prop,
  363. sizeof(page_sizes_prop));
  364. if (page_sizes_prop_size) {
  365. _FDT((fdt_property(fdt, "ibm,segment-page-sizes",
  366. page_sizes_prop, page_sizes_prop_size)));
  367. }
  368. _FDT((fdt_end_node(fdt)));
  369. }
  370. g_free(modelname);
  371. _FDT((fdt_end_node(fdt)));
  372. /* RTAS */
  373. _FDT((fdt_begin_node(fdt, "rtas")));
  374. _FDT((fdt_property(fdt, "ibm,hypertas-functions", hypertas_prop,
  375. sizeof(hypertas_prop))));
  376. _FDT((fdt_property(fdt, "qemu,hypertas-functions", qemu_hypertas_prop,
  377. sizeof(qemu_hypertas_prop))));
  378. _FDT((fdt_property(fdt, "ibm,associativity-reference-points",
  379. refpoints, sizeof(refpoints))));
  380. _FDT((fdt_end_node(fdt)));
  381. /* interrupt controller */
  382. _FDT((fdt_begin_node(fdt, "interrupt-controller")));
  383. _FDT((fdt_property_string(fdt, "device_type",
  384. "PowerPC-External-Interrupt-Presentation")));
  385. _FDT((fdt_property_string(fdt, "compatible", "IBM,ppc-xicp")));
  386. _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0)));
  387. _FDT((fdt_property(fdt, "ibm,interrupt-server-ranges",
  388. interrupt_server_ranges_prop,
  389. sizeof(interrupt_server_ranges_prop))));
  390. _FDT((fdt_property_cell(fdt, "#interrupt-cells", 2)));
  391. _FDT((fdt_property_cell(fdt, "linux,phandle", PHANDLE_XICP)));
  392. _FDT((fdt_property_cell(fdt, "phandle", PHANDLE_XICP)));
  393. _FDT((fdt_end_node(fdt)));
  394. /* vdevice */
  395. _FDT((fdt_begin_node(fdt, "vdevice")));
  396. _FDT((fdt_property_string(fdt, "device_type", "vdevice")));
  397. _FDT((fdt_property_string(fdt, "compatible", "IBM,vdevice")));
  398. _FDT((fdt_property_cell(fdt, "#address-cells", 0x1)));
  399. _FDT((fdt_property_cell(fdt, "#size-cells", 0x0)));
  400. _FDT((fdt_property_cell(fdt, "#interrupt-cells", 0x2)));
  401. _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0)));
  402. _FDT((fdt_end_node(fdt)));
  403. _FDT((fdt_end_node(fdt))); /* close root node */
  404. _FDT((fdt_finish(fdt)));
  405. return fdt;
  406. }
  407. static void spapr_finalize_fdt(sPAPREnvironment *spapr,
  408. target_phys_addr_t fdt_addr,
  409. target_phys_addr_t rtas_addr,
  410. target_phys_addr_t rtas_size)
  411. {
  412. int ret;
  413. void *fdt;
  414. sPAPRPHBState *phb;
  415. fdt = g_malloc(FDT_MAX_SIZE);
  416. /* open out the base tree into a temp buffer for the final tweaks */
  417. _FDT((fdt_open_into(spapr->fdt_skel, fdt, FDT_MAX_SIZE)));
  418. ret = spapr_populate_vdevice(spapr->vio_bus, fdt);
  419. if (ret < 0) {
  420. fprintf(stderr, "couldn't setup vio devices in fdt\n");
  421. exit(1);
  422. }
  423. QLIST_FOREACH(phb, &spapr->phbs, list) {
  424. ret = spapr_populate_pci_dt(phb, PHANDLE_XICP, fdt);
  425. }
  426. if (ret < 0) {
  427. fprintf(stderr, "couldn't setup PCI devices in fdt\n");
  428. exit(1);
  429. }
  430. /* RTAS */
  431. ret = spapr_rtas_device_tree_setup(fdt, rtas_addr, rtas_size);
  432. if (ret < 0) {
  433. fprintf(stderr, "Couldn't set up RTAS device tree properties\n");
  434. }
  435. /* Advertise NUMA via ibm,associativity */
  436. if (nb_numa_nodes > 1) {
  437. ret = spapr_set_associativity(fdt, spapr);
  438. if (ret < 0) {
  439. fprintf(stderr, "Couldn't set up NUMA device tree properties\n");
  440. }
  441. }
  442. if (!spapr->has_graphics) {
  443. spapr_populate_chosen_stdout(fdt, spapr->vio_bus);
  444. }
  445. _FDT((fdt_pack(fdt)));
  446. if (fdt_totalsize(fdt) > FDT_MAX_SIZE) {
  447. hw_error("FDT too big ! 0x%x bytes (max is 0x%x)\n",
  448. fdt_totalsize(fdt), FDT_MAX_SIZE);
  449. exit(1);
  450. }
  451. cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt));
  452. g_free(fdt);
  453. }
  454. static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
  455. {
  456. return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR;
  457. }
  458. static void emulate_spapr_hypercall(CPUPPCState *env)
  459. {
  460. if (msr_pr) {
  461. hcall_dprintf("Hypercall made with MSR[PR]=1\n");
  462. env->gpr[3] = H_PRIVILEGE;
  463. } else {
  464. env->gpr[3] = spapr_hypercall(env, env->gpr[3], &env->gpr[4]);
  465. }
  466. }
  467. static void spapr_reset(void *opaque)
  468. {
  469. sPAPREnvironment *spapr = (sPAPREnvironment *)opaque;
  470. /* flush out the hash table */
  471. memset(spapr->htab, 0, spapr->htab_size);
  472. /* Load the fdt */
  473. spapr_finalize_fdt(spapr, spapr->fdt_addr, spapr->rtas_addr,
  474. spapr->rtas_size);
  475. /* Set up the entry state */
  476. first_cpu->gpr[3] = spapr->fdt_addr;
  477. first_cpu->gpr[5] = 0;
  478. first_cpu->halted = 0;
  479. first_cpu->nip = spapr->entry_point;
  480. }
  481. static void spapr_cpu_reset(void *opaque)
  482. {
  483. PowerPCCPU *cpu = opaque;
  484. cpu_reset(CPU(cpu));
  485. }
  486. /* Returns whether we want to use VGA or not */
  487. static int spapr_vga_init(PCIBus *pci_bus)
  488. {
  489. switch (vga_interface_type) {
  490. case VGA_STD:
  491. pci_vga_init(pci_bus);
  492. return 1;
  493. case VGA_NONE:
  494. return 0;
  495. default:
  496. fprintf(stderr, "This vga model is not supported,"
  497. "currently it only supports -vga std\n");
  498. exit(0);
  499. break;
  500. }
  501. }
  502. /* pSeries LPAR / sPAPR hardware init */
  503. static void ppc_spapr_init(ram_addr_t ram_size,
  504. const char *boot_device,
  505. const char *kernel_filename,
  506. const char *kernel_cmdline,
  507. const char *initrd_filename,
  508. const char *cpu_model)
  509. {
  510. PowerPCCPU *cpu;
  511. CPUPPCState *env;
  512. PCIHostState *phb;
  513. int i;
  514. MemoryRegion *sysmem = get_system_memory();
  515. MemoryRegion *ram = g_new(MemoryRegion, 1);
  516. target_phys_addr_t rma_alloc_size, rma_size;
  517. uint32_t initrd_base = 0;
  518. long kernel_size = 0, initrd_size = 0;
  519. long load_limit, rtas_limit, fw_size;
  520. long pteg_shift = 17;
  521. char *filename;
  522. msi_supported = true;
  523. spapr = g_malloc0(sizeof(*spapr));
  524. QLIST_INIT(&spapr->phbs);
  525. cpu_ppc_hypercall = emulate_spapr_hypercall;
  526. /* Allocate RMA if necessary */
  527. rma_alloc_size = kvmppc_alloc_rma("ppc_spapr.rma", sysmem);
  528. if (rma_alloc_size == -1) {
  529. hw_error("qemu: Unable to create RMA\n");
  530. exit(1);
  531. }
  532. if (rma_alloc_size && (rma_alloc_size < ram_size)) {
  533. rma_size = rma_alloc_size;
  534. } else {
  535. rma_size = ram_size;
  536. }
  537. /* We place the device tree and RTAS just below either the top of the RMA,
  538. * or just below 2GB, whichever is lowere, so that it can be
  539. * processed with 32-bit real mode code if necessary */
  540. rtas_limit = MIN(rma_size, 0x80000000);
  541. spapr->rtas_addr = rtas_limit - RTAS_MAX_SIZE;
  542. spapr->fdt_addr = spapr->rtas_addr - FDT_MAX_SIZE;
  543. load_limit = spapr->fdt_addr - FW_OVERHEAD;
  544. /* init CPUs */
  545. if (cpu_model == NULL) {
  546. cpu_model = kvm_enabled() ? "host" : "POWER7";
  547. }
  548. for (i = 0; i < smp_cpus; i++) {
  549. cpu = cpu_ppc_init(cpu_model);
  550. if (cpu == NULL) {
  551. fprintf(stderr, "Unable to find PowerPC CPU definition\n");
  552. exit(1);
  553. }
  554. env = &cpu->env;
  555. /* Set time-base frequency to 512 MHz */
  556. cpu_ppc_tb_init(env, TIMEBASE_FREQ);
  557. qemu_register_reset(spapr_cpu_reset, cpu);
  558. env->hreset_vector = 0x60;
  559. env->hreset_excp_prefix = 0;
  560. env->gpr[3] = env->cpu_index;
  561. }
  562. /* allocate RAM */
  563. spapr->ram_limit = ram_size;
  564. if (spapr->ram_limit > rma_alloc_size) {
  565. ram_addr_t nonrma_base = rma_alloc_size;
  566. ram_addr_t nonrma_size = spapr->ram_limit - rma_alloc_size;
  567. memory_region_init_ram(ram, "ppc_spapr.ram", nonrma_size);
  568. vmstate_register_ram_global(ram);
  569. memory_region_add_subregion(sysmem, nonrma_base, ram);
  570. }
  571. /* allocate hash page table. For now we always make this 16mb,
  572. * later we should probably make it scale to the size of guest
  573. * RAM */
  574. spapr->htab_size = 1ULL << (pteg_shift + 7);
  575. spapr->htab = qemu_memalign(spapr->htab_size, spapr->htab_size);
  576. for (env = first_cpu; env != NULL; env = env->next_cpu) {
  577. env->external_htab = spapr->htab;
  578. env->htab_base = -1;
  579. env->htab_mask = spapr->htab_size - 1;
  580. /* Tell KVM that we're in PAPR mode */
  581. env->spr[SPR_SDR1] = (unsigned long)spapr->htab |
  582. ((pteg_shift + 7) - 18);
  583. env->spr[SPR_HIOR] = 0;
  584. if (kvm_enabled()) {
  585. kvmppc_set_papr(env);
  586. }
  587. }
  588. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "spapr-rtas.bin");
  589. spapr->rtas_size = load_image_targphys(filename, spapr->rtas_addr,
  590. rtas_limit - spapr->rtas_addr);
  591. if (spapr->rtas_size < 0) {
  592. hw_error("qemu: could not load LPAR rtas '%s'\n", filename);
  593. exit(1);
  594. }
  595. if (spapr->rtas_size > RTAS_MAX_SIZE) {
  596. hw_error("RTAS too big ! 0x%lx bytes (max is 0x%x)\n",
  597. spapr->rtas_size, RTAS_MAX_SIZE);
  598. exit(1);
  599. }
  600. g_free(filename);
  601. /* Set up Interrupt Controller */
  602. spapr->icp = xics_system_init(XICS_IRQS);
  603. spapr->next_irq = 16;
  604. /* Set up IOMMU */
  605. spapr_iommu_init();
  606. /* Set up VIO bus */
  607. spapr->vio_bus = spapr_vio_bus_init();
  608. for (i = 0; i < MAX_SERIAL_PORTS; i++) {
  609. if (serial_hds[i]) {
  610. spapr_vty_create(spapr->vio_bus, serial_hds[i]);
  611. }
  612. }
  613. /* Set up PCI */
  614. spapr_pci_rtas_init();
  615. spapr_create_phb(spapr, "pci", SPAPR_PCI_BUID,
  616. SPAPR_PCI_MEM_WIN_ADDR,
  617. SPAPR_PCI_MEM_WIN_SIZE,
  618. SPAPR_PCI_IO_WIN_ADDR,
  619. SPAPR_PCI_MSI_WIN_ADDR);
  620. phb = PCI_HOST_BRIDGE(QLIST_FIRST(&spapr->phbs));
  621. for (i = 0; i < nb_nics; i++) {
  622. NICInfo *nd = &nd_table[i];
  623. if (!nd->model) {
  624. nd->model = g_strdup("ibmveth");
  625. }
  626. if (strcmp(nd->model, "ibmveth") == 0) {
  627. spapr_vlan_create(spapr->vio_bus, nd);
  628. } else {
  629. pci_nic_init_nofail(&nd_table[i], nd->model, NULL);
  630. }
  631. }
  632. for (i = 0; i <= drive_get_max_bus(IF_SCSI); i++) {
  633. spapr_vscsi_create(spapr->vio_bus);
  634. }
  635. /* Graphics */
  636. if (spapr_vga_init(phb->bus)) {
  637. spapr->has_graphics = true;
  638. }
  639. if (usb_enabled) {
  640. pci_create_simple(phb->bus, -1, "pci-ohci");
  641. if (spapr->has_graphics) {
  642. usbdevice_create("keyboard");
  643. usbdevice_create("mouse");
  644. }
  645. }
  646. if (rma_size < (MIN_RMA_SLOF << 20)) {
  647. fprintf(stderr, "qemu: pSeries SLOF firmware requires >= "
  648. "%ldM guest RMA (Real Mode Area memory)\n", MIN_RMA_SLOF);
  649. exit(1);
  650. }
  651. if (kernel_filename) {
  652. uint64_t lowaddr = 0;
  653. kernel_size = load_elf(kernel_filename, translate_kernel_address, NULL,
  654. NULL, &lowaddr, NULL, 1, ELF_MACHINE, 0);
  655. if (kernel_size < 0) {
  656. kernel_size = load_image_targphys(kernel_filename,
  657. KERNEL_LOAD_ADDR,
  658. load_limit - KERNEL_LOAD_ADDR);
  659. }
  660. if (kernel_size < 0) {
  661. fprintf(stderr, "qemu: could not load kernel '%s'\n",
  662. kernel_filename);
  663. exit(1);
  664. }
  665. /* load initrd */
  666. if (initrd_filename) {
  667. /* Try to locate the initrd in the gap between the kernel
  668. * and the firmware. Add a bit of space just in case
  669. */
  670. initrd_base = (KERNEL_LOAD_ADDR + kernel_size + 0x1ffff) & ~0xffff;
  671. initrd_size = load_image_targphys(initrd_filename, initrd_base,
  672. load_limit - initrd_base);
  673. if (initrd_size < 0) {
  674. fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
  675. initrd_filename);
  676. exit(1);
  677. }
  678. } else {
  679. initrd_base = 0;
  680. initrd_size = 0;
  681. }
  682. }
  683. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, FW_FILE_NAME);
  684. fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE);
  685. if (fw_size < 0) {
  686. hw_error("qemu: could not load LPAR rtas '%s'\n", filename);
  687. exit(1);
  688. }
  689. g_free(filename);
  690. spapr->entry_point = 0x100;
  691. /* SLOF will startup the secondary CPUs using RTAS */
  692. for (env = first_cpu; env != NULL; env = env->next_cpu) {
  693. env->halted = 1;
  694. }
  695. /* Prepare the device tree */
  696. spapr->fdt_skel = spapr_create_fdt_skel(cpu_model, rma_size,
  697. initrd_base, initrd_size,
  698. kernel_size,
  699. boot_device, kernel_cmdline,
  700. pteg_shift + 7);
  701. assert(spapr->fdt_skel != NULL);
  702. qemu_register_reset(spapr_reset, spapr);
  703. }
  704. static QEMUMachine spapr_machine = {
  705. .name = "pseries",
  706. .desc = "pSeries Logical Partition (PAPR compliant)",
  707. .init = ppc_spapr_init,
  708. .max_cpus = MAX_CPUS,
  709. .no_parallel = 1,
  710. .use_scsi = 1,
  711. };
  712. static void spapr_machine_init(void)
  713. {
  714. qemu_register_machine(&spapr_machine);
  715. }
  716. machine_init(spapr_machine_init);