2
0

apb.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. /*
  2. * QEMU Ultrasparc APB PCI host
  3. *
  4. * Copyright (c) 2006 Fabrice Bellard
  5. * Copyright (c) 2012,2013 Artyom Tarasenko
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. /* XXX This file and most of its contents are somewhat misnamed. The
  26. Ultrasparc PCI host is called the PCI Bus Module (PBM). The APB is
  27. the secondary PCI bridge. */
  28. #include "hw/sysbus.h"
  29. #include "hw/pci/pci.h"
  30. #include "hw/pci/pci_host.h"
  31. #include "hw/pci/pci_bridge.h"
  32. #include "hw/pci/pci_bus.h"
  33. #include "hw/pci-host/apb.h"
  34. #include "sysemu/sysemu.h"
  35. #include "exec/address-spaces.h"
  36. /* debug APB */
  37. //#define DEBUG_APB
  38. #ifdef DEBUG_APB
  39. #define APB_DPRINTF(fmt, ...) \
  40. do { printf("APB: " fmt , ## __VA_ARGS__); } while (0)
  41. #else
  42. #define APB_DPRINTF(fmt, ...)
  43. #endif
  44. /* debug IOMMU */
  45. //#define DEBUG_IOMMU
  46. #ifdef DEBUG_IOMMU
  47. #define IOMMU_DPRINTF(fmt, ...) \
  48. do { printf("IOMMU: " fmt , ## __VA_ARGS__); } while (0)
  49. #else
  50. #define IOMMU_DPRINTF(fmt, ...)
  51. #endif
  52. /*
  53. * Chipset docs:
  54. * PBM: "UltraSPARC IIi User's Manual",
  55. * http://www.sun.com/processors/manuals/805-0087.pdf
  56. *
  57. * APB: "Advanced PCI Bridge (APB) User's Manual",
  58. * http://www.sun.com/processors/manuals/805-1251.pdf
  59. */
  60. #define PBM_PCI_IMR_MASK 0x7fffffff
  61. #define PBM_PCI_IMR_ENABLED 0x80000000
  62. #define POR (1U << 31)
  63. #define SOFT_POR (1U << 30)
  64. #define SOFT_XIR (1U << 29)
  65. #define BTN_POR (1U << 28)
  66. #define BTN_XIR (1U << 27)
  67. #define RESET_MASK 0xf8000000
  68. #define RESET_WCMASK 0x98000000
  69. #define RESET_WMASK 0x60000000
  70. #define MAX_IVEC 0x40
  71. #define NO_IRQ_REQUEST (MAX_IVEC + 1)
  72. #define IOMMU_PAGE_SIZE_8K (1ULL << 13)
  73. #define IOMMU_PAGE_MASK_8K (~(IOMMU_PAGE_SIZE_8K - 1))
  74. #define IOMMU_PAGE_SIZE_64K (1ULL << 16)
  75. #define IOMMU_PAGE_MASK_64K (~(IOMMU_PAGE_SIZE_64K - 1))
  76. #define IOMMU_NREGS 3
  77. #define IOMMU_CTRL 0x0
  78. #define IOMMU_CTRL_TBW_SIZE (1ULL << 2)
  79. #define IOMMU_CTRL_MMU_EN (1ULL)
  80. #define IOMMU_CTRL_TSB_SHIFT 16
  81. #define IOMMU_BASE 0x8
  82. #define IOMMU_TTE_DATA_V (1ULL << 63)
  83. #define IOMMU_TTE_DATA_SIZE (1ULL << 61)
  84. #define IOMMU_TTE_DATA_W (1ULL << 1)
  85. #define IOMMU_TTE_PHYS_MASK_8K 0x1ffffffe000ULL
  86. #define IOMMU_TTE_PHYS_MASK_64K 0x1ffffff8000ULL
  87. #define IOMMU_TSB_8K_OFFSET_MASK_8M 0x00000000007fe000ULL
  88. #define IOMMU_TSB_8K_OFFSET_MASK_16M 0x0000000000ffe000ULL
  89. #define IOMMU_TSB_8K_OFFSET_MASK_32M 0x0000000001ffe000ULL
  90. #define IOMMU_TSB_8K_OFFSET_MASK_64M 0x0000000003ffe000ULL
  91. #define IOMMU_TSB_8K_OFFSET_MASK_128M 0x0000000007ffe000ULL
  92. #define IOMMU_TSB_8K_OFFSET_MASK_256M 0x000000000fffe000ULL
  93. #define IOMMU_TSB_8K_OFFSET_MASK_512M 0x000000001fffe000ULL
  94. #define IOMMU_TSB_8K_OFFSET_MASK_1G 0x000000003fffe000ULL
  95. #define IOMMU_TSB_64K_OFFSET_MASK_64M 0x0000000003ff0000ULL
  96. #define IOMMU_TSB_64K_OFFSET_MASK_128M 0x0000000007ff0000ULL
  97. #define IOMMU_TSB_64K_OFFSET_MASK_256M 0x000000000fff0000ULL
  98. #define IOMMU_TSB_64K_OFFSET_MASK_512M 0x000000001fff0000ULL
  99. #define IOMMU_TSB_64K_OFFSET_MASK_1G 0x000000003fff0000ULL
  100. #define IOMMU_TSB_64K_OFFSET_MASK_2G 0x000000007fff0000ULL
  101. typedef struct IOMMUState {
  102. AddressSpace iommu_as;
  103. MemoryRegion iommu;
  104. uint64_t regs[IOMMU_NREGS];
  105. } IOMMUState;
  106. #define TYPE_APB "pbm"
  107. #define APB_DEVICE(obj) \
  108. OBJECT_CHECK(APBState, (obj), TYPE_APB)
  109. typedef struct APBState {
  110. PCIHostState parent_obj;
  111. MemoryRegion apb_config;
  112. MemoryRegion pci_config;
  113. MemoryRegion pci_mmio;
  114. MemoryRegion pci_ioport;
  115. uint64_t pci_irq_in;
  116. IOMMUState iommu;
  117. uint32_t pci_control[16];
  118. uint32_t pci_irq_map[8];
  119. uint32_t obio_irq_map[32];
  120. qemu_irq *pbm_irqs;
  121. qemu_irq *ivec_irqs;
  122. unsigned int irq_request;
  123. uint32_t reset_control;
  124. unsigned int nr_resets;
  125. } APBState;
  126. static inline void pbm_set_request(APBState *s, unsigned int irq_num)
  127. {
  128. APB_DPRINTF("%s: request irq %d\n", __func__, irq_num);
  129. s->irq_request = irq_num;
  130. qemu_set_irq(s->ivec_irqs[irq_num], 1);
  131. }
  132. static inline void pbm_check_irqs(APBState *s)
  133. {
  134. unsigned int i;
  135. /* Previous request is not acknowledged, resubmit */
  136. if (s->irq_request != NO_IRQ_REQUEST) {
  137. pbm_set_request(s, s->irq_request);
  138. return;
  139. }
  140. /* no request pending */
  141. if (s->pci_irq_in == 0ULL) {
  142. return;
  143. }
  144. for (i = 0; i < 32; i++) {
  145. if (s->pci_irq_in & (1ULL << i)) {
  146. if (s->pci_irq_map[i >> 2] & PBM_PCI_IMR_ENABLED) {
  147. pbm_set_request(s, i);
  148. return;
  149. }
  150. }
  151. }
  152. for (i = 32; i < 64; i++) {
  153. if (s->pci_irq_in & (1ULL << i)) {
  154. if (s->obio_irq_map[i - 32] & PBM_PCI_IMR_ENABLED) {
  155. pbm_set_request(s, i);
  156. break;
  157. }
  158. }
  159. }
  160. }
  161. static inline void pbm_clear_request(APBState *s, unsigned int irq_num)
  162. {
  163. APB_DPRINTF("%s: clear request irq %d\n", __func__, irq_num);
  164. qemu_set_irq(s->ivec_irqs[irq_num], 0);
  165. s->irq_request = NO_IRQ_REQUEST;
  166. }
  167. static AddressSpace *pbm_pci_dma_iommu(PCIBus *bus, void *opaque, int devfn)
  168. {
  169. IOMMUState *is = opaque;
  170. return &is->iommu_as;
  171. }
  172. static IOMMUTLBEntry pbm_translate_iommu(MemoryRegion *iommu, hwaddr addr)
  173. {
  174. IOMMUState *is = container_of(iommu, IOMMUState, iommu);
  175. hwaddr baseaddr, offset;
  176. uint64_t tte;
  177. uint32_t tsbsize;
  178. IOMMUTLBEntry ret = {
  179. .target_as = &address_space_memory,
  180. .iova = 0,
  181. .translated_addr = 0,
  182. .addr_mask = ~(hwaddr)0,
  183. .perm = IOMMU_NONE,
  184. };
  185. if (!(is->regs[IOMMU_CTRL >> 3] & IOMMU_CTRL_MMU_EN)) {
  186. /* IOMMU disabled, passthrough using standard 8K page */
  187. ret.iova = addr & IOMMU_PAGE_MASK_8K;
  188. ret.translated_addr = addr;
  189. ret.addr_mask = IOMMU_PAGE_MASK_8K;
  190. ret.perm = IOMMU_RW;
  191. return ret;
  192. }
  193. baseaddr = is->regs[IOMMU_BASE >> 3];
  194. tsbsize = (is->regs[IOMMU_CTRL >> 3] >> IOMMU_CTRL_TSB_SHIFT) & 0x7;
  195. if (is->regs[IOMMU_CTRL >> 3] & IOMMU_CTRL_TBW_SIZE) {
  196. /* 64K */
  197. switch (tsbsize) {
  198. case 0:
  199. offset = (addr & IOMMU_TSB_64K_OFFSET_MASK_64M) >> 13;
  200. break;
  201. case 1:
  202. offset = (addr & IOMMU_TSB_64K_OFFSET_MASK_128M) >> 13;
  203. break;
  204. case 2:
  205. offset = (addr & IOMMU_TSB_64K_OFFSET_MASK_256M) >> 13;
  206. break;
  207. case 3:
  208. offset = (addr & IOMMU_TSB_64K_OFFSET_MASK_512M) >> 13;
  209. break;
  210. case 4:
  211. offset = (addr & IOMMU_TSB_64K_OFFSET_MASK_1G) >> 13;
  212. break;
  213. case 5:
  214. offset = (addr & IOMMU_TSB_64K_OFFSET_MASK_2G) >> 13;
  215. break;
  216. default:
  217. /* Not implemented, error */
  218. return ret;
  219. }
  220. } else {
  221. /* 8K */
  222. switch (tsbsize) {
  223. case 0:
  224. offset = (addr & IOMMU_TSB_8K_OFFSET_MASK_8M) >> 10;
  225. break;
  226. case 1:
  227. offset = (addr & IOMMU_TSB_8K_OFFSET_MASK_16M) >> 10;
  228. break;
  229. case 2:
  230. offset = (addr & IOMMU_TSB_8K_OFFSET_MASK_32M) >> 10;
  231. break;
  232. case 3:
  233. offset = (addr & IOMMU_TSB_8K_OFFSET_MASK_64M) >> 10;
  234. break;
  235. case 4:
  236. offset = (addr & IOMMU_TSB_8K_OFFSET_MASK_128M) >> 10;
  237. break;
  238. case 5:
  239. offset = (addr & IOMMU_TSB_8K_OFFSET_MASK_256M) >> 10;
  240. break;
  241. case 6:
  242. offset = (addr & IOMMU_TSB_8K_OFFSET_MASK_512M) >> 10;
  243. break;
  244. case 7:
  245. offset = (addr & IOMMU_TSB_8K_OFFSET_MASK_1G) >> 10;
  246. break;
  247. }
  248. }
  249. tte = ldq_be_phys(&address_space_memory, baseaddr + offset);
  250. if (!(tte & IOMMU_TTE_DATA_V)) {
  251. /* Invalid mapping */
  252. return ret;
  253. }
  254. if (tte & IOMMU_TTE_DATA_W) {
  255. /* Writeable */
  256. ret.perm = IOMMU_RW;
  257. } else {
  258. ret.perm = IOMMU_RO;
  259. }
  260. /* Extract phys */
  261. if (tte & IOMMU_TTE_DATA_SIZE) {
  262. /* 64K */
  263. ret.iova = addr & IOMMU_PAGE_MASK_64K;
  264. ret.translated_addr = tte & IOMMU_TTE_PHYS_MASK_64K;
  265. ret.addr_mask = (IOMMU_PAGE_SIZE_64K - 1);
  266. } else {
  267. /* 8K */
  268. ret.iova = addr & IOMMU_PAGE_MASK_8K;
  269. ret.translated_addr = tte & IOMMU_TTE_PHYS_MASK_8K;
  270. ret.addr_mask = (IOMMU_PAGE_SIZE_8K - 1);
  271. }
  272. return ret;
  273. }
  274. static MemoryRegionIOMMUOps pbm_iommu_ops = {
  275. .translate = pbm_translate_iommu,
  276. };
  277. static void iommu_config_write(void *opaque, hwaddr addr,
  278. uint64_t val, unsigned size)
  279. {
  280. IOMMUState *is = opaque;
  281. IOMMU_DPRINTF("IOMMU config write: 0x%" HWADDR_PRIx " val: %" PRIx64
  282. " size: %d\n", addr, val, size);
  283. switch (addr) {
  284. case IOMMU_CTRL:
  285. if (size == 4) {
  286. is->regs[IOMMU_CTRL >> 3] &= 0xffffffffULL;
  287. is->regs[IOMMU_CTRL >> 3] |= val << 32;
  288. } else {
  289. is->regs[IOMMU_CTRL >> 3] = val;
  290. }
  291. break;
  292. case IOMMU_CTRL + 0x4:
  293. is->regs[IOMMU_CTRL >> 3] &= 0xffffffff00000000ULL;
  294. is->regs[IOMMU_CTRL >> 3] |= val & 0xffffffffULL;
  295. break;
  296. case IOMMU_BASE:
  297. if (size == 4) {
  298. is->regs[IOMMU_BASE >> 3] &= 0xffffffffULL;
  299. is->regs[IOMMU_BASE >> 3] |= val << 32;
  300. } else {
  301. is->regs[IOMMU_BASE >> 3] = val;
  302. }
  303. break;
  304. case IOMMU_BASE + 0x4:
  305. is->regs[IOMMU_BASE >> 3] &= 0xffffffff00000000ULL;
  306. is->regs[IOMMU_BASE >> 3] |= val & 0xffffffffULL;
  307. break;
  308. default:
  309. qemu_log_mask(LOG_UNIMP,
  310. "apb iommu: Unimplemented register write "
  311. "reg 0x%" HWADDR_PRIx " size 0x%x value 0x%" PRIx64 "\n",
  312. addr, size, val);
  313. break;
  314. }
  315. }
  316. static uint64_t iommu_config_read(void *opaque, hwaddr addr, unsigned size)
  317. {
  318. IOMMUState *is = opaque;
  319. uint64_t val;
  320. switch (addr) {
  321. case IOMMU_CTRL:
  322. if (size == 4) {
  323. val = is->regs[IOMMU_CTRL >> 3] >> 32;
  324. } else {
  325. val = is->regs[IOMMU_CTRL >> 3];
  326. }
  327. break;
  328. case IOMMU_CTRL + 0x4:
  329. val = is->regs[IOMMU_CTRL >> 3] & 0xffffffffULL;
  330. break;
  331. case IOMMU_BASE:
  332. if (size == 4) {
  333. val = is->regs[IOMMU_BASE >> 3] >> 32;
  334. } else {
  335. val = is->regs[IOMMU_BASE >> 3];
  336. }
  337. break;
  338. case IOMMU_BASE + 0x4:
  339. val = is->regs[IOMMU_BASE >> 3] & 0xffffffffULL;
  340. break;
  341. default:
  342. qemu_log_mask(LOG_UNIMP,
  343. "apb iommu: Unimplemented register read "
  344. "reg 0x%" HWADDR_PRIx " size 0x%x\n",
  345. addr, size);
  346. val = 0;
  347. break;
  348. }
  349. IOMMU_DPRINTF("IOMMU config read: 0x%" HWADDR_PRIx " val: %" PRIx64
  350. " size: %d\n", addr, val, size);
  351. return val;
  352. }
  353. static void apb_config_writel (void *opaque, hwaddr addr,
  354. uint64_t val, unsigned size)
  355. {
  356. APBState *s = opaque;
  357. IOMMUState *is = &s->iommu;
  358. APB_DPRINTF("%s: addr " TARGET_FMT_plx " val %" PRIx64 "\n", __func__, addr, val);
  359. switch (addr & 0xffff) {
  360. case 0x30 ... 0x4f: /* DMA error registers */
  361. /* XXX: not implemented yet */
  362. break;
  363. case 0x200 ... 0x217: /* IOMMU */
  364. iommu_config_write(is, (addr & 0xf), val, size);
  365. break;
  366. case 0xc00 ... 0xc3f: /* PCI interrupt control */
  367. if (addr & 4) {
  368. unsigned int ino = (addr & 0x3f) >> 3;
  369. s->pci_irq_map[ino] &= PBM_PCI_IMR_MASK;
  370. s->pci_irq_map[ino] |= val & ~PBM_PCI_IMR_MASK;
  371. if ((s->irq_request == ino) && !(val & ~PBM_PCI_IMR_MASK)) {
  372. pbm_clear_request(s, ino);
  373. }
  374. pbm_check_irqs(s);
  375. }
  376. break;
  377. case 0x1000 ... 0x1080: /* OBIO interrupt control */
  378. if (addr & 4) {
  379. unsigned int ino = ((addr & 0xff) >> 3);
  380. s->obio_irq_map[ino] &= PBM_PCI_IMR_MASK;
  381. s->obio_irq_map[ino] |= val & ~PBM_PCI_IMR_MASK;
  382. if ((s->irq_request == (ino | 0x20))
  383. && !(val & ~PBM_PCI_IMR_MASK)) {
  384. pbm_clear_request(s, ino | 0x20);
  385. }
  386. pbm_check_irqs(s);
  387. }
  388. break;
  389. case 0x1400 ... 0x14ff: /* PCI interrupt clear */
  390. if (addr & 4) {
  391. unsigned int ino = (addr & 0xff) >> 5;
  392. if ((s->irq_request / 4) == ino) {
  393. pbm_clear_request(s, s->irq_request);
  394. pbm_check_irqs(s);
  395. }
  396. }
  397. break;
  398. case 0x1800 ... 0x1860: /* OBIO interrupt clear */
  399. if (addr & 4) {
  400. unsigned int ino = ((addr & 0xff) >> 3) | 0x20;
  401. if (s->irq_request == ino) {
  402. pbm_clear_request(s, ino);
  403. pbm_check_irqs(s);
  404. }
  405. }
  406. break;
  407. case 0x2000 ... 0x202f: /* PCI control */
  408. s->pci_control[(addr & 0x3f) >> 2] = val;
  409. break;
  410. case 0xf020 ... 0xf027: /* Reset control */
  411. if (addr & 4) {
  412. val &= RESET_MASK;
  413. s->reset_control &= ~(val & RESET_WCMASK);
  414. s->reset_control |= val & RESET_WMASK;
  415. if (val & SOFT_POR) {
  416. s->nr_resets = 0;
  417. qemu_system_reset_request();
  418. } else if (val & SOFT_XIR) {
  419. qemu_system_reset_request();
  420. }
  421. }
  422. break;
  423. case 0x5000 ... 0x51cf: /* PIO/DMA diagnostics */
  424. case 0xa400 ... 0xa67f: /* IOMMU diagnostics */
  425. case 0xa800 ... 0xa80f: /* Interrupt diagnostics */
  426. case 0xf000 ... 0xf01f: /* FFB config, memory control */
  427. /* we don't care */
  428. default:
  429. break;
  430. }
  431. }
  432. static uint64_t apb_config_readl (void *opaque,
  433. hwaddr addr, unsigned size)
  434. {
  435. APBState *s = opaque;
  436. IOMMUState *is = &s->iommu;
  437. uint32_t val;
  438. switch (addr & 0xffff) {
  439. case 0x30 ... 0x4f: /* DMA error registers */
  440. val = 0;
  441. /* XXX: not implemented yet */
  442. break;
  443. case 0x200 ... 0x217: /* IOMMU */
  444. val = iommu_config_read(is, (addr & 0xf), size);
  445. break;
  446. case 0xc00 ... 0xc3f: /* PCI interrupt control */
  447. if (addr & 4) {
  448. val = s->pci_irq_map[(addr & 0x3f) >> 3];
  449. } else {
  450. val = 0;
  451. }
  452. break;
  453. case 0x1000 ... 0x1080: /* OBIO interrupt control */
  454. if (addr & 4) {
  455. val = s->obio_irq_map[(addr & 0xff) >> 3];
  456. } else {
  457. val = 0;
  458. }
  459. break;
  460. case 0x2000 ... 0x202f: /* PCI control */
  461. val = s->pci_control[(addr & 0x3f) >> 2];
  462. break;
  463. case 0xf020 ... 0xf027: /* Reset control */
  464. if (addr & 4) {
  465. val = s->reset_control;
  466. } else {
  467. val = 0;
  468. }
  469. break;
  470. case 0x5000 ... 0x51cf: /* PIO/DMA diagnostics */
  471. case 0xa400 ... 0xa67f: /* IOMMU diagnostics */
  472. case 0xa800 ... 0xa80f: /* Interrupt diagnostics */
  473. case 0xf000 ... 0xf01f: /* FFB config, memory control */
  474. /* we don't care */
  475. default:
  476. val = 0;
  477. break;
  478. }
  479. APB_DPRINTF("%s: addr " TARGET_FMT_plx " -> %x\n", __func__, addr, val);
  480. return val;
  481. }
  482. static const MemoryRegionOps apb_config_ops = {
  483. .read = apb_config_readl,
  484. .write = apb_config_writel,
  485. .endianness = DEVICE_NATIVE_ENDIAN,
  486. };
  487. static void apb_pci_config_write(void *opaque, hwaddr addr,
  488. uint64_t val, unsigned size)
  489. {
  490. APBState *s = opaque;
  491. PCIHostState *phb = PCI_HOST_BRIDGE(s);
  492. val = qemu_bswap_len(val, size);
  493. APB_DPRINTF("%s: addr " TARGET_FMT_plx " val %" PRIx64 "\n", __func__, addr, val);
  494. pci_data_write(phb->bus, addr, val, size);
  495. }
  496. static uint64_t apb_pci_config_read(void *opaque, hwaddr addr,
  497. unsigned size)
  498. {
  499. uint32_t ret;
  500. APBState *s = opaque;
  501. PCIHostState *phb = PCI_HOST_BRIDGE(s);
  502. ret = pci_data_read(phb->bus, addr, size);
  503. ret = qemu_bswap_len(ret, size);
  504. APB_DPRINTF("%s: addr " TARGET_FMT_plx " -> %x\n", __func__, addr, ret);
  505. return ret;
  506. }
  507. /* The APB host has an IRQ line for each IRQ line of each slot. */
  508. static int pci_apb_map_irq(PCIDevice *pci_dev, int irq_num)
  509. {
  510. return ((pci_dev->devfn & 0x18) >> 1) + irq_num;
  511. }
  512. static int pci_pbm_map_irq(PCIDevice *pci_dev, int irq_num)
  513. {
  514. int bus_offset;
  515. if (pci_dev->devfn & 1)
  516. bus_offset = 16;
  517. else
  518. bus_offset = 0;
  519. return (bus_offset + (PCI_SLOT(pci_dev->devfn) << 2) + irq_num) & 0x1f;
  520. }
  521. static void pci_apb_set_irq(void *opaque, int irq_num, int level)
  522. {
  523. APBState *s = opaque;
  524. APB_DPRINTF("%s: set irq_in %d level %d\n", __func__, irq_num, level);
  525. /* PCI IRQ map onto the first 32 INO. */
  526. if (irq_num < 32) {
  527. if (level) {
  528. s->pci_irq_in |= 1ULL << irq_num;
  529. if (s->pci_irq_map[irq_num >> 2] & PBM_PCI_IMR_ENABLED) {
  530. pbm_set_request(s, irq_num);
  531. }
  532. } else {
  533. s->pci_irq_in &= ~(1ULL << irq_num);
  534. }
  535. } else {
  536. /* OBIO IRQ map onto the next 32 INO. */
  537. if (level) {
  538. APB_DPRINTF("%s: set irq %d level %d\n", __func__, irq_num, level);
  539. s->pci_irq_in |= 1ULL << irq_num;
  540. if ((s->irq_request == NO_IRQ_REQUEST)
  541. && (s->obio_irq_map[irq_num - 32] & PBM_PCI_IMR_ENABLED)) {
  542. pbm_set_request(s, irq_num);
  543. }
  544. } else {
  545. s->pci_irq_in &= ~(1ULL << irq_num);
  546. }
  547. }
  548. }
  549. static int apb_pci_bridge_initfn(PCIDevice *dev)
  550. {
  551. int rc;
  552. rc = pci_bridge_initfn(dev, TYPE_PCI_BUS);
  553. if (rc < 0) {
  554. return rc;
  555. }
  556. /*
  557. * command register:
  558. * According to PCI bridge spec, after reset
  559. * bus master bit is off
  560. * memory space enable bit is off
  561. * According to manual (805-1251.pdf).
  562. * the reset value should be zero unless the boot pin is tied high
  563. * (which is true) and thus it should be PCI_COMMAND_MEMORY.
  564. */
  565. pci_set_word(dev->config + PCI_COMMAND,
  566. PCI_COMMAND_MEMORY);
  567. pci_set_word(dev->config + PCI_STATUS,
  568. PCI_STATUS_FAST_BACK | PCI_STATUS_66MHZ |
  569. PCI_STATUS_DEVSEL_MEDIUM);
  570. return 0;
  571. }
  572. PCIBus *pci_apb_init(hwaddr special_base,
  573. hwaddr mem_base,
  574. qemu_irq *ivec_irqs, PCIBus **bus2, PCIBus **bus3,
  575. qemu_irq **pbm_irqs)
  576. {
  577. DeviceState *dev;
  578. SysBusDevice *s;
  579. PCIHostState *phb;
  580. APBState *d;
  581. IOMMUState *is;
  582. PCIDevice *pci_dev;
  583. PCIBridge *br;
  584. /* Ultrasparc PBM main bus */
  585. dev = qdev_create(NULL, TYPE_APB);
  586. qdev_init_nofail(dev);
  587. s = SYS_BUS_DEVICE(dev);
  588. /* apb_config */
  589. sysbus_mmio_map(s, 0, special_base);
  590. /* PCI configuration space */
  591. sysbus_mmio_map(s, 1, special_base + 0x1000000ULL);
  592. /* pci_ioport */
  593. sysbus_mmio_map(s, 2, special_base + 0x2000000ULL);
  594. d = APB_DEVICE(dev);
  595. memory_region_init(&d->pci_mmio, OBJECT(s), "pci-mmio", 0x100000000ULL);
  596. memory_region_add_subregion(get_system_memory(), mem_base, &d->pci_mmio);
  597. phb = PCI_HOST_BRIDGE(dev);
  598. phb->bus = pci_register_bus(DEVICE(phb), "pci",
  599. pci_apb_set_irq, pci_pbm_map_irq, d,
  600. &d->pci_mmio,
  601. get_system_io(),
  602. 0, 32, TYPE_PCI_BUS);
  603. *pbm_irqs = d->pbm_irqs;
  604. d->ivec_irqs = ivec_irqs;
  605. pci_create_simple(phb->bus, 0, "pbm-pci");
  606. /* APB IOMMU */
  607. is = &d->iommu;
  608. memset(is, 0, sizeof(IOMMUState));
  609. memory_region_init_iommu(&is->iommu, OBJECT(dev), &pbm_iommu_ops,
  610. "iommu-apb", UINT64_MAX);
  611. address_space_init(&is->iommu_as, &is->iommu, "pbm-as");
  612. pci_setup_iommu(phb->bus, pbm_pci_dma_iommu, is);
  613. /* APB secondary busses */
  614. pci_dev = pci_create_multifunction(phb->bus, PCI_DEVFN(1, 0), true,
  615. "pbm-bridge");
  616. br = PCI_BRIDGE(pci_dev);
  617. pci_bridge_map_irq(br, "Advanced PCI Bus secondary bridge 1",
  618. pci_apb_map_irq);
  619. qdev_init_nofail(&pci_dev->qdev);
  620. *bus2 = pci_bridge_get_sec_bus(br);
  621. pci_dev = pci_create_multifunction(phb->bus, PCI_DEVFN(1, 1), true,
  622. "pbm-bridge");
  623. br = PCI_BRIDGE(pci_dev);
  624. pci_bridge_map_irq(br, "Advanced PCI Bus secondary bridge 2",
  625. pci_apb_map_irq);
  626. qdev_init_nofail(&pci_dev->qdev);
  627. *bus3 = pci_bridge_get_sec_bus(br);
  628. return phb->bus;
  629. }
  630. static void pci_pbm_reset(DeviceState *d)
  631. {
  632. unsigned int i;
  633. APBState *s = APB_DEVICE(d);
  634. for (i = 0; i < 8; i++) {
  635. s->pci_irq_map[i] &= PBM_PCI_IMR_MASK;
  636. }
  637. for (i = 0; i < 32; i++) {
  638. s->obio_irq_map[i] &= PBM_PCI_IMR_MASK;
  639. }
  640. s->irq_request = NO_IRQ_REQUEST;
  641. s->pci_irq_in = 0ULL;
  642. if (s->nr_resets++ == 0) {
  643. /* Power on reset */
  644. s->reset_control = POR;
  645. }
  646. }
  647. static const MemoryRegionOps pci_config_ops = {
  648. .read = apb_pci_config_read,
  649. .write = apb_pci_config_write,
  650. .endianness = DEVICE_NATIVE_ENDIAN,
  651. };
  652. static int pci_pbm_init_device(SysBusDevice *dev)
  653. {
  654. APBState *s;
  655. unsigned int i;
  656. s = APB_DEVICE(dev);
  657. for (i = 0; i < 8; i++) {
  658. s->pci_irq_map[i] = (0x1f << 6) | (i << 2);
  659. }
  660. for (i = 0; i < 32; i++) {
  661. s->obio_irq_map[i] = ((0x1f << 6) | 0x20) + i;
  662. }
  663. s->pbm_irqs = qemu_allocate_irqs(pci_apb_set_irq, s, MAX_IVEC);
  664. s->irq_request = NO_IRQ_REQUEST;
  665. s->pci_irq_in = 0ULL;
  666. /* apb_config */
  667. memory_region_init_io(&s->apb_config, OBJECT(s), &apb_config_ops, s,
  668. "apb-config", 0x10000);
  669. /* at region 0 */
  670. sysbus_init_mmio(dev, &s->apb_config);
  671. memory_region_init_io(&s->pci_config, OBJECT(s), &pci_config_ops, s,
  672. "apb-pci-config", 0x1000000);
  673. /* at region 1 */
  674. sysbus_init_mmio(dev, &s->pci_config);
  675. /* pci_ioport */
  676. memory_region_init_alias(&s->pci_ioport, OBJECT(s), "apb-pci-ioport",
  677. get_system_io(), 0, 0x10000);
  678. /* at region 2 */
  679. sysbus_init_mmio(dev, &s->pci_ioport);
  680. return 0;
  681. }
  682. static int pbm_pci_host_init(PCIDevice *d)
  683. {
  684. pci_set_word(d->config + PCI_COMMAND,
  685. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  686. pci_set_word(d->config + PCI_STATUS,
  687. PCI_STATUS_FAST_BACK | PCI_STATUS_66MHZ |
  688. PCI_STATUS_DEVSEL_MEDIUM);
  689. return 0;
  690. }
  691. static void pbm_pci_host_class_init(ObjectClass *klass, void *data)
  692. {
  693. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  694. DeviceClass *dc = DEVICE_CLASS(klass);
  695. k->init = pbm_pci_host_init;
  696. k->vendor_id = PCI_VENDOR_ID_SUN;
  697. k->device_id = PCI_DEVICE_ID_SUN_SABRE;
  698. k->class_id = PCI_CLASS_BRIDGE_HOST;
  699. /*
  700. * PCI-facing part of the host bridge, not usable without the
  701. * host-facing part, which can't be device_add'ed, yet.
  702. */
  703. dc->cannot_instantiate_with_device_add_yet = true;
  704. }
  705. static const TypeInfo pbm_pci_host_info = {
  706. .name = "pbm-pci",
  707. .parent = TYPE_PCI_DEVICE,
  708. .instance_size = sizeof(PCIDevice),
  709. .class_init = pbm_pci_host_class_init,
  710. };
  711. static void pbm_host_class_init(ObjectClass *klass, void *data)
  712. {
  713. DeviceClass *dc = DEVICE_CLASS(klass);
  714. SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
  715. k->init = pci_pbm_init_device;
  716. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  717. dc->reset = pci_pbm_reset;
  718. }
  719. static const TypeInfo pbm_host_info = {
  720. .name = TYPE_APB,
  721. .parent = TYPE_PCI_HOST_BRIDGE,
  722. .instance_size = sizeof(APBState),
  723. .class_init = pbm_host_class_init,
  724. };
  725. static void pbm_pci_bridge_class_init(ObjectClass *klass, void *data)
  726. {
  727. DeviceClass *dc = DEVICE_CLASS(klass);
  728. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  729. k->init = apb_pci_bridge_initfn;
  730. k->exit = pci_bridge_exitfn;
  731. k->vendor_id = PCI_VENDOR_ID_SUN;
  732. k->device_id = PCI_DEVICE_ID_SUN_SIMBA;
  733. k->revision = 0x11;
  734. k->config_write = pci_bridge_write_config;
  735. k->is_bridge = 1;
  736. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  737. dc->reset = pci_bridge_reset;
  738. dc->vmsd = &vmstate_pci_device;
  739. }
  740. static const TypeInfo pbm_pci_bridge_info = {
  741. .name = "pbm-bridge",
  742. .parent = TYPE_PCI_BRIDGE,
  743. .class_init = pbm_pci_bridge_class_init,
  744. };
  745. static void pbm_register_types(void)
  746. {
  747. type_register_static(&pbm_host_info);
  748. type_register_static(&pbm_pci_host_info);
  749. type_register_static(&pbm_pci_bridge_info);
  750. }
  751. type_init(pbm_register_types)