spapr.c 30 KB

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