malta.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460
  1. /*
  2. * QEMU Malta board support
  3. *
  4. * Copyright (c) 2006 Aurelien Jarno
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qemu/units.h"
  26. #include "qemu/bitops.h"
  27. #include "qemu/datadir.h"
  28. #include "hw/clock.h"
  29. #include "hw/southbridge/piix.h"
  30. #include "hw/isa/superio.h"
  31. #include "hw/char/serial.h"
  32. #include "net/net.h"
  33. #include "hw/boards.h"
  34. #include "hw/i2c/smbus_eeprom.h"
  35. #include "hw/block/flash.h"
  36. #include "hw/mips/mips.h"
  37. #include "hw/mips/cpudevs.h"
  38. #include "hw/pci/pci.h"
  39. #include "qemu/log.h"
  40. #include "hw/mips/bios.h"
  41. #include "hw/ide.h"
  42. #include "hw/irq.h"
  43. #include "hw/loader.h"
  44. #include "elf.h"
  45. #include "qom/object.h"
  46. #include "hw/sysbus.h" /* SysBusDevice */
  47. #include "qemu/host-utils.h"
  48. #include "sysemu/qtest.h"
  49. #include "sysemu/reset.h"
  50. #include "sysemu/runstate.h"
  51. #include "qapi/error.h"
  52. #include "qemu/error-report.h"
  53. #include "hw/misc/empty_slot.h"
  54. #include "sysemu/kvm.h"
  55. #include "semihosting/semihost.h"
  56. #include "hw/mips/cps.h"
  57. #include "hw/qdev-clock.h"
  58. #define ENVP_PADDR 0x2000
  59. #define ENVP_VADDR cpu_mips_phys_to_kseg0(NULL, ENVP_PADDR)
  60. #define ENVP_NB_ENTRIES 16
  61. #define ENVP_ENTRY_SIZE 256
  62. /* Hardware addresses */
  63. #define FLASH_ADDRESS 0x1e000000ULL
  64. #define FPGA_ADDRESS 0x1f000000ULL
  65. #define RESET_ADDRESS 0x1fc00000ULL
  66. #define FLASH_SIZE 0x400000
  67. #define MAX_IDE_BUS 2
  68. typedef struct {
  69. MemoryRegion iomem;
  70. MemoryRegion iomem_lo; /* 0 - 0x900 */
  71. MemoryRegion iomem_hi; /* 0xa00 - 0x100000 */
  72. uint32_t leds;
  73. uint32_t brk;
  74. uint32_t gpout;
  75. uint32_t i2cin;
  76. uint32_t i2coe;
  77. uint32_t i2cout;
  78. uint32_t i2csel;
  79. CharBackend display;
  80. char display_text[9];
  81. SerialMM *uart;
  82. bool display_inited;
  83. } MaltaFPGAState;
  84. #define TYPE_MIPS_MALTA "mips-malta"
  85. OBJECT_DECLARE_SIMPLE_TYPE(MaltaState, MIPS_MALTA)
  86. struct MaltaState {
  87. SysBusDevice parent_obj;
  88. Clock *cpuclk;
  89. MIPSCPSState cps;
  90. };
  91. static struct _loaderparams {
  92. int ram_size, ram_low_size;
  93. const char *kernel_filename;
  94. const char *kernel_cmdline;
  95. const char *initrd_filename;
  96. } loaderparams;
  97. /* Malta FPGA */
  98. static void malta_fpga_update_display(void *opaque)
  99. {
  100. char leds_text[9];
  101. int i;
  102. MaltaFPGAState *s = opaque;
  103. for (i = 7 ; i >= 0 ; i--) {
  104. if (s->leds & (1 << i)) {
  105. leds_text[i] = '#';
  106. } else {
  107. leds_text[i] = ' ';
  108. }
  109. }
  110. leds_text[8] = '\0';
  111. qemu_chr_fe_printf(&s->display, "\e[H\n\n|\e[32m%-8.8s\e[00m|\r\n",
  112. leds_text);
  113. qemu_chr_fe_printf(&s->display, "\n\n\n\n|\e[31m%-8.8s\e[00m|",
  114. s->display_text);
  115. }
  116. /*
  117. * EEPROM 24C01 / 24C02 emulation.
  118. *
  119. * Emulation for serial EEPROMs:
  120. * 24C01 - 1024 bit (128 x 8)
  121. * 24C02 - 2048 bit (256 x 8)
  122. *
  123. * Typical device names include Microchip 24C02SC or SGS Thomson ST24C02.
  124. */
  125. #if defined(DEBUG)
  126. # define logout(fmt, ...) \
  127. fprintf(stderr, "MALTA\t%-24s" fmt, __func__, ## __VA_ARGS__)
  128. #else
  129. # define logout(fmt, ...) ((void)0)
  130. #endif
  131. struct _eeprom24c0x_t {
  132. uint8_t tick;
  133. uint8_t address;
  134. uint8_t command;
  135. uint8_t ack;
  136. uint8_t scl;
  137. uint8_t sda;
  138. uint8_t data;
  139. /* uint16_t size; */
  140. uint8_t contents[256];
  141. };
  142. typedef struct _eeprom24c0x_t eeprom24c0x_t;
  143. static eeprom24c0x_t spd_eeprom = {
  144. .contents = {
  145. /* 00000000: */
  146. 0x80, 0x08, 0xFF, 0x0D, 0x0A, 0xFF, 0x40, 0x00,
  147. /* 00000008: */
  148. 0x01, 0x75, 0x54, 0x00, 0x82, 0x08, 0x00, 0x01,
  149. /* 00000010: */
  150. 0x8F, 0x04, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00,
  151. /* 00000018: */
  152. 0x00, 0x00, 0x00, 0x14, 0x0F, 0x14, 0x2D, 0xFF,
  153. /* 00000020: */
  154. 0x15, 0x08, 0x15, 0x08, 0x00, 0x00, 0x00, 0x00,
  155. /* 00000028: */
  156. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  157. /* 00000030: */
  158. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  159. /* 00000038: */
  160. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0xD0,
  161. /* 00000040: */
  162. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  163. /* 00000048: */
  164. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  165. /* 00000050: */
  166. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  167. /* 00000058: */
  168. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  169. /* 00000060: */
  170. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  171. /* 00000068: */
  172. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  173. /* 00000070: */
  174. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  175. /* 00000078: */
  176. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0xF4,
  177. },
  178. };
  179. static void generate_eeprom_spd(uint8_t *eeprom, ram_addr_t ram_size)
  180. {
  181. enum { SDR = 0x4, DDR2 = 0x8 } type;
  182. uint8_t *spd = spd_eeprom.contents;
  183. uint8_t nbanks = 0;
  184. uint16_t density = 0;
  185. int i;
  186. /* work in terms of MB */
  187. ram_size /= MiB;
  188. while ((ram_size >= 4) && (nbanks <= 2)) {
  189. int sz_log2 = MIN(31 - clz32(ram_size), 14);
  190. nbanks++;
  191. density |= 1 << (sz_log2 - 2);
  192. ram_size -= 1 << sz_log2;
  193. }
  194. /* split to 2 banks if possible */
  195. if ((nbanks == 1) && (density > 1)) {
  196. nbanks++;
  197. density >>= 1;
  198. }
  199. if (density & 0xff00) {
  200. density = (density & 0xe0) | ((density >> 8) & 0x1f);
  201. type = DDR2;
  202. } else if (!(density & 0x1f)) {
  203. type = DDR2;
  204. } else {
  205. type = SDR;
  206. }
  207. if (ram_size) {
  208. warn_report("SPD cannot represent final " RAM_ADDR_FMT "MB"
  209. " of SDRAM", ram_size);
  210. }
  211. /* fill in SPD memory information */
  212. spd[2] = type;
  213. spd[5] = nbanks;
  214. spd[31] = density;
  215. /* checksum */
  216. spd[63] = 0;
  217. for (i = 0; i < 63; i++) {
  218. spd[63] += spd[i];
  219. }
  220. /* copy for SMBUS */
  221. memcpy(eeprom, spd, sizeof(spd_eeprom.contents));
  222. }
  223. static void generate_eeprom_serial(uint8_t *eeprom)
  224. {
  225. int i, pos = 0;
  226. uint8_t mac[6] = { 0x00 };
  227. uint8_t sn[5] = { 0x01, 0x23, 0x45, 0x67, 0x89 };
  228. /* version */
  229. eeprom[pos++] = 0x01;
  230. /* count */
  231. eeprom[pos++] = 0x02;
  232. /* MAC address */
  233. eeprom[pos++] = 0x01; /* MAC */
  234. eeprom[pos++] = 0x06; /* length */
  235. memcpy(&eeprom[pos], mac, sizeof(mac));
  236. pos += sizeof(mac);
  237. /* serial number */
  238. eeprom[pos++] = 0x02; /* serial */
  239. eeprom[pos++] = 0x05; /* length */
  240. memcpy(&eeprom[pos], sn, sizeof(sn));
  241. pos += sizeof(sn);
  242. /* checksum */
  243. eeprom[pos] = 0;
  244. for (i = 0; i < pos; i++) {
  245. eeprom[pos] += eeprom[i];
  246. }
  247. }
  248. static uint8_t eeprom24c0x_read(eeprom24c0x_t *eeprom)
  249. {
  250. logout("%u: scl = %u, sda = %u, data = 0x%02x\n",
  251. eeprom->tick, eeprom->scl, eeprom->sda, eeprom->data);
  252. return eeprom->sda;
  253. }
  254. static void eeprom24c0x_write(eeprom24c0x_t *eeprom, int scl, int sda)
  255. {
  256. if (eeprom->scl && scl && (eeprom->sda != sda)) {
  257. logout("%u: scl = %u->%u, sda = %u->%u i2c %s\n",
  258. eeprom->tick, eeprom->scl, scl, eeprom->sda, sda,
  259. sda ? "stop" : "start");
  260. if (!sda) {
  261. eeprom->tick = 1;
  262. eeprom->command = 0;
  263. }
  264. } else if (eeprom->tick == 0 && !eeprom->ack) {
  265. /* Waiting for start. */
  266. logout("%u: scl = %u->%u, sda = %u->%u wait for i2c start\n",
  267. eeprom->tick, eeprom->scl, scl, eeprom->sda, sda);
  268. } else if (!eeprom->scl && scl) {
  269. logout("%u: scl = %u->%u, sda = %u->%u trigger bit\n",
  270. eeprom->tick, eeprom->scl, scl, eeprom->sda, sda);
  271. if (eeprom->ack) {
  272. logout("\ti2c ack bit = 0\n");
  273. sda = 0;
  274. eeprom->ack = 0;
  275. } else if (eeprom->sda == sda) {
  276. uint8_t bit = (sda != 0);
  277. logout("\ti2c bit = %d\n", bit);
  278. if (eeprom->tick < 9) {
  279. eeprom->command <<= 1;
  280. eeprom->command += bit;
  281. eeprom->tick++;
  282. if (eeprom->tick == 9) {
  283. logout("\tcommand 0x%04x, %s\n", eeprom->command,
  284. bit ? "read" : "write");
  285. eeprom->ack = 1;
  286. }
  287. } else if (eeprom->tick < 17) {
  288. if (eeprom->command & 1) {
  289. sda = ((eeprom->data & 0x80) != 0);
  290. }
  291. eeprom->address <<= 1;
  292. eeprom->address += bit;
  293. eeprom->tick++;
  294. eeprom->data <<= 1;
  295. if (eeprom->tick == 17) {
  296. eeprom->data = eeprom->contents[eeprom->address];
  297. logout("\taddress 0x%04x, data 0x%02x\n",
  298. eeprom->address, eeprom->data);
  299. eeprom->ack = 1;
  300. eeprom->tick = 0;
  301. }
  302. } else if (eeprom->tick >= 17) {
  303. sda = 0;
  304. }
  305. } else {
  306. logout("\tsda changed with raising scl\n");
  307. }
  308. } else {
  309. logout("%u: scl = %u->%u, sda = %u->%u\n", eeprom->tick, eeprom->scl,
  310. scl, eeprom->sda, sda);
  311. }
  312. eeprom->scl = scl;
  313. eeprom->sda = sda;
  314. }
  315. static uint64_t malta_fpga_read(void *opaque, hwaddr addr,
  316. unsigned size)
  317. {
  318. MaltaFPGAState *s = opaque;
  319. uint32_t val = 0;
  320. uint32_t saddr;
  321. saddr = (addr & 0xfffff);
  322. switch (saddr) {
  323. /* SWITCH Register */
  324. case 0x00200:
  325. val = 0x00000000;
  326. break;
  327. /* STATUS Register */
  328. case 0x00208:
  329. #if TARGET_BIG_ENDIAN
  330. val = 0x00000012;
  331. #else
  332. val = 0x00000010;
  333. #endif
  334. break;
  335. /* JMPRS Register */
  336. case 0x00210:
  337. val = 0x00;
  338. break;
  339. /* LEDBAR Register */
  340. case 0x00408:
  341. val = s->leds;
  342. break;
  343. /* BRKRES Register */
  344. case 0x00508:
  345. val = s->brk;
  346. break;
  347. /* UART Registers are handled directly by the serial device */
  348. /* GPOUT Register */
  349. case 0x00a00:
  350. val = s->gpout;
  351. break;
  352. /* XXX: implement a real I2C controller */
  353. /* GPINP Register */
  354. case 0x00a08:
  355. /* IN = OUT until a real I2C control is implemented */
  356. if (s->i2csel) {
  357. val = s->i2cout;
  358. } else {
  359. val = 0x00;
  360. }
  361. break;
  362. /* I2CINP Register */
  363. case 0x00b00:
  364. val = ((s->i2cin & ~1) | eeprom24c0x_read(&spd_eeprom));
  365. break;
  366. /* I2COE Register */
  367. case 0x00b08:
  368. val = s->i2coe;
  369. break;
  370. /* I2COUT Register */
  371. case 0x00b10:
  372. val = s->i2cout;
  373. break;
  374. /* I2CSEL Register */
  375. case 0x00b18:
  376. val = s->i2csel;
  377. break;
  378. default:
  379. qemu_log_mask(LOG_GUEST_ERROR,
  380. "malta_fpga_read: Bad register addr 0x%"HWADDR_PRIX"\n",
  381. addr);
  382. break;
  383. }
  384. return val;
  385. }
  386. static void malta_fpga_write(void *opaque, hwaddr addr,
  387. uint64_t val, unsigned size)
  388. {
  389. MaltaFPGAState *s = opaque;
  390. uint32_t saddr;
  391. saddr = (addr & 0xfffff);
  392. switch (saddr) {
  393. /* SWITCH Register */
  394. case 0x00200:
  395. break;
  396. /* JMPRS Register */
  397. case 0x00210:
  398. break;
  399. /* LEDBAR Register */
  400. case 0x00408:
  401. s->leds = val & 0xff;
  402. malta_fpga_update_display(s);
  403. break;
  404. /* ASCIIWORD Register */
  405. case 0x00410:
  406. snprintf(s->display_text, 9, "%08X", (uint32_t)val);
  407. malta_fpga_update_display(s);
  408. break;
  409. /* ASCIIPOS0 to ASCIIPOS7 Registers */
  410. case 0x00418:
  411. case 0x00420:
  412. case 0x00428:
  413. case 0x00430:
  414. case 0x00438:
  415. case 0x00440:
  416. case 0x00448:
  417. case 0x00450:
  418. s->display_text[(saddr - 0x00418) >> 3] = (char) val;
  419. malta_fpga_update_display(s);
  420. break;
  421. /* SOFTRES Register */
  422. case 0x00500:
  423. if (val == 0x42) {
  424. qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
  425. }
  426. break;
  427. /* BRKRES Register */
  428. case 0x00508:
  429. s->brk = val & 0xff;
  430. break;
  431. /* UART Registers are handled directly by the serial device */
  432. /* GPOUT Register */
  433. case 0x00a00:
  434. s->gpout = val & 0xff;
  435. break;
  436. /* I2COE Register */
  437. case 0x00b08:
  438. s->i2coe = val & 0x03;
  439. break;
  440. /* I2COUT Register */
  441. case 0x00b10:
  442. eeprom24c0x_write(&spd_eeprom, val & 0x02, val & 0x01);
  443. s->i2cout = val;
  444. break;
  445. /* I2CSEL Register */
  446. case 0x00b18:
  447. s->i2csel = val & 0x01;
  448. break;
  449. default:
  450. qemu_log_mask(LOG_GUEST_ERROR,
  451. "malta_fpga_write: Bad register addr 0x%"HWADDR_PRIX"\n",
  452. addr);
  453. break;
  454. }
  455. }
  456. static const MemoryRegionOps malta_fpga_ops = {
  457. .read = malta_fpga_read,
  458. .write = malta_fpga_write,
  459. .endianness = DEVICE_NATIVE_ENDIAN,
  460. };
  461. static void malta_fpga_reset(void *opaque)
  462. {
  463. MaltaFPGAState *s = opaque;
  464. s->leds = 0x00;
  465. s->brk = 0x0a;
  466. s->gpout = 0x00;
  467. s->i2cin = 0x3;
  468. s->i2coe = 0x0;
  469. s->i2cout = 0x3;
  470. s->i2csel = 0x1;
  471. s->display_text[8] = '\0';
  472. snprintf(s->display_text, 9, " ");
  473. }
  474. static void malta_fgpa_display_event(void *opaque, QEMUChrEvent event)
  475. {
  476. MaltaFPGAState *s = opaque;
  477. if (event == CHR_EVENT_OPENED && !s->display_inited) {
  478. qemu_chr_fe_printf(&s->display, "\e[HMalta LEDBAR\r\n");
  479. qemu_chr_fe_printf(&s->display, "+--------+\r\n");
  480. qemu_chr_fe_printf(&s->display, "+ +\r\n");
  481. qemu_chr_fe_printf(&s->display, "+--------+\r\n");
  482. qemu_chr_fe_printf(&s->display, "\n");
  483. qemu_chr_fe_printf(&s->display, "Malta ASCII\r\n");
  484. qemu_chr_fe_printf(&s->display, "+--------+\r\n");
  485. qemu_chr_fe_printf(&s->display, "+ +\r\n");
  486. qemu_chr_fe_printf(&s->display, "+--------+\r\n");
  487. s->display_inited = true;
  488. }
  489. }
  490. static MaltaFPGAState *malta_fpga_init(MemoryRegion *address_space,
  491. hwaddr base, qemu_irq uart_irq, Chardev *uart_chr)
  492. {
  493. MaltaFPGAState *s;
  494. Chardev *chr;
  495. s = g_new0(MaltaFPGAState, 1);
  496. memory_region_init_io(&s->iomem, NULL, &malta_fpga_ops, s,
  497. "malta-fpga", 0x100000);
  498. memory_region_init_alias(&s->iomem_lo, NULL, "malta-fpga",
  499. &s->iomem, 0, 0x900);
  500. memory_region_init_alias(&s->iomem_hi, NULL, "malta-fpga",
  501. &s->iomem, 0xa00, 0x100000 - 0xa00);
  502. memory_region_add_subregion(address_space, base, &s->iomem_lo);
  503. memory_region_add_subregion(address_space, base + 0xa00, &s->iomem_hi);
  504. chr = qemu_chr_new("fpga", "vc:320x200", NULL);
  505. qemu_chr_fe_init(&s->display, chr, NULL);
  506. qemu_chr_fe_set_handlers(&s->display, NULL, NULL,
  507. malta_fgpa_display_event, NULL, s, NULL, true);
  508. s->uart = serial_mm_init(address_space, base + 0x900, 3, uart_irq,
  509. 230400, uart_chr, DEVICE_NATIVE_ENDIAN);
  510. malta_fpga_reset(s);
  511. qemu_register_reset(malta_fpga_reset, s);
  512. return s;
  513. }
  514. /* Network support */
  515. static void network_init(PCIBus *pci_bus)
  516. {
  517. int i;
  518. for (i = 0; i < nb_nics; i++) {
  519. NICInfo *nd = &nd_table[i];
  520. const char *default_devaddr = NULL;
  521. if (i == 0 && (!nd->model || strcmp(nd->model, "pcnet") == 0))
  522. /* The malta board has a PCNet card using PCI SLOT 11 */
  523. default_devaddr = "0b";
  524. pci_nic_init_nofail(nd, pci_bus, "pcnet", default_devaddr);
  525. }
  526. }
  527. static void write_bootloader_nanomips(uint8_t *base, uint64_t run_addr,
  528. uint64_t kernel_entry)
  529. {
  530. uint16_t *p;
  531. /* Small bootloader */
  532. p = (uint16_t *)base;
  533. #define NM_HI1(VAL) (((VAL) >> 16) & 0x1f)
  534. #define NM_HI2(VAL) \
  535. (((VAL) & 0xf000) | (((VAL) >> 19) & 0xffc) | (((VAL) >> 31) & 0x1))
  536. #define NM_LO(VAL) ((VAL) & 0xfff)
  537. stw_p(p++, 0x2800); stw_p(p++, 0x001c);
  538. /* bc to_here */
  539. stw_p(p++, 0x8000); stw_p(p++, 0xc000);
  540. /* nop */
  541. stw_p(p++, 0x8000); stw_p(p++, 0xc000);
  542. /* nop */
  543. stw_p(p++, 0x8000); stw_p(p++, 0xc000);
  544. /* nop */
  545. stw_p(p++, 0x8000); stw_p(p++, 0xc000);
  546. /* nop */
  547. stw_p(p++, 0x8000); stw_p(p++, 0xc000);
  548. /* nop */
  549. stw_p(p++, 0x8000); stw_p(p++, 0xc000);
  550. /* nop */
  551. stw_p(p++, 0x8000); stw_p(p++, 0xc000);
  552. /* nop */
  553. /* to_here: */
  554. if (semihosting_get_argc()) {
  555. /* Preserve a0 content as arguments have been passed */
  556. stw_p(p++, 0x8000); stw_p(p++, 0xc000);
  557. /* nop */
  558. } else {
  559. stw_p(p++, 0x0080); stw_p(p++, 0x0002);
  560. /* li a0,2 */
  561. }
  562. stw_p(p++, 0xe3a0 | NM_HI1(ENVP_VADDR - 64));
  563. stw_p(p++, NM_HI2(ENVP_VADDR - 64));
  564. /* lui sp,%hi(ENVP_VADDR - 64) */
  565. stw_p(p++, 0x83bd); stw_p(p++, NM_LO(ENVP_VADDR - 64));
  566. /* ori sp,sp,%lo(ENVP_VADDR - 64) */
  567. stw_p(p++, 0xe0a0 | NM_HI1(ENVP_VADDR));
  568. stw_p(p++, NM_HI2(ENVP_VADDR));
  569. /* lui a1,%hi(ENVP_VADDR) */
  570. stw_p(p++, 0x80a5); stw_p(p++, NM_LO(ENVP_VADDR));
  571. /* ori a1,a1,%lo(ENVP_VADDR) */
  572. stw_p(p++, 0xe0c0 | NM_HI1(ENVP_VADDR + 8));
  573. stw_p(p++, NM_HI2(ENVP_VADDR + 8));
  574. /* lui a2,%hi(ENVP_VADDR + 8) */
  575. stw_p(p++, 0x80c6); stw_p(p++, NM_LO(ENVP_VADDR + 8));
  576. /* ori a2,a2,%lo(ENVP_VADDR + 8) */
  577. stw_p(p++, 0xe0e0 | NM_HI1(loaderparams.ram_low_size));
  578. stw_p(p++, NM_HI2(loaderparams.ram_low_size));
  579. /* lui a3,%hi(loaderparams.ram_low_size) */
  580. stw_p(p++, 0x80e7); stw_p(p++, NM_LO(loaderparams.ram_low_size));
  581. /* ori a3,a3,%lo(loaderparams.ram_low_size) */
  582. /*
  583. * Load BAR registers as done by YAMON:
  584. *
  585. * - set up PCI0 I/O BARs from 0x18000000 to 0x181fffff
  586. * - set up PCI0 MEM0 at 0x10000000, size 0x8000000
  587. * - set up PCI0 MEM1 at 0x18200000, size 0xbe00000
  588. *
  589. */
  590. stw_p(p++, 0xe040); stw_p(p++, 0x0681);
  591. /* lui t1, %hi(0xb4000000) */
  592. #if TARGET_BIG_ENDIAN
  593. stw_p(p++, 0xe020); stw_p(p++, 0x0be1);
  594. /* lui t0, %hi(0xdf000000) */
  595. /* 0x68 corresponds to GT_ISD (from hw/mips/gt64xxx_pci.c) */
  596. stw_p(p++, 0x8422); stw_p(p++, 0x9068);
  597. /* sw t0, 0x68(t1) */
  598. stw_p(p++, 0xe040); stw_p(p++, 0x077d);
  599. /* lui t1, %hi(0xbbe00000) */
  600. stw_p(p++, 0xe020); stw_p(p++, 0x0801);
  601. /* lui t0, %hi(0xc0000000) */
  602. /* 0x48 corresponds to GT_PCI0IOLD */
  603. stw_p(p++, 0x8422); stw_p(p++, 0x9048);
  604. /* sw t0, 0x48(t1) */
  605. stw_p(p++, 0xe020); stw_p(p++, 0x0800);
  606. /* lui t0, %hi(0x40000000) */
  607. /* 0x50 corresponds to GT_PCI0IOHD */
  608. stw_p(p++, 0x8422); stw_p(p++, 0x9050);
  609. /* sw t0, 0x50(t1) */
  610. stw_p(p++, 0xe020); stw_p(p++, 0x0001);
  611. /* lui t0, %hi(0x80000000) */
  612. /* 0x58 corresponds to GT_PCI0M0LD */
  613. stw_p(p++, 0x8422); stw_p(p++, 0x9058);
  614. /* sw t0, 0x58(t1) */
  615. stw_p(p++, 0xe020); stw_p(p++, 0x07e0);
  616. /* lui t0, %hi(0x3f000000) */
  617. /* 0x60 corresponds to GT_PCI0M0HD */
  618. stw_p(p++, 0x8422); stw_p(p++, 0x9060);
  619. /* sw t0, 0x60(t1) */
  620. stw_p(p++, 0xe020); stw_p(p++, 0x0821);
  621. /* lui t0, %hi(0xc1000000) */
  622. /* 0x80 corresponds to GT_PCI0M1LD */
  623. stw_p(p++, 0x8422); stw_p(p++, 0x9080);
  624. /* sw t0, 0x80(t1) */
  625. stw_p(p++, 0xe020); stw_p(p++, 0x0bc0);
  626. /* lui t0, %hi(0x5e000000) */
  627. #else
  628. stw_p(p++, 0x0020); stw_p(p++, 0x00df);
  629. /* addiu[32] t0, $0, 0xdf */
  630. /* 0x68 corresponds to GT_ISD */
  631. stw_p(p++, 0x8422); stw_p(p++, 0x9068);
  632. /* sw t0, 0x68(t1) */
  633. /* Use kseg2 remapped address 0x1be00000 */
  634. stw_p(p++, 0xe040); stw_p(p++, 0x077d);
  635. /* lui t1, %hi(0xbbe00000) */
  636. stw_p(p++, 0x0020); stw_p(p++, 0x00c0);
  637. /* addiu[32] t0, $0, 0xc0 */
  638. /* 0x48 corresponds to GT_PCI0IOLD */
  639. stw_p(p++, 0x8422); stw_p(p++, 0x9048);
  640. /* sw t0, 0x48(t1) */
  641. stw_p(p++, 0x0020); stw_p(p++, 0x0040);
  642. /* addiu[32] t0, $0, 0x40 */
  643. /* 0x50 corresponds to GT_PCI0IOHD */
  644. stw_p(p++, 0x8422); stw_p(p++, 0x9050);
  645. /* sw t0, 0x50(t1) */
  646. stw_p(p++, 0x0020); stw_p(p++, 0x0080);
  647. /* addiu[32] t0, $0, 0x80 */
  648. /* 0x58 corresponds to GT_PCI0M0LD */
  649. stw_p(p++, 0x8422); stw_p(p++, 0x9058);
  650. /* sw t0, 0x58(t1) */
  651. stw_p(p++, 0x0020); stw_p(p++, 0x003f);
  652. /* addiu[32] t0, $0, 0x3f */
  653. /* 0x60 corresponds to GT_PCI0M0HD */
  654. stw_p(p++, 0x8422); stw_p(p++, 0x9060);
  655. /* sw t0, 0x60(t1) */
  656. stw_p(p++, 0x0020); stw_p(p++, 0x00c1);
  657. /* addiu[32] t0, $0, 0xc1 */
  658. /* 0x80 corresponds to GT_PCI0M1LD */
  659. stw_p(p++, 0x8422); stw_p(p++, 0x9080);
  660. /* sw t0, 0x80(t1) */
  661. stw_p(p++, 0x0020); stw_p(p++, 0x005e);
  662. /* addiu[32] t0, $0, 0x5e */
  663. #endif
  664. /* 0x88 corresponds to GT_PCI0M1HD */
  665. stw_p(p++, 0x8422); stw_p(p++, 0x9088);
  666. /* sw t0, 0x88(t1) */
  667. stw_p(p++, 0xe320 | NM_HI1(kernel_entry));
  668. stw_p(p++, NM_HI2(kernel_entry));
  669. /* lui t9,%hi(kernel_entry) */
  670. stw_p(p++, 0x8339); stw_p(p++, NM_LO(kernel_entry));
  671. /* ori t9,t9,%lo(kernel_entry) */
  672. stw_p(p++, 0x4bf9); stw_p(p++, 0x0000);
  673. /* jalrc t8 */
  674. }
  675. /*
  676. * ROM and pseudo bootloader
  677. *
  678. * The following code implements a very very simple bootloader. It first
  679. * loads the registers a0 to a3 to the values expected by the OS, and
  680. * then jump at the kernel address.
  681. *
  682. * The bootloader should pass the locations of the kernel arguments and
  683. * environment variables tables. Those tables contain the 32-bit address
  684. * of NULL terminated strings. The environment variables table should be
  685. * terminated by a NULL address.
  686. *
  687. * For a simpler implementation, the number of kernel arguments is fixed
  688. * to two (the name of the kernel and the command line), and the two
  689. * tables are actually the same one.
  690. *
  691. * The registers a0 to a3 should contain the following values:
  692. * a0 - number of kernel arguments
  693. * a1 - 32-bit address of the kernel arguments table
  694. * a2 - 32-bit address of the environment variables table
  695. * a3 - RAM size in bytes
  696. */
  697. static void write_bootloader(uint8_t *base, uint64_t run_addr,
  698. uint64_t kernel_entry)
  699. {
  700. uint32_t *p;
  701. /* Small bootloader */
  702. p = (uint32_t *)base;
  703. stl_p(p++, 0x08000000 | /* j 0x1fc00580 */
  704. ((run_addr + 0x580) & 0x0fffffff) >> 2);
  705. stl_p(p++, 0x00000000); /* nop */
  706. /* YAMON service vector */
  707. stl_p(base + 0x500, run_addr + 0x0580); /* start: */
  708. stl_p(base + 0x504, run_addr + 0x083c); /* print_count: */
  709. stl_p(base + 0x520, run_addr + 0x0580); /* start: */
  710. stl_p(base + 0x52c, run_addr + 0x0800); /* flush_cache: */
  711. stl_p(base + 0x534, run_addr + 0x0808); /* print: */
  712. stl_p(base + 0x538, run_addr + 0x0800); /* reg_cpu_isr: */
  713. stl_p(base + 0x53c, run_addr + 0x0800); /* unred_cpu_isr: */
  714. stl_p(base + 0x540, run_addr + 0x0800); /* reg_ic_isr: */
  715. stl_p(base + 0x544, run_addr + 0x0800); /* unred_ic_isr: */
  716. stl_p(base + 0x548, run_addr + 0x0800); /* reg_esr: */
  717. stl_p(base + 0x54c, run_addr + 0x0800); /* unreg_esr: */
  718. stl_p(base + 0x550, run_addr + 0x0800); /* getchar: */
  719. stl_p(base + 0x554, run_addr + 0x0800); /* syscon_read: */
  720. /* Second part of the bootloader */
  721. p = (uint32_t *) (base + 0x580);
  722. if (semihosting_get_argc()) {
  723. /* Preserve a0 content as arguments have been passed */
  724. stl_p(p++, 0x00000000); /* nop */
  725. } else {
  726. stl_p(p++, 0x24040002); /* addiu a0, zero, 2 */
  727. }
  728. /* lui sp, high(ENVP_VADDR) */
  729. stl_p(p++, 0x3c1d0000 | (((ENVP_VADDR - 64) >> 16) & 0xffff));
  730. /* ori sp, sp, low(ENVP_VADDR) */
  731. stl_p(p++, 0x37bd0000 | ((ENVP_VADDR - 64) & 0xffff));
  732. /* lui a1, high(ENVP_VADDR) */
  733. stl_p(p++, 0x3c050000 | ((ENVP_VADDR >> 16) & 0xffff));
  734. /* ori a1, a1, low(ENVP_VADDR) */
  735. stl_p(p++, 0x34a50000 | (ENVP_VADDR & 0xffff));
  736. /* lui a2, high(ENVP_VADDR + 8) */
  737. stl_p(p++, 0x3c060000 | (((ENVP_VADDR + 8) >> 16) & 0xffff));
  738. /* ori a2, a2, low(ENVP_VADDR + 8) */
  739. stl_p(p++, 0x34c60000 | ((ENVP_VADDR + 8) & 0xffff));
  740. /* lui a3, high(ram_low_size) */
  741. stl_p(p++, 0x3c070000 | (loaderparams.ram_low_size >> 16));
  742. /* ori a3, a3, low(ram_low_size) */
  743. stl_p(p++, 0x34e70000 | (loaderparams.ram_low_size & 0xffff));
  744. /* Load BAR registers as done by YAMON */
  745. stl_p(p++, 0x3c09b400); /* lui t1, 0xb400 */
  746. #if TARGET_BIG_ENDIAN
  747. stl_p(p++, 0x3c08df00); /* lui t0, 0xdf00 */
  748. #else
  749. stl_p(p++, 0x340800df); /* ori t0, r0, 0x00df */
  750. #endif
  751. stl_p(p++, 0xad280068); /* sw t0, 0x0068(t1) */
  752. stl_p(p++, 0x3c09bbe0); /* lui t1, 0xbbe0 */
  753. #if TARGET_BIG_ENDIAN
  754. stl_p(p++, 0x3c08c000); /* lui t0, 0xc000 */
  755. #else
  756. stl_p(p++, 0x340800c0); /* ori t0, r0, 0x00c0 */
  757. #endif
  758. stl_p(p++, 0xad280048); /* sw t0, 0x0048(t1) */
  759. #if TARGET_BIG_ENDIAN
  760. stl_p(p++, 0x3c084000); /* lui t0, 0x4000 */
  761. #else
  762. stl_p(p++, 0x34080040); /* ori t0, r0, 0x0040 */
  763. #endif
  764. stl_p(p++, 0xad280050); /* sw t0, 0x0050(t1) */
  765. #if TARGET_BIG_ENDIAN
  766. stl_p(p++, 0x3c088000); /* lui t0, 0x8000 */
  767. #else
  768. stl_p(p++, 0x34080080); /* ori t0, r0, 0x0080 */
  769. #endif
  770. stl_p(p++, 0xad280058); /* sw t0, 0x0058(t1) */
  771. #if TARGET_BIG_ENDIAN
  772. stl_p(p++, 0x3c083f00); /* lui t0, 0x3f00 */
  773. #else
  774. stl_p(p++, 0x3408003f); /* ori t0, r0, 0x003f */
  775. #endif
  776. stl_p(p++, 0xad280060); /* sw t0, 0x0060(t1) */
  777. #if TARGET_BIG_ENDIAN
  778. stl_p(p++, 0x3c08c100); /* lui t0, 0xc100 */
  779. #else
  780. stl_p(p++, 0x340800c1); /* ori t0, r0, 0x00c1 */
  781. #endif
  782. stl_p(p++, 0xad280080); /* sw t0, 0x0080(t1) */
  783. #if TARGET_BIG_ENDIAN
  784. stl_p(p++, 0x3c085e00); /* lui t0, 0x5e00 */
  785. #else
  786. stl_p(p++, 0x3408005e); /* ori t0, r0, 0x005e */
  787. #endif
  788. stl_p(p++, 0xad280088); /* sw t0, 0x0088(t1) */
  789. /* Jump to kernel code */
  790. stl_p(p++, 0x3c1f0000 |
  791. ((kernel_entry >> 16) & 0xffff)); /* lui ra, high(kernel_entry) */
  792. stl_p(p++, 0x37ff0000 |
  793. (kernel_entry & 0xffff)); /* ori ra, ra, low(kernel_entry) */
  794. stl_p(p++, 0x03e00009); /* jalr ra */
  795. stl_p(p++, 0x00000000); /* nop */
  796. /* YAMON subroutines */
  797. p = (uint32_t *) (base + 0x800);
  798. stl_p(p++, 0x03e00009); /* jalr ra */
  799. stl_p(p++, 0x24020000); /* li v0,0 */
  800. /* 808 YAMON print */
  801. stl_p(p++, 0x03e06821); /* move t5,ra */
  802. stl_p(p++, 0x00805821); /* move t3,a0 */
  803. stl_p(p++, 0x00a05021); /* move t2,a1 */
  804. stl_p(p++, 0x91440000); /* lbu a0,0(t2) */
  805. stl_p(p++, 0x254a0001); /* addiu t2,t2,1 */
  806. stl_p(p++, 0x10800005); /* beqz a0,834 */
  807. stl_p(p++, 0x00000000); /* nop */
  808. stl_p(p++, 0x0ff0021c); /* jal 870 */
  809. stl_p(p++, 0x00000000); /* nop */
  810. stl_p(p++, 0x1000fff9); /* b 814 */
  811. stl_p(p++, 0x00000000); /* nop */
  812. stl_p(p++, 0x01a00009); /* jalr t5 */
  813. stl_p(p++, 0x01602021); /* move a0,t3 */
  814. /* 0x83c YAMON print_count */
  815. stl_p(p++, 0x03e06821); /* move t5,ra */
  816. stl_p(p++, 0x00805821); /* move t3,a0 */
  817. stl_p(p++, 0x00a05021); /* move t2,a1 */
  818. stl_p(p++, 0x00c06021); /* move t4,a2 */
  819. stl_p(p++, 0x91440000); /* lbu a0,0(t2) */
  820. stl_p(p++, 0x0ff0021c); /* jal 870 */
  821. stl_p(p++, 0x00000000); /* nop */
  822. stl_p(p++, 0x254a0001); /* addiu t2,t2,1 */
  823. stl_p(p++, 0x258cffff); /* addiu t4,t4,-1 */
  824. stl_p(p++, 0x1580fffa); /* bnez t4,84c */
  825. stl_p(p++, 0x00000000); /* nop */
  826. stl_p(p++, 0x01a00009); /* jalr t5 */
  827. stl_p(p++, 0x01602021); /* move a0,t3 */
  828. /* 0x870 */
  829. stl_p(p++, 0x3c08b800); /* lui t0,0xb400 */
  830. stl_p(p++, 0x350803f8); /* ori t0,t0,0x3f8 */
  831. stl_p(p++, 0x91090005); /* lbu t1,5(t0) */
  832. stl_p(p++, 0x00000000); /* nop */
  833. stl_p(p++, 0x31290040); /* andi t1,t1,0x40 */
  834. stl_p(p++, 0x1120fffc); /* beqz t1,878 <outch+0x8> */
  835. stl_p(p++, 0x00000000); /* nop */
  836. stl_p(p++, 0x03e00009); /* jalr ra */
  837. stl_p(p++, 0xa1040000); /* sb a0,0(t0) */
  838. }
  839. static void G_GNUC_PRINTF(3, 4) prom_set(uint32_t *prom_buf, int index,
  840. const char *string, ...)
  841. {
  842. va_list ap;
  843. uint32_t table_addr;
  844. if (index >= ENVP_NB_ENTRIES) {
  845. return;
  846. }
  847. if (string == NULL) {
  848. prom_buf[index] = 0;
  849. return;
  850. }
  851. table_addr = sizeof(uint32_t) * ENVP_NB_ENTRIES + index * ENVP_ENTRY_SIZE;
  852. prom_buf[index] = tswap32(ENVP_VADDR + table_addr);
  853. va_start(ap, string);
  854. vsnprintf((char *)prom_buf + table_addr, ENVP_ENTRY_SIZE, string, ap);
  855. va_end(ap);
  856. }
  857. /* Kernel */
  858. static uint64_t load_kernel(void)
  859. {
  860. uint64_t kernel_entry, kernel_high, initrd_size;
  861. long kernel_size;
  862. ram_addr_t initrd_offset;
  863. int big_endian;
  864. uint32_t *prom_buf;
  865. long prom_size;
  866. int prom_index = 0;
  867. uint64_t (*xlate_to_kseg0) (void *opaque, uint64_t addr);
  868. #if TARGET_BIG_ENDIAN
  869. big_endian = 1;
  870. #else
  871. big_endian = 0;
  872. #endif
  873. kernel_size = load_elf(loaderparams.kernel_filename, NULL,
  874. cpu_mips_kseg0_to_phys, NULL,
  875. &kernel_entry, NULL,
  876. &kernel_high, NULL, big_endian, EM_MIPS,
  877. 1, 0);
  878. if (kernel_size < 0) {
  879. error_report("could not load kernel '%s': %s",
  880. loaderparams.kernel_filename,
  881. load_elf_strerror(kernel_size));
  882. exit(1);
  883. }
  884. /* Check where the kernel has been linked */
  885. if (kernel_entry & 0x80000000ll) {
  886. if (kvm_enabled()) {
  887. error_report("KVM guest kernels must be linked in useg. "
  888. "Did you forget to enable CONFIG_KVM_GUEST?");
  889. exit(1);
  890. }
  891. xlate_to_kseg0 = cpu_mips_phys_to_kseg0;
  892. } else {
  893. /* if kernel entry is in useg it is probably a KVM T&E kernel */
  894. mips_um_ksegs_enable();
  895. xlate_to_kseg0 = cpu_mips_kvm_um_phys_to_kseg0;
  896. }
  897. /* load initrd */
  898. initrd_size = 0;
  899. initrd_offset = 0;
  900. if (loaderparams.initrd_filename) {
  901. initrd_size = get_image_size(loaderparams.initrd_filename);
  902. if (initrd_size > 0) {
  903. /*
  904. * The kernel allocates the bootmap memory in the low memory after
  905. * the initrd. It takes at most 128kiB for 2GB RAM and 4kiB
  906. * pages.
  907. */
  908. initrd_offset = ROUND_UP(loaderparams.ram_low_size
  909. - (initrd_size + 128 * KiB),
  910. INITRD_PAGE_SIZE);
  911. if (kernel_high >= initrd_offset) {
  912. error_report("memory too small for initial ram disk '%s'",
  913. loaderparams.initrd_filename);
  914. exit(1);
  915. }
  916. initrd_size = load_image_targphys(loaderparams.initrd_filename,
  917. initrd_offset,
  918. loaderparams.ram_size - initrd_offset);
  919. }
  920. if (initrd_size == (target_ulong) -1) {
  921. error_report("could not load initial ram disk '%s'",
  922. loaderparams.initrd_filename);
  923. exit(1);
  924. }
  925. }
  926. /* Setup prom parameters. */
  927. prom_size = ENVP_NB_ENTRIES * (sizeof(int32_t) + ENVP_ENTRY_SIZE);
  928. prom_buf = g_malloc(prom_size);
  929. prom_set(prom_buf, prom_index++, "%s", loaderparams.kernel_filename);
  930. if (initrd_size > 0) {
  931. prom_set(prom_buf, prom_index++,
  932. "rd_start=0x%" PRIx64 " rd_size=%" PRId64 " %s",
  933. xlate_to_kseg0(NULL, initrd_offset),
  934. initrd_size, loaderparams.kernel_cmdline);
  935. } else {
  936. prom_set(prom_buf, prom_index++, "%s", loaderparams.kernel_cmdline);
  937. }
  938. prom_set(prom_buf, prom_index++, "memsize");
  939. prom_set(prom_buf, prom_index++, "%u", loaderparams.ram_low_size);
  940. prom_set(prom_buf, prom_index++, "ememsize");
  941. prom_set(prom_buf, prom_index++, "%u", loaderparams.ram_size);
  942. prom_set(prom_buf, prom_index++, "modetty0");
  943. prom_set(prom_buf, prom_index++, "38400n8r");
  944. prom_set(prom_buf, prom_index++, NULL);
  945. rom_add_blob_fixed("prom", prom_buf, prom_size, ENVP_PADDR);
  946. g_free(prom_buf);
  947. return kernel_entry;
  948. }
  949. static void malta_mips_config(MIPSCPU *cpu)
  950. {
  951. MachineState *ms = MACHINE(qdev_get_machine());
  952. unsigned int smp_cpus = ms->smp.cpus;
  953. CPUMIPSState *env = &cpu->env;
  954. CPUState *cs = CPU(cpu);
  955. if (ase_mt_available(env)) {
  956. env->mvp->CP0_MVPConf0 = deposit32(env->mvp->CP0_MVPConf0,
  957. CP0MVPC0_PTC, 8,
  958. smp_cpus * cs->nr_threads - 1);
  959. env->mvp->CP0_MVPConf0 = deposit32(env->mvp->CP0_MVPConf0,
  960. CP0MVPC0_PVPE, 4, smp_cpus - 1);
  961. }
  962. }
  963. static void main_cpu_reset(void *opaque)
  964. {
  965. MIPSCPU *cpu = opaque;
  966. CPUMIPSState *env = &cpu->env;
  967. cpu_reset(CPU(cpu));
  968. /*
  969. * The bootloader does not need to be rewritten as it is located in a
  970. * read only location. The kernel location and the arguments table
  971. * location does not change.
  972. */
  973. if (loaderparams.kernel_filename) {
  974. env->CP0_Status &= ~(1 << CP0St_ERL);
  975. }
  976. malta_mips_config(cpu);
  977. if (kvm_enabled()) {
  978. /* Start running from the bootloader we wrote to end of RAM */
  979. env->active_tc.PC = 0x40000000 + loaderparams.ram_low_size;
  980. }
  981. }
  982. static void create_cpu_without_cps(MachineState *ms, MaltaState *s,
  983. qemu_irq *cbus_irq, qemu_irq *i8259_irq)
  984. {
  985. CPUMIPSState *env;
  986. MIPSCPU *cpu;
  987. int i;
  988. for (i = 0; i < ms->smp.cpus; i++) {
  989. cpu = mips_cpu_create_with_clock(ms->cpu_type, s->cpuclk);
  990. /* Init internal devices */
  991. cpu_mips_irq_init_cpu(cpu);
  992. cpu_mips_clock_init(cpu);
  993. qemu_register_reset(main_cpu_reset, cpu);
  994. }
  995. cpu = MIPS_CPU(first_cpu);
  996. env = &cpu->env;
  997. *i8259_irq = env->irq[2];
  998. *cbus_irq = env->irq[4];
  999. }
  1000. static void create_cps(MachineState *ms, MaltaState *s,
  1001. qemu_irq *cbus_irq, qemu_irq *i8259_irq)
  1002. {
  1003. object_initialize_child(OBJECT(s), "cps", &s->cps, TYPE_MIPS_CPS);
  1004. object_property_set_str(OBJECT(&s->cps), "cpu-type", ms->cpu_type,
  1005. &error_fatal);
  1006. object_property_set_int(OBJECT(&s->cps), "num-vp", ms->smp.cpus,
  1007. &error_fatal);
  1008. qdev_connect_clock_in(DEVICE(&s->cps), "clk-in", s->cpuclk);
  1009. sysbus_realize(SYS_BUS_DEVICE(&s->cps), &error_fatal);
  1010. sysbus_mmio_map_overlap(SYS_BUS_DEVICE(&s->cps), 0, 0, 1);
  1011. *i8259_irq = get_cps_irq(&s->cps, 3);
  1012. *cbus_irq = NULL;
  1013. }
  1014. static void mips_create_cpu(MachineState *ms, MaltaState *s,
  1015. qemu_irq *cbus_irq, qemu_irq *i8259_irq)
  1016. {
  1017. if ((ms->smp.cpus > 1) && cpu_type_supports_cps_smp(ms->cpu_type)) {
  1018. create_cps(ms, s, cbus_irq, i8259_irq);
  1019. } else {
  1020. create_cpu_without_cps(ms, s, cbus_irq, i8259_irq);
  1021. }
  1022. }
  1023. static
  1024. void mips_malta_init(MachineState *machine)
  1025. {
  1026. ram_addr_t ram_size = machine->ram_size;
  1027. ram_addr_t ram_low_size;
  1028. const char *kernel_filename = machine->kernel_filename;
  1029. const char *kernel_cmdline = machine->kernel_cmdline;
  1030. const char *initrd_filename = machine->initrd_filename;
  1031. char *filename;
  1032. PFlashCFI01 *fl;
  1033. MemoryRegion *system_memory = get_system_memory();
  1034. MemoryRegion *ram_low_preio = g_new(MemoryRegion, 1);
  1035. MemoryRegion *ram_low_postio;
  1036. MemoryRegion *bios, *bios_copy = g_new(MemoryRegion, 1);
  1037. const size_t smbus_eeprom_size = 8 * 256;
  1038. uint8_t *smbus_eeprom_buf = g_malloc0(smbus_eeprom_size);
  1039. uint64_t kernel_entry, bootloader_run_addr;
  1040. PCIBus *pci_bus;
  1041. ISABus *isa_bus;
  1042. qemu_irq cbus_irq, i8259_irq;
  1043. I2CBus *smbus;
  1044. DriveInfo *dinfo;
  1045. int fl_idx = 0;
  1046. int be;
  1047. MaltaState *s;
  1048. DeviceState *dev;
  1049. s = MIPS_MALTA(qdev_new(TYPE_MIPS_MALTA));
  1050. sysbus_realize_and_unref(SYS_BUS_DEVICE(s), &error_fatal);
  1051. /* create CPU */
  1052. mips_create_cpu(machine, s, &cbus_irq, &i8259_irq);
  1053. /* allocate RAM */
  1054. if (ram_size > 2 * GiB) {
  1055. error_report("Too much memory for this machine: %" PRId64 "MB,"
  1056. " maximum 2048MB", ram_size / MiB);
  1057. exit(1);
  1058. }
  1059. /* register RAM at high address where it is undisturbed by IO */
  1060. memory_region_add_subregion(system_memory, 0x80000000, machine->ram);
  1061. /* alias for pre IO hole access */
  1062. memory_region_init_alias(ram_low_preio, NULL, "mips_malta_low_preio.ram",
  1063. machine->ram, 0, MIN(ram_size, 256 * MiB));
  1064. memory_region_add_subregion(system_memory, 0, ram_low_preio);
  1065. /* alias for post IO hole access, if there is enough RAM */
  1066. if (ram_size > 512 * MiB) {
  1067. ram_low_postio = g_new(MemoryRegion, 1);
  1068. memory_region_init_alias(ram_low_postio, NULL,
  1069. "mips_malta_low_postio.ram",
  1070. machine->ram, 512 * MiB,
  1071. ram_size - 512 * MiB);
  1072. memory_region_add_subregion(system_memory, 512 * MiB,
  1073. ram_low_postio);
  1074. }
  1075. #if TARGET_BIG_ENDIAN
  1076. be = 1;
  1077. #else
  1078. be = 0;
  1079. #endif
  1080. /* FPGA */
  1081. /* The CBUS UART is attached to the MIPS CPU INT2 pin, ie interrupt 4 */
  1082. malta_fpga_init(system_memory, FPGA_ADDRESS, cbus_irq, serial_hd(2));
  1083. /* Load firmware in flash / BIOS. */
  1084. dinfo = drive_get(IF_PFLASH, 0, fl_idx);
  1085. fl = pflash_cfi01_register(FLASH_ADDRESS, "mips_malta.bios",
  1086. FLASH_SIZE,
  1087. dinfo ? blk_by_legacy_dinfo(dinfo) : NULL,
  1088. 65536,
  1089. 4, 0x0000, 0x0000, 0x0000, 0x0000, be);
  1090. bios = pflash_cfi01_get_memory(fl);
  1091. fl_idx++;
  1092. if (kernel_filename) {
  1093. ram_low_size = MIN(ram_size, 256 * MiB);
  1094. /* For KVM we reserve 1MB of RAM for running bootloader */
  1095. if (kvm_enabled()) {
  1096. ram_low_size -= 0x100000;
  1097. bootloader_run_addr = cpu_mips_kvm_um_phys_to_kseg0(NULL, ram_low_size);
  1098. } else {
  1099. bootloader_run_addr = cpu_mips_phys_to_kseg0(NULL, RESET_ADDRESS);
  1100. }
  1101. /* Write a small bootloader to the flash location. */
  1102. loaderparams.ram_size = ram_size;
  1103. loaderparams.ram_low_size = ram_low_size;
  1104. loaderparams.kernel_filename = kernel_filename;
  1105. loaderparams.kernel_cmdline = kernel_cmdline;
  1106. loaderparams.initrd_filename = initrd_filename;
  1107. kernel_entry = load_kernel();
  1108. if (!cpu_type_supports_isa(machine->cpu_type, ISA_NANOMIPS32)) {
  1109. write_bootloader(memory_region_get_ram_ptr(bios),
  1110. bootloader_run_addr, kernel_entry);
  1111. } else {
  1112. write_bootloader_nanomips(memory_region_get_ram_ptr(bios),
  1113. bootloader_run_addr, kernel_entry);
  1114. }
  1115. if (kvm_enabled()) {
  1116. /* Write the bootloader code @ the end of RAM, 1MB reserved */
  1117. write_bootloader(memory_region_get_ram_ptr(ram_low_preio) +
  1118. ram_low_size,
  1119. bootloader_run_addr, kernel_entry);
  1120. }
  1121. } else {
  1122. target_long bios_size = FLASH_SIZE;
  1123. /* The flash region isn't executable from a KVM guest */
  1124. if (kvm_enabled()) {
  1125. error_report("KVM enabled but no -kernel argument was specified. "
  1126. "Booting from flash is not supported with KVM.");
  1127. exit(1);
  1128. }
  1129. /* Load firmware from flash. */
  1130. if (!dinfo) {
  1131. /* Load a BIOS image. */
  1132. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS,
  1133. machine->firmware ?: BIOS_FILENAME);
  1134. if (filename) {
  1135. bios_size = load_image_targphys(filename, FLASH_ADDRESS,
  1136. BIOS_SIZE);
  1137. g_free(filename);
  1138. } else {
  1139. bios_size = -1;
  1140. }
  1141. if ((bios_size < 0 || bios_size > BIOS_SIZE) &&
  1142. machine->firmware && !qtest_enabled()) {
  1143. error_report("Could not load MIPS bios '%s'", machine->firmware);
  1144. exit(1);
  1145. }
  1146. }
  1147. /*
  1148. * In little endian mode the 32bit words in the bios are swapped,
  1149. * a neat trick which allows bi-endian firmware.
  1150. */
  1151. #if !TARGET_BIG_ENDIAN
  1152. {
  1153. uint32_t *end, *addr;
  1154. const size_t swapsize = MIN(bios_size, 0x3e0000);
  1155. addr = rom_ptr(FLASH_ADDRESS, swapsize);
  1156. if (!addr) {
  1157. addr = memory_region_get_ram_ptr(bios);
  1158. }
  1159. end = (void *)addr + swapsize;
  1160. while (addr < end) {
  1161. bswap32s(addr);
  1162. addr++;
  1163. }
  1164. }
  1165. #endif
  1166. }
  1167. /*
  1168. * Map the BIOS at a 2nd physical location, as on the real board.
  1169. * Copy it so that we can patch in the MIPS revision, which cannot be
  1170. * handled by an overlapping region as the resulting ROM code subpage
  1171. * regions are not executable.
  1172. */
  1173. memory_region_init_ram(bios_copy, NULL, "bios.1fc", BIOS_SIZE,
  1174. &error_fatal);
  1175. if (!rom_copy(memory_region_get_ram_ptr(bios_copy),
  1176. FLASH_ADDRESS, BIOS_SIZE)) {
  1177. memcpy(memory_region_get_ram_ptr(bios_copy),
  1178. memory_region_get_ram_ptr(bios), BIOS_SIZE);
  1179. }
  1180. memory_region_set_readonly(bios_copy, true);
  1181. memory_region_add_subregion(system_memory, RESET_ADDRESS, bios_copy);
  1182. /* Board ID = 0x420 (Malta Board with CoreLV) */
  1183. stl_p(memory_region_get_ram_ptr(bios_copy) + 0x10, 0x00000420);
  1184. /* Northbridge */
  1185. dev = sysbus_create_simple("gt64120", -1, NULL);
  1186. pci_bus = PCI_BUS(qdev_get_child_bus(dev, "pci"));
  1187. /*
  1188. * The whole address space decoded by the GT-64120A doesn't generate
  1189. * exception when accessing invalid memory. Create an empty slot to
  1190. * emulate this feature.
  1191. */
  1192. empty_slot_init("GT64120", 0, 0x20000000);
  1193. /* Southbridge */
  1194. dev = piix4_create(pci_bus, &isa_bus, &smbus);
  1195. /* Interrupt controller */
  1196. qdev_connect_gpio_out_named(dev, "intr", 0, i8259_irq);
  1197. /* generate SPD EEPROM data */
  1198. generate_eeprom_spd(&smbus_eeprom_buf[0 * 256], ram_size);
  1199. generate_eeprom_serial(&smbus_eeprom_buf[6 * 256]);
  1200. smbus_eeprom_init(smbus, 8, smbus_eeprom_buf, smbus_eeprom_size);
  1201. g_free(smbus_eeprom_buf);
  1202. /* Super I/O: SMS FDC37M817 */
  1203. isa_create_simple(isa_bus, TYPE_FDC37M81X_SUPERIO);
  1204. /* Network card */
  1205. network_init(pci_bus);
  1206. /* Optional PCI video card */
  1207. pci_vga_init(pci_bus);
  1208. }
  1209. static void mips_malta_instance_init(Object *obj)
  1210. {
  1211. MaltaState *s = MIPS_MALTA(obj);
  1212. s->cpuclk = qdev_init_clock_out(DEVICE(obj), "cpu-refclk");
  1213. clock_set_hz(s->cpuclk, 320000000); /* 320 MHz */
  1214. }
  1215. static const TypeInfo mips_malta_device = {
  1216. .name = TYPE_MIPS_MALTA,
  1217. .parent = TYPE_SYS_BUS_DEVICE,
  1218. .instance_size = sizeof(MaltaState),
  1219. .instance_init = mips_malta_instance_init,
  1220. };
  1221. static void mips_malta_machine_init(MachineClass *mc)
  1222. {
  1223. mc->desc = "MIPS Malta Core LV";
  1224. mc->init = mips_malta_init;
  1225. mc->block_default_type = IF_IDE;
  1226. mc->max_cpus = 16;
  1227. mc->is_default = true;
  1228. #ifdef TARGET_MIPS64
  1229. mc->default_cpu_type = MIPS_CPU_TYPE_NAME("20Kc");
  1230. #else
  1231. mc->default_cpu_type = MIPS_CPU_TYPE_NAME("24Kf");
  1232. #endif
  1233. mc->default_ram_id = "mips_malta.ram";
  1234. }
  1235. DEFINE_MACHINE("malta", mips_malta_machine_init)
  1236. static void mips_malta_register_types(void)
  1237. {
  1238. type_register_static(&mips_malta_device);
  1239. }
  1240. type_init(mips_malta_register_types)