bonito.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. /*
  2. * bonito north bridge support
  3. *
  4. * Copyright (c) 2008 yajin (yajin@vm-kernel.org)
  5. * Copyright (c) 2010 Huacai Chen (zltjiangshi@gmail.com)
  6. *
  7. * This code is licensed under the GNU GPL v2.
  8. *
  9. * Contributions after 2012-01-13 are licensed under the terms of the
  10. * GNU GPL, version 2 or (at your option) any later version.
  11. */
  12. /*
  13. * fuloong 2e mini pc has a bonito north bridge.
  14. */
  15. /*
  16. * what is the meaning of devfn in qemu and IDSEL in bonito northbridge?
  17. *
  18. * devfn pci_slot<<3 + funno
  19. * one pci bus can have 32 devices and each device can have 8 functions.
  20. *
  21. * In bonito north bridge, pci slot = IDSEL bit - 12.
  22. * For example, PCI_IDSEL_VIA686B = 17,
  23. * pci slot = 17-12=5
  24. *
  25. * so
  26. * VT686B_FUN0's devfn = (5<<3)+0
  27. * VT686B_FUN1's devfn = (5<<3)+1
  28. *
  29. * qemu also uses pci address for north bridge to access pci config register.
  30. * bus_no [23:16]
  31. * dev_no [15:11]
  32. * fun_no [10:8]
  33. * reg_no [7:2]
  34. *
  35. * so function bonito_sbridge_pciaddr for the translation from
  36. * north bridge address to pci address.
  37. */
  38. #include "qemu/osdep.h"
  39. #include "qemu/units.h"
  40. #include "qapi/error.h"
  41. #include "qemu/error-report.h"
  42. #include "hw/pci/pci_device.h"
  43. #include "hw/irq.h"
  44. #include "hw/mips/mips.h"
  45. #include "hw/pci/pci_host.h"
  46. #include "migration/vmstate.h"
  47. #include "sysemu/reset.h"
  48. #include "sysemu/runstate.h"
  49. #include "hw/misc/unimp.h"
  50. #include "hw/registerfields.h"
  51. #include "qom/object.h"
  52. #include "trace.h"
  53. /* #define DEBUG_BONITO */
  54. #ifdef DEBUG_BONITO
  55. #define DPRINTF(fmt, ...) fprintf(stderr, "%s: " fmt, __func__, ##__VA_ARGS__)
  56. #else
  57. #define DPRINTF(fmt, ...)
  58. #endif
  59. /* from linux soure code. include/asm-mips/mips-boards/bonito64.h*/
  60. #define BONITO_BOOT_BASE 0x1fc00000
  61. #define BONITO_BOOT_SIZE 0x00100000
  62. #define BONITO_BOOT_TOP (BONITO_BOOT_BASE + BONITO_BOOT_SIZE - 1)
  63. #define BONITO_FLASH_BASE 0x1c000000
  64. #define BONITO_FLASH_SIZE 0x03000000
  65. #define BONITO_FLASH_TOP (BONITO_FLASH_BASE + BONITO_FLASH_SIZE - 1)
  66. #define BONITO_SOCKET_BASE 0x1f800000
  67. #define BONITO_SOCKET_SIZE 0x00400000
  68. #define BONITO_SOCKET_TOP (BONITO_SOCKET_BASE + BONITO_SOCKET_SIZE - 1)
  69. #define BONITO_REG_BASE 0x1fe00000
  70. #define BONITO_REG_SIZE 0x00040000
  71. #define BONITO_REG_TOP (BONITO_REG_BASE + BONITO_REG_SIZE - 1)
  72. #define BONITO_DEV_BASE 0x1ff00000
  73. #define BONITO_DEV_SIZE 0x00100000
  74. #define BONITO_DEV_TOP (BONITO_DEV_BASE + BONITO_DEV_SIZE - 1)
  75. #define BONITO_PCILO_BASE 0x10000000
  76. #define BONITO_PCILO_BASE_VA 0xb0000000
  77. #define BONITO_PCILO_SIZE 0x0c000000
  78. #define BONITO_PCILO_TOP (BONITO_PCILO_BASE + BONITO_PCILO_SIZE - 1)
  79. #define BONITO_PCILO0_BASE 0x10000000
  80. #define BONITO_PCILO1_BASE 0x14000000
  81. #define BONITO_PCILO2_BASE 0x18000000
  82. #define BONITO_PCIHI_BASE 0x20000000
  83. #define BONITO_PCIHI_SIZE 0x60000000
  84. #define BONITO_PCIHI_TOP (BONITO_PCIHI_BASE + BONITO_PCIHI_SIZE - 1)
  85. #define BONITO_PCIIO_BASE 0x1fd00000
  86. #define BONITO_PCIIO_BASE_VA 0xbfd00000
  87. #define BONITO_PCIIO_SIZE 0x00010000
  88. #define BONITO_PCIIO_TOP (BONITO_PCIIO_BASE + BONITO_PCIIO_SIZE - 1)
  89. #define BONITO_PCICFG_BASE 0x1fe80000
  90. #define BONITO_PCICFG_SIZE 0x00080000
  91. #define BONITO_PCICFG_TOP (BONITO_PCICFG_BASE + BONITO_PCICFG_SIZE - 1)
  92. #define BONITO_PCICONFIGBASE 0x00
  93. #define BONITO_REGBASE 0x100
  94. #define BONITO_PCICONFIG_BASE (BONITO_PCICONFIGBASE + BONITO_REG_BASE)
  95. #define BONITO_PCICONFIG_SIZE (0x100)
  96. #define BONITO_INTERNAL_REG_BASE (BONITO_REGBASE + BONITO_REG_BASE)
  97. #define BONITO_INTERNAL_REG_SIZE (0x70)
  98. #define BONITO_SPCICONFIG_BASE (BONITO_PCICFG_BASE)
  99. #define BONITO_SPCICONFIG_SIZE (BONITO_PCICFG_SIZE)
  100. /* 1. Bonito h/w Configuration */
  101. /* Power on register */
  102. #define BONITO_BONPONCFG (0x00 >> 2) /* 0x100 */
  103. /* PCI configuration register */
  104. #define BONITO_BONGENCFG_OFFSET 0x4
  105. #define BONITO_BONGENCFG (BONITO_BONGENCFG_OFFSET >> 2) /*0x104 */
  106. REG32(BONGENCFG, 0x104)
  107. FIELD(BONGENCFG, DEBUGMODE, 0, 1)
  108. FIELD(BONGENCFG, SNOOP, 1, 1)
  109. FIELD(BONGENCFG, CPUSELFRESET, 2, 1)
  110. FIELD(BONGENCFG, BYTESWAP, 6, 1)
  111. FIELD(BONGENCFG, UNCACHED, 7, 1)
  112. FIELD(BONGENCFG, PREFETCH, 8, 1)
  113. FIELD(BONGENCFG, WRITEBEHIND, 9, 1)
  114. FIELD(BONGENCFG, PCIQUEUE, 12, 1)
  115. /* 2. IO & IDE configuration */
  116. #define BONITO_IODEVCFG (0x08 >> 2) /* 0x108 */
  117. /* 3. IO & IDE configuration */
  118. #define BONITO_SDCFG (0x0c >> 2) /* 0x10c */
  119. /* 4. PCI address map control */
  120. #define BONITO_PCIMAP (0x10 >> 2) /* 0x110 */
  121. #define BONITO_PCIMEMBASECFG (0x14 >> 2) /* 0x114 */
  122. #define BONITO_PCIMAP_CFG (0x18 >> 2) /* 0x118 */
  123. /* 5. ICU & GPIO regs */
  124. /* GPIO Regs - r/w */
  125. #define BONITO_GPIODATA_OFFSET 0x1c
  126. #define BONITO_GPIODATA (BONITO_GPIODATA_OFFSET >> 2) /* 0x11c */
  127. #define BONITO_GPIOIE (0x20 >> 2) /* 0x120 */
  128. /* ICU Configuration Regs - r/w */
  129. #define BONITO_INTEDGE (0x24 >> 2) /* 0x124 */
  130. #define BONITO_INTSTEER (0x28 >> 2) /* 0x128 */
  131. #define BONITO_INTPOL (0x2c >> 2) /* 0x12c */
  132. /* ICU Enable Regs - IntEn & IntISR are r/o. */
  133. #define BONITO_INTENSET (0x30 >> 2) /* 0x130 */
  134. #define BONITO_INTENCLR (0x34 >> 2) /* 0x134 */
  135. #define BONITO_INTEN (0x38 >> 2) /* 0x138 */
  136. #define BONITO_INTISR (0x3c >> 2) /* 0x13c */
  137. /* PCI mail boxes */
  138. #define BONITO_PCIMAIL0_OFFSET 0x40
  139. #define BONITO_PCIMAIL1_OFFSET 0x44
  140. #define BONITO_PCIMAIL2_OFFSET 0x48
  141. #define BONITO_PCIMAIL3_OFFSET 0x4c
  142. #define BONITO_PCIMAIL0 (0x40 >> 2) /* 0x140 */
  143. #define BONITO_PCIMAIL1 (0x44 >> 2) /* 0x144 */
  144. #define BONITO_PCIMAIL2 (0x48 >> 2) /* 0x148 */
  145. #define BONITO_PCIMAIL3 (0x4c >> 2) /* 0x14c */
  146. /* 6. PCI cache */
  147. #define BONITO_PCICACHECTRL (0x50 >> 2) /* 0x150 */
  148. #define BONITO_PCICACHETAG (0x54 >> 2) /* 0x154 */
  149. #define BONITO_PCIBADADDR (0x58 >> 2) /* 0x158 */
  150. #define BONITO_PCIMSTAT (0x5c >> 2) /* 0x15c */
  151. /* 7. other*/
  152. #define BONITO_TIMECFG (0x60 >> 2) /* 0x160 */
  153. #define BONITO_CPUCFG (0x64 >> 2) /* 0x164 */
  154. #define BONITO_DQCFG (0x68 >> 2) /* 0x168 */
  155. #define BONITO_MEMSIZE (0x6C >> 2) /* 0x16c */
  156. #define BONITO_REGS (0x70 >> 2)
  157. /* PCI config for south bridge. type 0 */
  158. #define BONITO_PCICONF_IDSEL_MASK 0xfffff800 /* [31:11] */
  159. #define BONITO_PCICONF_IDSEL_OFFSET 11
  160. #define BONITO_PCICONF_FUN_MASK 0x700 /* [10:8] */
  161. #define BONITO_PCICONF_FUN_OFFSET 8
  162. #define BONITO_PCICONF_REG_MASK_DS (~3) /* Per datasheet */
  163. #define BONITO_PCICONF_REG_MASK_HW 0xff /* As seen running PMON */
  164. #define BONITO_PCICONF_REG_OFFSET 0
  165. /* idsel BIT = pci slot number +12 */
  166. #define PCI_SLOT_BASE 12
  167. #define PCI_IDSEL_VIA686B_BIT (17)
  168. #define PCI_IDSEL_VIA686B (1 << PCI_IDSEL_VIA686B_BIT)
  169. #define PCI_ADDR(busno , devno , funno , regno) \
  170. ((PCI_BUILD_BDF(busno, PCI_DEVFN(devno , funno)) << 8) + (regno))
  171. typedef struct BonitoState BonitoState;
  172. struct PCIBonitoState {
  173. PCIDevice dev;
  174. BonitoState *pcihost;
  175. uint32_t regs[BONITO_REGS];
  176. struct bonldma {
  177. uint32_t ldmactrl;
  178. uint32_t ldmastat;
  179. uint32_t ldmaaddr;
  180. uint32_t ldmago;
  181. } bonldma;
  182. /* Based at 1fe00300, bonito Copier */
  183. struct boncop {
  184. uint32_t copctrl;
  185. uint32_t copstat;
  186. uint32_t coppaddr;
  187. uint32_t copgo;
  188. } boncop;
  189. /* Bonito registers */
  190. MemoryRegion iomem;
  191. MemoryRegion iomem_ldma;
  192. MemoryRegion iomem_cop;
  193. MemoryRegion bonito_pciio;
  194. MemoryRegion bonito_localio;
  195. };
  196. typedef struct PCIBonitoState PCIBonitoState;
  197. struct BonitoState {
  198. PCIHostState parent_obj;
  199. qemu_irq *pic;
  200. PCIBonitoState *pci_dev;
  201. MemoryRegion pci_mem;
  202. };
  203. #define TYPE_BONITO_PCI_HOST_BRIDGE "Bonito-pcihost"
  204. OBJECT_DECLARE_SIMPLE_TYPE(BonitoState, BONITO_PCI_HOST_BRIDGE)
  205. #define TYPE_PCI_BONITO "Bonito"
  206. OBJECT_DECLARE_SIMPLE_TYPE(PCIBonitoState, PCI_BONITO)
  207. static void bonito_writel(void *opaque, hwaddr addr,
  208. uint64_t val, unsigned size)
  209. {
  210. PCIBonitoState *s = opaque;
  211. uint32_t saddr;
  212. int reset = 0;
  213. saddr = addr >> 2;
  214. DPRINTF("bonito_writel "TARGET_FMT_plx" val %lx saddr %x\n",
  215. addr, val, saddr);
  216. switch (saddr) {
  217. case BONITO_BONPONCFG:
  218. case BONITO_IODEVCFG:
  219. case BONITO_SDCFG:
  220. case BONITO_PCIMAP:
  221. case BONITO_PCIMEMBASECFG:
  222. case BONITO_PCIMAP_CFG:
  223. case BONITO_GPIODATA:
  224. case BONITO_GPIOIE:
  225. case BONITO_INTEDGE:
  226. case BONITO_INTSTEER:
  227. case BONITO_INTPOL:
  228. case BONITO_PCIMAIL0:
  229. case BONITO_PCIMAIL1:
  230. case BONITO_PCIMAIL2:
  231. case BONITO_PCIMAIL3:
  232. case BONITO_PCICACHECTRL:
  233. case BONITO_PCICACHETAG:
  234. case BONITO_PCIBADADDR:
  235. case BONITO_PCIMSTAT:
  236. case BONITO_TIMECFG:
  237. case BONITO_CPUCFG:
  238. case BONITO_DQCFG:
  239. case BONITO_MEMSIZE:
  240. s->regs[saddr] = val;
  241. break;
  242. case BONITO_BONGENCFG:
  243. if (!(s->regs[saddr] & 0x04) && (val & 0x04)) {
  244. reset = 1; /* bit 2 jump from 0 to 1 cause reset */
  245. }
  246. s->regs[saddr] = val;
  247. if (reset) {
  248. qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
  249. }
  250. break;
  251. case BONITO_INTENSET:
  252. s->regs[BONITO_INTENSET] = val;
  253. s->regs[BONITO_INTEN] |= val;
  254. break;
  255. case BONITO_INTENCLR:
  256. s->regs[BONITO_INTENCLR] = val;
  257. s->regs[BONITO_INTEN] &= ~val;
  258. break;
  259. case BONITO_INTEN:
  260. case BONITO_INTISR:
  261. DPRINTF("write to readonly bonito register %x\n", saddr);
  262. break;
  263. default:
  264. DPRINTF("write to unknown bonito register %x\n", saddr);
  265. break;
  266. }
  267. }
  268. static uint64_t bonito_readl(void *opaque, hwaddr addr,
  269. unsigned size)
  270. {
  271. PCIBonitoState *s = opaque;
  272. uint32_t saddr;
  273. saddr = addr >> 2;
  274. DPRINTF("bonito_readl "TARGET_FMT_plx"\n", addr);
  275. switch (saddr) {
  276. case BONITO_INTISR:
  277. return s->regs[saddr];
  278. default:
  279. return s->regs[saddr];
  280. }
  281. }
  282. static const MemoryRegionOps bonito_ops = {
  283. .read = bonito_readl,
  284. .write = bonito_writel,
  285. .endianness = DEVICE_NATIVE_ENDIAN,
  286. .valid = {
  287. .min_access_size = 4,
  288. .max_access_size = 4,
  289. },
  290. };
  291. static void bonito_pciconf_writel(void *opaque, hwaddr addr,
  292. uint64_t val, unsigned size)
  293. {
  294. PCIBonitoState *s = opaque;
  295. PCIDevice *d = PCI_DEVICE(s);
  296. DPRINTF("bonito_pciconf_writel "TARGET_FMT_plx" val %lx\n", addr, val);
  297. d->config_write(d, addr, val, 4);
  298. }
  299. static uint64_t bonito_pciconf_readl(void *opaque, hwaddr addr,
  300. unsigned size)
  301. {
  302. PCIBonitoState *s = opaque;
  303. PCIDevice *d = PCI_DEVICE(s);
  304. DPRINTF("bonito_pciconf_readl "TARGET_FMT_plx"\n", addr);
  305. return d->config_read(d, addr, 4);
  306. }
  307. /* north bridge PCI configure space. 0x1fe0 0000 - 0x1fe0 00ff */
  308. static const MemoryRegionOps bonito_pciconf_ops = {
  309. .read = bonito_pciconf_readl,
  310. .write = bonito_pciconf_writel,
  311. .endianness = DEVICE_NATIVE_ENDIAN,
  312. .valid = {
  313. .min_access_size = 4,
  314. .max_access_size = 4,
  315. },
  316. };
  317. static uint64_t bonito_ldma_readl(void *opaque, hwaddr addr,
  318. unsigned size)
  319. {
  320. uint32_t val;
  321. PCIBonitoState *s = opaque;
  322. if (addr >= sizeof(s->bonldma)) {
  323. return 0;
  324. }
  325. val = ((uint32_t *)(&s->bonldma))[addr / sizeof(uint32_t)];
  326. return val;
  327. }
  328. static void bonito_ldma_writel(void *opaque, hwaddr addr,
  329. uint64_t val, unsigned size)
  330. {
  331. PCIBonitoState *s = opaque;
  332. if (addr >= sizeof(s->bonldma)) {
  333. return;
  334. }
  335. ((uint32_t *)(&s->bonldma))[addr / sizeof(uint32_t)] = val & 0xffffffff;
  336. }
  337. static const MemoryRegionOps bonito_ldma_ops = {
  338. .read = bonito_ldma_readl,
  339. .write = bonito_ldma_writel,
  340. .endianness = DEVICE_NATIVE_ENDIAN,
  341. .valid = {
  342. .min_access_size = 4,
  343. .max_access_size = 4,
  344. },
  345. };
  346. static uint64_t bonito_cop_readl(void *opaque, hwaddr addr,
  347. unsigned size)
  348. {
  349. uint32_t val;
  350. PCIBonitoState *s = opaque;
  351. if (addr >= sizeof(s->boncop)) {
  352. return 0;
  353. }
  354. val = ((uint32_t *)(&s->boncop))[addr / sizeof(uint32_t)];
  355. return val;
  356. }
  357. static void bonito_cop_writel(void *opaque, hwaddr addr,
  358. uint64_t val, unsigned size)
  359. {
  360. PCIBonitoState *s = opaque;
  361. if (addr >= sizeof(s->boncop)) {
  362. return;
  363. }
  364. ((uint32_t *)(&s->boncop))[addr / sizeof(uint32_t)] = val & 0xffffffff;
  365. }
  366. static const MemoryRegionOps bonito_cop_ops = {
  367. .read = bonito_cop_readl,
  368. .write = bonito_cop_writel,
  369. .endianness = DEVICE_NATIVE_ENDIAN,
  370. .valid = {
  371. .min_access_size = 4,
  372. .max_access_size = 4,
  373. },
  374. };
  375. static uint32_t bonito_sbridge_pciaddr(void *opaque, hwaddr addr)
  376. {
  377. PCIBonitoState *s = opaque;
  378. PCIHostState *phb = PCI_HOST_BRIDGE(s->pcihost);
  379. uint32_t cfgaddr;
  380. uint32_t idsel;
  381. uint32_t devno;
  382. uint32_t funno;
  383. uint32_t regno;
  384. uint32_t pciaddr;
  385. /* support type0 pci config */
  386. if ((s->regs[BONITO_PCIMAP_CFG] & 0x10000) != 0x0) {
  387. return 0xffffffff;
  388. }
  389. cfgaddr = addr & 0xffff;
  390. cfgaddr |= (s->regs[BONITO_PCIMAP_CFG] & 0xffff) << 16;
  391. idsel = (cfgaddr & BONITO_PCICONF_IDSEL_MASK) >>
  392. BONITO_PCICONF_IDSEL_OFFSET;
  393. devno = ctz32(idsel);
  394. funno = (cfgaddr & BONITO_PCICONF_FUN_MASK) >> BONITO_PCICONF_FUN_OFFSET;
  395. regno = (cfgaddr & BONITO_PCICONF_REG_MASK_HW) >> BONITO_PCICONF_REG_OFFSET;
  396. if (idsel == 0) {
  397. error_report("error in bonito pci config address 0x" TARGET_FMT_plx
  398. ",pcimap_cfg=0x%x", addr, s->regs[BONITO_PCIMAP_CFG]);
  399. exit(1);
  400. }
  401. pciaddr = PCI_ADDR(pci_bus_num(phb->bus), devno, funno, regno);
  402. DPRINTF("cfgaddr %x pciaddr %x busno %x devno %d funno %d regno %d\n",
  403. cfgaddr, pciaddr, pci_bus_num(phb->bus), devno, funno, regno);
  404. return pciaddr;
  405. }
  406. static void bonito_spciconf_write(void *opaque, hwaddr addr, uint64_t val,
  407. unsigned size)
  408. {
  409. PCIBonitoState *s = opaque;
  410. PCIDevice *d = PCI_DEVICE(s);
  411. PCIHostState *phb = PCI_HOST_BRIDGE(s->pcihost);
  412. uint32_t pciaddr;
  413. uint16_t status;
  414. DPRINTF("bonito_spciconf_write "TARGET_FMT_plx" size %d val %lx\n",
  415. addr, size, val);
  416. pciaddr = bonito_sbridge_pciaddr(s, addr);
  417. if (pciaddr == 0xffffffff) {
  418. return;
  419. }
  420. if (addr & ~BONITO_PCICONF_REG_MASK_DS) {
  421. trace_bonito_spciconf_small_access(addr, size);
  422. }
  423. /* set the pci address in s->config_reg */
  424. phb->config_reg = (pciaddr) | (1u << 31);
  425. pci_data_write(phb->bus, phb->config_reg, val, size);
  426. /* clear PCI_STATUS_REC_MASTER_ABORT and PCI_STATUS_REC_TARGET_ABORT */
  427. status = pci_get_word(d->config + PCI_STATUS);
  428. status &= ~(PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT);
  429. pci_set_word(d->config + PCI_STATUS, status);
  430. }
  431. static uint64_t bonito_spciconf_read(void *opaque, hwaddr addr, unsigned size)
  432. {
  433. PCIBonitoState *s = opaque;
  434. PCIDevice *d = PCI_DEVICE(s);
  435. PCIHostState *phb = PCI_HOST_BRIDGE(s->pcihost);
  436. uint32_t pciaddr;
  437. uint16_t status;
  438. DPRINTF("bonito_spciconf_read "TARGET_FMT_plx" size %d\n", addr, size);
  439. pciaddr = bonito_sbridge_pciaddr(s, addr);
  440. if (pciaddr == 0xffffffff) {
  441. return MAKE_64BIT_MASK(0, size * 8);
  442. }
  443. if (addr & ~BONITO_PCICONF_REG_MASK_DS) {
  444. trace_bonito_spciconf_small_access(addr, size);
  445. }
  446. /* set the pci address in s->config_reg */
  447. phb->config_reg = (pciaddr) | (1u << 31);
  448. /* clear PCI_STATUS_REC_MASTER_ABORT and PCI_STATUS_REC_TARGET_ABORT */
  449. status = pci_get_word(d->config + PCI_STATUS);
  450. status &= ~(PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT);
  451. pci_set_word(d->config + PCI_STATUS, status);
  452. return pci_data_read(phb->bus, phb->config_reg, size);
  453. }
  454. /* south bridge PCI configure space. 0x1fe8 0000 - 0x1fef ffff */
  455. static const MemoryRegionOps bonito_spciconf_ops = {
  456. .read = bonito_spciconf_read,
  457. .write = bonito_spciconf_write,
  458. .valid.min_access_size = 1,
  459. .valid.max_access_size = 4,
  460. .impl.min_access_size = 1,
  461. .impl.max_access_size = 4,
  462. .endianness = DEVICE_NATIVE_ENDIAN,
  463. };
  464. #define BONITO_IRQ_BASE 32
  465. static void pci_bonito_set_irq(void *opaque, int irq_num, int level)
  466. {
  467. BonitoState *s = opaque;
  468. qemu_irq *pic = s->pic;
  469. PCIBonitoState *bonito_state = s->pci_dev;
  470. int internal_irq = irq_num - BONITO_IRQ_BASE;
  471. if (bonito_state->regs[BONITO_INTEDGE] & (1 << internal_irq)) {
  472. qemu_irq_pulse(*pic);
  473. } else { /* level triggered */
  474. if (bonito_state->regs[BONITO_INTPOL] & (1 << internal_irq)) {
  475. qemu_irq_raise(*pic);
  476. } else {
  477. qemu_irq_lower(*pic);
  478. }
  479. }
  480. }
  481. /* map the original irq (0~3) to bonito irq (16~47, but 16~31 are unused) */
  482. static int pci_bonito_map_irq(PCIDevice *pci_dev, int irq_num)
  483. {
  484. int slot;
  485. slot = PCI_SLOT(pci_dev->devfn);
  486. switch (slot) {
  487. case 5: /* FULOONG2E_VIA_SLOT, SouthBridge, IDE, USB, ACPI, AC97, MC97 */
  488. return irq_num % 4 + BONITO_IRQ_BASE;
  489. case 6: /* FULOONG2E_ATI_SLOT, VGA */
  490. return 4 + BONITO_IRQ_BASE;
  491. case 7: /* FULOONG2E_RTL_SLOT, RTL8139 */
  492. return 5 + BONITO_IRQ_BASE;
  493. case 8 ... 12: /* PCI slot 1 to 4 */
  494. return (slot - 8 + irq_num) + 6 + BONITO_IRQ_BASE;
  495. default: /* Unknown device, don't do any translation */
  496. return irq_num;
  497. }
  498. }
  499. static void bonito_reset(void *opaque)
  500. {
  501. PCIBonitoState *s = opaque;
  502. uint32_t val = 0;
  503. /* set the default value of north bridge registers */
  504. s->regs[BONITO_BONPONCFG] = 0xc40;
  505. val = FIELD_DP32(val, BONGENCFG, PCIQUEUE, 1);
  506. val = FIELD_DP32(val, BONGENCFG, WRITEBEHIND, 1);
  507. val = FIELD_DP32(val, BONGENCFG, PREFETCH, 1);
  508. val = FIELD_DP32(val, BONGENCFG, UNCACHED, 1);
  509. val = FIELD_DP32(val, BONGENCFG, CPUSELFRESET, 1);
  510. s->regs[BONITO_BONGENCFG] = val;
  511. s->regs[BONITO_IODEVCFG] = 0x2bff8010;
  512. s->regs[BONITO_SDCFG] = 0x255e0091;
  513. s->regs[BONITO_GPIODATA] = 0x1ff;
  514. s->regs[BONITO_GPIOIE] = 0x1ff;
  515. s->regs[BONITO_DQCFG] = 0x8;
  516. s->regs[BONITO_MEMSIZE] = 0x10000000;
  517. s->regs[BONITO_PCIMAP] = 0x6140;
  518. }
  519. static const VMStateDescription vmstate_bonito = {
  520. .name = "Bonito",
  521. .version_id = 1,
  522. .minimum_version_id = 1,
  523. .fields = (VMStateField[]) {
  524. VMSTATE_PCI_DEVICE(dev, PCIBonitoState),
  525. VMSTATE_END_OF_LIST()
  526. }
  527. };
  528. static void bonito_pcihost_realize(DeviceState *dev, Error **errp)
  529. {
  530. PCIHostState *phb = PCI_HOST_BRIDGE(dev);
  531. BonitoState *bs = BONITO_PCI_HOST_BRIDGE(dev);
  532. MemoryRegion *pcimem_lo_alias = g_new(MemoryRegion, 3);
  533. memory_region_init(&bs->pci_mem, OBJECT(dev), "pci.mem", BONITO_PCIHI_SIZE);
  534. phb->bus = pci_register_root_bus(dev, "pci",
  535. pci_bonito_set_irq, pci_bonito_map_irq,
  536. dev, &bs->pci_mem, get_system_io(),
  537. PCI_DEVFN(5, 0), 32, TYPE_PCI_BUS);
  538. for (size_t i = 0; i < 3; i++) {
  539. char *name = g_strdup_printf("pci.lomem%zu", i);
  540. memory_region_init_alias(&pcimem_lo_alias[i], NULL, name,
  541. &bs->pci_mem, i * 64 * MiB, 64 * MiB);
  542. memory_region_add_subregion(get_system_memory(),
  543. BONITO_PCILO_BASE + i * 64 * MiB,
  544. &pcimem_lo_alias[i]);
  545. g_free(name);
  546. }
  547. create_unimplemented_device("pci.io", BONITO_PCIIO_BASE, 1 * MiB);
  548. }
  549. static void bonito_realize(PCIDevice *dev, Error **errp)
  550. {
  551. PCIBonitoState *s = PCI_BONITO(dev);
  552. SysBusDevice *sysbus = SYS_BUS_DEVICE(s->pcihost);
  553. PCIHostState *phb = PCI_HOST_BRIDGE(s->pcihost);
  554. BonitoState *bs = BONITO_PCI_HOST_BRIDGE(s->pcihost);
  555. MemoryRegion *pcimem_alias = g_new(MemoryRegion, 1);
  556. /*
  557. * Bonito North Bridge, built on FPGA,
  558. * VENDOR_ID/DEVICE_ID are "undefined"
  559. */
  560. pci_config_set_prog_interface(dev->config, 0x00);
  561. /* set the north bridge register mapping */
  562. memory_region_init_io(&s->iomem, OBJECT(s), &bonito_ops, s,
  563. "north-bridge-register", BONITO_INTERNAL_REG_SIZE);
  564. sysbus_init_mmio(sysbus, &s->iomem);
  565. sysbus_mmio_map(sysbus, 0, BONITO_INTERNAL_REG_BASE);
  566. /* set the north bridge pci configure mapping */
  567. memory_region_init_io(&phb->conf_mem, OBJECT(s), &bonito_pciconf_ops, s,
  568. "north-bridge-pci-config", BONITO_PCICONFIG_SIZE);
  569. sysbus_init_mmio(sysbus, &phb->conf_mem);
  570. sysbus_mmio_map(sysbus, 1, BONITO_PCICONFIG_BASE);
  571. /* set the south bridge pci configure mapping */
  572. memory_region_init_io(&phb->data_mem, OBJECT(s), &bonito_spciconf_ops, s,
  573. "south-bridge-pci-config", BONITO_SPCICONFIG_SIZE);
  574. sysbus_init_mmio(sysbus, &phb->data_mem);
  575. sysbus_mmio_map(sysbus, 2, BONITO_SPCICONFIG_BASE);
  576. create_unimplemented_device("bonito", BONITO_REG_BASE, BONITO_REG_SIZE);
  577. memory_region_init_io(&s->iomem_ldma, OBJECT(s), &bonito_ldma_ops, s,
  578. "ldma", 0x100);
  579. sysbus_init_mmio(sysbus, &s->iomem_ldma);
  580. sysbus_mmio_map(sysbus, 3, 0x1fe00200);
  581. /* PCI copier */
  582. memory_region_init_io(&s->iomem_cop, OBJECT(s), &bonito_cop_ops, s,
  583. "cop", 0x100);
  584. sysbus_init_mmio(sysbus, &s->iomem_cop);
  585. sysbus_mmio_map(sysbus, 4, 0x1fe00300);
  586. create_unimplemented_device("ROMCS", BONITO_FLASH_BASE, 60 * MiB);
  587. /* Map PCI IO Space 0x1fd0 0000 - 0x1fd1 0000 */
  588. memory_region_init_alias(&s->bonito_pciio, OBJECT(s), "isa_mmio",
  589. get_system_io(), 0, BONITO_PCIIO_SIZE);
  590. sysbus_init_mmio(sysbus, &s->bonito_pciio);
  591. sysbus_mmio_map(sysbus, 5, BONITO_PCIIO_BASE);
  592. /* add pci local io mapping */
  593. memory_region_init_alias(&s->bonito_localio, OBJECT(s), "IOCS[0]",
  594. get_system_io(), 0, 256 * KiB);
  595. sysbus_init_mmio(sysbus, &s->bonito_localio);
  596. sysbus_mmio_map(sysbus, 6, BONITO_DEV_BASE);
  597. create_unimplemented_device("IOCS[1]", BONITO_DEV_BASE + 1 * 256 * KiB,
  598. 256 * KiB);
  599. create_unimplemented_device("IOCS[2]", BONITO_DEV_BASE + 2 * 256 * KiB,
  600. 256 * KiB);
  601. create_unimplemented_device("IOCS[3]", BONITO_DEV_BASE + 3 * 256 * KiB,
  602. 256 * KiB);
  603. memory_region_init_alias(pcimem_alias, NULL, "pci.mem.alias",
  604. &bs->pci_mem, 0, BONITO_PCIHI_SIZE);
  605. memory_region_add_subregion(get_system_memory(),
  606. BONITO_PCIHI_BASE, pcimem_alias);
  607. create_unimplemented_device("PCI_2",
  608. (hwaddr)BONITO_PCIHI_BASE + BONITO_PCIHI_SIZE,
  609. 2 * GiB);
  610. /* set the default value of north bridge pci config */
  611. pci_set_word(dev->config + PCI_COMMAND, 0x0000);
  612. pci_set_word(dev->config + PCI_STATUS, 0x0000);
  613. pci_set_word(dev->config + PCI_SUBSYSTEM_VENDOR_ID, 0x0000);
  614. pci_set_word(dev->config + PCI_SUBSYSTEM_ID, 0x0000);
  615. pci_set_byte(dev->config + PCI_INTERRUPT_LINE, 0x00);
  616. pci_config_set_interrupt_pin(dev->config, 0x01); /* interrupt pin A */
  617. pci_set_byte(dev->config + PCI_MIN_GNT, 0x3c);
  618. pci_set_byte(dev->config + PCI_MAX_LAT, 0x00);
  619. qemu_register_reset(bonito_reset, s);
  620. }
  621. PCIBus *bonito_init(qemu_irq *pic)
  622. {
  623. DeviceState *dev;
  624. BonitoState *pcihost;
  625. PCIHostState *phb;
  626. PCIBonitoState *s;
  627. PCIDevice *d;
  628. dev = qdev_new(TYPE_BONITO_PCI_HOST_BRIDGE);
  629. phb = PCI_HOST_BRIDGE(dev);
  630. pcihost = BONITO_PCI_HOST_BRIDGE(dev);
  631. pcihost->pic = pic;
  632. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  633. d = pci_new(PCI_DEVFN(0, 0), TYPE_PCI_BONITO);
  634. s = PCI_BONITO(d);
  635. s->pcihost = pcihost;
  636. pcihost->pci_dev = s;
  637. pci_realize_and_unref(d, phb->bus, &error_fatal);
  638. return phb->bus;
  639. }
  640. static void bonito_class_init(ObjectClass *klass, void *data)
  641. {
  642. DeviceClass *dc = DEVICE_CLASS(klass);
  643. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  644. k->realize = bonito_realize;
  645. k->vendor_id = 0xdf53;
  646. k->device_id = 0x00d5;
  647. k->revision = 0x01;
  648. k->class_id = PCI_CLASS_BRIDGE_HOST;
  649. dc->desc = "Host bridge";
  650. dc->vmsd = &vmstate_bonito;
  651. /*
  652. * PCI-facing part of the host bridge, not usable without the
  653. * host-facing part, which can't be device_add'ed, yet.
  654. */
  655. dc->user_creatable = false;
  656. }
  657. static const TypeInfo bonito_info = {
  658. .name = TYPE_PCI_BONITO,
  659. .parent = TYPE_PCI_DEVICE,
  660. .instance_size = sizeof(PCIBonitoState),
  661. .class_init = bonito_class_init,
  662. .interfaces = (InterfaceInfo[]) {
  663. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  664. { },
  665. },
  666. };
  667. static void bonito_pcihost_class_init(ObjectClass *klass, void *data)
  668. {
  669. DeviceClass *dc = DEVICE_CLASS(klass);
  670. dc->realize = bonito_pcihost_realize;
  671. }
  672. static const TypeInfo bonito_pcihost_info = {
  673. .name = TYPE_BONITO_PCI_HOST_BRIDGE,
  674. .parent = TYPE_PCI_HOST_BRIDGE,
  675. .instance_size = sizeof(BonitoState),
  676. .class_init = bonito_pcihost_class_init,
  677. };
  678. static void bonito_register_types(void)
  679. {
  680. type_register_static(&bonito_pcihost_info);
  681. type_register_static(&bonito_info);
  682. }
  683. type_init(bonito_register_types)