pci_bridge.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * QEMU PCI bus manager
  3. *
  4. * Copyright (c) 2004 Fabrice Bellard
  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 dea
  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. /*
  25. * split out from pci.c
  26. * Copyright (c) 2010 Isaku Yamahata <yamahata at valinux co jp>
  27. * VA Linux Systems Japan K.K.
  28. */
  29. #include "qemu/osdep.h"
  30. #include "hw/pci/pci_bridge.h"
  31. #include "hw/pci/pci_bus.h"
  32. #include "qemu/module.h"
  33. #include "qemu/range.h"
  34. #include "qapi/error.h"
  35. /* PCI bridge subsystem vendor ID helper functions */
  36. #define PCI_SSVID_SIZEOF 8
  37. #define PCI_SSVID_SVID 4
  38. #define PCI_SSVID_SSID 6
  39. int pci_bridge_ssvid_init(PCIDevice *dev, uint8_t offset,
  40. uint16_t svid, uint16_t ssid,
  41. Error **errp)
  42. {
  43. int pos;
  44. pos = pci_add_capability(dev, PCI_CAP_ID_SSVID, offset,
  45. PCI_SSVID_SIZEOF, errp);
  46. if (pos < 0) {
  47. return pos;
  48. }
  49. pci_set_word(dev->config + pos + PCI_SSVID_SVID, svid);
  50. pci_set_word(dev->config + pos + PCI_SSVID_SSID, ssid);
  51. return pos;
  52. }
  53. /* Accessor function to get parent bridge device from pci bus. */
  54. PCIDevice *pci_bridge_get_device(PCIBus *bus)
  55. {
  56. return bus->parent_dev;
  57. }
  58. /* Accessor function to get secondary bus from pci-to-pci bridge device */
  59. PCIBus *pci_bridge_get_sec_bus(PCIBridge *br)
  60. {
  61. return &br->sec_bus;
  62. }
  63. static uint32_t pci_config_get_io_base(const PCIDevice *d,
  64. uint32_t base, uint32_t base_upper16)
  65. {
  66. uint32_t val;
  67. val = ((uint32_t)d->config[base] & PCI_IO_RANGE_MASK) << 8;
  68. if (d->config[base] & PCI_IO_RANGE_TYPE_32) {
  69. val |= (uint32_t)pci_get_word(d->config + base_upper16) << 16;
  70. }
  71. return val;
  72. }
  73. static pcibus_t pci_config_get_memory_base(const PCIDevice *d, uint32_t base)
  74. {
  75. return ((pcibus_t)pci_get_word(d->config + base) & PCI_MEMORY_RANGE_MASK)
  76. << 16;
  77. }
  78. static pcibus_t pci_config_get_pref_base(const PCIDevice *d,
  79. uint32_t base, uint32_t upper)
  80. {
  81. pcibus_t tmp;
  82. pcibus_t val;
  83. tmp = (pcibus_t)pci_get_word(d->config + base);
  84. val = (tmp & PCI_PREF_RANGE_MASK) << 16;
  85. if (tmp & PCI_PREF_RANGE_TYPE_64) {
  86. val |= (pcibus_t)pci_get_long(d->config + upper) << 32;
  87. }
  88. return val;
  89. }
  90. /* accessor function to get bridge filtering base address */
  91. pcibus_t pci_bridge_get_base(const PCIDevice *bridge, uint8_t type)
  92. {
  93. pcibus_t base;
  94. if (type & PCI_BASE_ADDRESS_SPACE_IO) {
  95. base = pci_config_get_io_base(bridge,
  96. PCI_IO_BASE, PCI_IO_BASE_UPPER16);
  97. } else {
  98. if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
  99. base = pci_config_get_pref_base(
  100. bridge, PCI_PREF_MEMORY_BASE, PCI_PREF_BASE_UPPER32);
  101. } else {
  102. base = pci_config_get_memory_base(bridge, PCI_MEMORY_BASE);
  103. }
  104. }
  105. return base;
  106. }
  107. /* accessor function to get bridge filtering limit */
  108. pcibus_t pci_bridge_get_limit(const PCIDevice *bridge, uint8_t type)
  109. {
  110. pcibus_t limit;
  111. if (type & PCI_BASE_ADDRESS_SPACE_IO) {
  112. limit = pci_config_get_io_base(bridge,
  113. PCI_IO_LIMIT, PCI_IO_LIMIT_UPPER16);
  114. limit |= 0xfff; /* PCI bridge spec 3.2.5.6. */
  115. } else {
  116. if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
  117. limit = pci_config_get_pref_base(
  118. bridge, PCI_PREF_MEMORY_LIMIT, PCI_PREF_LIMIT_UPPER32);
  119. } else {
  120. limit = pci_config_get_memory_base(bridge, PCI_MEMORY_LIMIT);
  121. }
  122. limit |= 0xfffff; /* PCI bridge spec 3.2.5.{1, 8}. */
  123. }
  124. return limit;
  125. }
  126. static void pci_bridge_init_alias(PCIBridge *bridge, MemoryRegion *alias,
  127. uint8_t type, const char *name,
  128. MemoryRegion *space,
  129. MemoryRegion *parent_space,
  130. bool enabled)
  131. {
  132. PCIDevice *bridge_dev = PCI_DEVICE(bridge);
  133. pcibus_t base = pci_bridge_get_base(bridge_dev, type);
  134. pcibus_t limit = pci_bridge_get_limit(bridge_dev, type);
  135. /* TODO: this doesn't handle base = 0 limit = 2^64 - 1 correctly.
  136. * Apparently no way to do this with existing memory APIs. */
  137. pcibus_t size = enabled && limit >= base ? limit + 1 - base : 0;
  138. memory_region_init_alias(alias, OBJECT(bridge), name, space, base, size);
  139. memory_region_add_subregion_overlap(parent_space, base, alias, 1);
  140. }
  141. static void pci_bridge_init_vga_aliases(PCIBridge *br, PCIBus *parent,
  142. MemoryRegion *alias_vga)
  143. {
  144. PCIDevice *pd = PCI_DEVICE(br);
  145. uint16_t brctl = pci_get_word(pd->config + PCI_BRIDGE_CONTROL);
  146. memory_region_init_alias(&alias_vga[QEMU_PCI_VGA_IO_LO], OBJECT(br),
  147. "pci_bridge_vga_io_lo", &br->address_space_io,
  148. QEMU_PCI_VGA_IO_LO_BASE, QEMU_PCI_VGA_IO_LO_SIZE);
  149. memory_region_init_alias(&alias_vga[QEMU_PCI_VGA_IO_HI], OBJECT(br),
  150. "pci_bridge_vga_io_hi", &br->address_space_io,
  151. QEMU_PCI_VGA_IO_HI_BASE, QEMU_PCI_VGA_IO_HI_SIZE);
  152. memory_region_init_alias(&alias_vga[QEMU_PCI_VGA_MEM], OBJECT(br),
  153. "pci_bridge_vga_mem", &br->address_space_mem,
  154. QEMU_PCI_VGA_MEM_BASE, QEMU_PCI_VGA_MEM_SIZE);
  155. if (brctl & PCI_BRIDGE_CTL_VGA) {
  156. pci_register_vga(pd, &alias_vga[QEMU_PCI_VGA_MEM],
  157. &alias_vga[QEMU_PCI_VGA_IO_LO],
  158. &alias_vga[QEMU_PCI_VGA_IO_HI]);
  159. }
  160. }
  161. static PCIBridgeWindows *pci_bridge_region_init(PCIBridge *br)
  162. {
  163. PCIDevice *pd = PCI_DEVICE(br);
  164. PCIBus *parent = pci_get_bus(pd);
  165. PCIBridgeWindows *w = g_new(PCIBridgeWindows, 1);
  166. uint16_t cmd = pci_get_word(pd->config + PCI_COMMAND);
  167. pci_bridge_init_alias(br, &w->alias_pref_mem,
  168. PCI_BASE_ADDRESS_MEM_PREFETCH,
  169. "pci_bridge_pref_mem",
  170. &br->address_space_mem,
  171. parent->address_space_mem,
  172. cmd & PCI_COMMAND_MEMORY);
  173. pci_bridge_init_alias(br, &w->alias_mem,
  174. PCI_BASE_ADDRESS_SPACE_MEMORY,
  175. "pci_bridge_mem",
  176. &br->address_space_mem,
  177. parent->address_space_mem,
  178. cmd & PCI_COMMAND_MEMORY);
  179. pci_bridge_init_alias(br, &w->alias_io,
  180. PCI_BASE_ADDRESS_SPACE_IO,
  181. "pci_bridge_io",
  182. &br->address_space_io,
  183. parent->address_space_io,
  184. cmd & PCI_COMMAND_IO);
  185. pci_bridge_init_vga_aliases(br, parent, w->alias_vga);
  186. return w;
  187. }
  188. static void pci_bridge_region_del(PCIBridge *br, PCIBridgeWindows *w)
  189. {
  190. PCIDevice *pd = PCI_DEVICE(br);
  191. PCIBus *parent = pci_get_bus(pd);
  192. memory_region_del_subregion(parent->address_space_io, &w->alias_io);
  193. memory_region_del_subregion(parent->address_space_mem, &w->alias_mem);
  194. memory_region_del_subregion(parent->address_space_mem, &w->alias_pref_mem);
  195. pci_unregister_vga(pd);
  196. }
  197. static void pci_bridge_region_cleanup(PCIBridge *br, PCIBridgeWindows *w)
  198. {
  199. object_unparent(OBJECT(&w->alias_io));
  200. object_unparent(OBJECT(&w->alias_mem));
  201. object_unparent(OBJECT(&w->alias_pref_mem));
  202. object_unparent(OBJECT(&w->alias_vga[QEMU_PCI_VGA_IO_LO]));
  203. object_unparent(OBJECT(&w->alias_vga[QEMU_PCI_VGA_IO_HI]));
  204. object_unparent(OBJECT(&w->alias_vga[QEMU_PCI_VGA_MEM]));
  205. g_free(w);
  206. }
  207. void pci_bridge_update_mappings(PCIBridge *br)
  208. {
  209. PCIBridgeWindows *w = br->windows;
  210. /* Make updates atomic to: handle the case of one VCPU updating the bridge
  211. * while another accesses an unaffected region. */
  212. memory_region_transaction_begin();
  213. pci_bridge_region_del(br, br->windows);
  214. pci_bridge_region_cleanup(br, w);
  215. br->windows = pci_bridge_region_init(br);
  216. memory_region_transaction_commit();
  217. }
  218. /* default write_config function for PCI-to-PCI bridge */
  219. void pci_bridge_write_config(PCIDevice *d,
  220. uint32_t address, uint32_t val, int len)
  221. {
  222. PCIBridge *s = PCI_BRIDGE(d);
  223. uint16_t oldctl = pci_get_word(d->config + PCI_BRIDGE_CONTROL);
  224. uint16_t newctl;
  225. pci_default_write_config(d, address, val, len);
  226. if (ranges_overlap(address, len, PCI_COMMAND, 2) ||
  227. /* io base/limit */
  228. ranges_overlap(address, len, PCI_IO_BASE, 2) ||
  229. /* memory base/limit, prefetchable base/limit and
  230. io base/limit upper 16 */
  231. ranges_overlap(address, len, PCI_MEMORY_BASE, 20) ||
  232. /* vga enable */
  233. ranges_overlap(address, len, PCI_BRIDGE_CONTROL, 2)) {
  234. pci_bridge_update_mappings(s);
  235. }
  236. newctl = pci_get_word(d->config + PCI_BRIDGE_CONTROL);
  237. if (~oldctl & newctl & PCI_BRIDGE_CTL_BUS_RESET) {
  238. /* Trigger hot reset on 0->1 transition. */
  239. qbus_reset_all(BUS(&s->sec_bus));
  240. }
  241. }
  242. void pci_bridge_disable_base_limit(PCIDevice *dev)
  243. {
  244. uint8_t *conf = dev->config;
  245. pci_byte_test_and_set_mask(conf + PCI_IO_BASE,
  246. PCI_IO_RANGE_MASK & 0xff);
  247. pci_byte_test_and_clear_mask(conf + PCI_IO_LIMIT,
  248. PCI_IO_RANGE_MASK & 0xff);
  249. pci_word_test_and_set_mask(conf + PCI_MEMORY_BASE,
  250. PCI_MEMORY_RANGE_MASK & 0xffff);
  251. pci_word_test_and_clear_mask(conf + PCI_MEMORY_LIMIT,
  252. PCI_MEMORY_RANGE_MASK & 0xffff);
  253. pci_word_test_and_set_mask(conf + PCI_PREF_MEMORY_BASE,
  254. PCI_PREF_RANGE_MASK & 0xffff);
  255. pci_word_test_and_clear_mask(conf + PCI_PREF_MEMORY_LIMIT,
  256. PCI_PREF_RANGE_MASK & 0xffff);
  257. pci_set_long(conf + PCI_PREF_BASE_UPPER32, 0);
  258. pci_set_long(conf + PCI_PREF_LIMIT_UPPER32, 0);
  259. }
  260. /* reset bridge specific configuration registers */
  261. void pci_bridge_reset(DeviceState *qdev)
  262. {
  263. PCIDevice *dev = PCI_DEVICE(qdev);
  264. uint8_t *conf = dev->config;
  265. conf[PCI_PRIMARY_BUS] = 0;
  266. conf[PCI_SECONDARY_BUS] = 0;
  267. conf[PCI_SUBORDINATE_BUS] = 0;
  268. conf[PCI_SEC_LATENCY_TIMER] = 0;
  269. /*
  270. * the default values for base/limit registers aren't specified
  271. * in the PCI-to-PCI-bridge spec. So we don't touch them here.
  272. * Each implementation can override it.
  273. * typical implementation does
  274. * zero base/limit registers or
  275. * disable forwarding: pci_bridge_disable_base_limit()
  276. * If disable forwarding is wanted, call pci_bridge_disable_base_limit()
  277. * after this function.
  278. */
  279. pci_byte_test_and_clear_mask(conf + PCI_IO_BASE,
  280. PCI_IO_RANGE_MASK & 0xff);
  281. pci_byte_test_and_clear_mask(conf + PCI_IO_LIMIT,
  282. PCI_IO_RANGE_MASK & 0xff);
  283. pci_word_test_and_clear_mask(conf + PCI_MEMORY_BASE,
  284. PCI_MEMORY_RANGE_MASK & 0xffff);
  285. pci_word_test_and_clear_mask(conf + PCI_MEMORY_LIMIT,
  286. PCI_MEMORY_RANGE_MASK & 0xffff);
  287. pci_word_test_and_clear_mask(conf + PCI_PREF_MEMORY_BASE,
  288. PCI_PREF_RANGE_MASK & 0xffff);
  289. pci_word_test_and_clear_mask(conf + PCI_PREF_MEMORY_LIMIT,
  290. PCI_PREF_RANGE_MASK & 0xffff);
  291. pci_set_long(conf + PCI_PREF_BASE_UPPER32, 0);
  292. pci_set_long(conf + PCI_PREF_LIMIT_UPPER32, 0);
  293. pci_set_word(conf + PCI_BRIDGE_CONTROL, 0);
  294. }
  295. /* default qdev initialization function for PCI-to-PCI bridge */
  296. void pci_bridge_initfn(PCIDevice *dev, const char *typename)
  297. {
  298. PCIBus *parent = pci_get_bus(dev);
  299. PCIBridge *br = PCI_BRIDGE(dev);
  300. PCIBus *sec_bus = &br->sec_bus;
  301. pci_word_test_and_set_mask(dev->config + PCI_STATUS,
  302. PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
  303. /*
  304. * TODO: We implement VGA Enable in the Bridge Control Register
  305. * therefore per the PCI to PCI bridge spec we must also implement
  306. * VGA Palette Snooping. When done, set this bit writable:
  307. *
  308. * pci_word_test_and_set_mask(dev->wmask + PCI_COMMAND,
  309. * PCI_COMMAND_VGA_PALETTE);
  310. */
  311. pci_config_set_class(dev->config, PCI_CLASS_BRIDGE_PCI);
  312. dev->config[PCI_HEADER_TYPE] =
  313. (dev->config[PCI_HEADER_TYPE] & PCI_HEADER_TYPE_MULTI_FUNCTION) |
  314. PCI_HEADER_TYPE_BRIDGE;
  315. pci_set_word(dev->config + PCI_SEC_STATUS,
  316. PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
  317. /*
  318. * If we don't specify the name, the bus will be addressed as <id>.0, where
  319. * id is the device id.
  320. * Since PCI Bridge devices have a single bus each, we don't need the index:
  321. * let users address the bus using the device name.
  322. */
  323. if (!br->bus_name && dev->qdev.id && *dev->qdev.id) {
  324. br->bus_name = dev->qdev.id;
  325. }
  326. qbus_create_inplace(sec_bus, sizeof(br->sec_bus), typename, DEVICE(dev),
  327. br->bus_name);
  328. sec_bus->parent_dev = dev;
  329. sec_bus->map_irq = br->map_irq ? br->map_irq : pci_swizzle_map_irq_fn;
  330. sec_bus->address_space_mem = &br->address_space_mem;
  331. memory_region_init(&br->address_space_mem, OBJECT(br), "pci_bridge_pci", UINT64_MAX);
  332. sec_bus->address_space_io = &br->address_space_io;
  333. memory_region_init(&br->address_space_io, OBJECT(br), "pci_bridge_io",
  334. UINT32_MAX);
  335. br->windows = pci_bridge_region_init(br);
  336. QLIST_INIT(&sec_bus->child);
  337. QLIST_INSERT_HEAD(&parent->child, sec_bus, sibling);
  338. }
  339. /* default qdev clean up function for PCI-to-PCI bridge */
  340. void pci_bridge_exitfn(PCIDevice *pci_dev)
  341. {
  342. PCIBridge *s = PCI_BRIDGE(pci_dev);
  343. assert(QLIST_EMPTY(&s->sec_bus.child));
  344. QLIST_REMOVE(&s->sec_bus, sibling);
  345. pci_bridge_region_del(s, s->windows);
  346. pci_bridge_region_cleanup(s, s->windows);
  347. /* object_unparent() is called automatically during device deletion */
  348. }
  349. /*
  350. * before qdev initialization(qdev_init()), this function sets bus_name and
  351. * map_irq callback which are necessary for pci_bridge_initfn() to
  352. * initialize bus.
  353. */
  354. void pci_bridge_map_irq(PCIBridge *br, const char* bus_name,
  355. pci_map_irq_fn map_irq)
  356. {
  357. br->map_irq = map_irq;
  358. br->bus_name = bus_name;
  359. }
  360. int pci_bridge_qemu_reserve_cap_init(PCIDevice *dev, int cap_offset,
  361. PCIResReserve res_reserve, Error **errp)
  362. {
  363. if (res_reserve.mem_pref_32 != (uint64_t)-1 &&
  364. res_reserve.mem_pref_64 != (uint64_t)-1) {
  365. error_setg(errp,
  366. "PCI resource reserve cap: PREF32 and PREF64 conflict");
  367. return -EINVAL;
  368. }
  369. if (res_reserve.mem_non_pref != (uint64_t)-1 &&
  370. res_reserve.mem_non_pref >= (1ULL << 32)) {
  371. error_setg(errp,
  372. "PCI resource reserve cap: mem-reserve must be less than 4G");
  373. return -EINVAL;
  374. }
  375. if (res_reserve.mem_pref_32 != (uint64_t)-1 &&
  376. res_reserve.mem_pref_32 >= (1ULL << 32)) {
  377. error_setg(errp,
  378. "PCI resource reserve cap: pref32-reserve must be less than 4G");
  379. return -EINVAL;
  380. }
  381. if (res_reserve.bus == (uint32_t)-1 &&
  382. res_reserve.io == (uint64_t)-1 &&
  383. res_reserve.mem_non_pref == (uint64_t)-1 &&
  384. res_reserve.mem_pref_32 == (uint64_t)-1 &&
  385. res_reserve.mem_pref_64 == (uint64_t)-1) {
  386. return 0;
  387. }
  388. size_t cap_len = sizeof(PCIBridgeQemuCap);
  389. PCIBridgeQemuCap cap = {
  390. .len = cap_len,
  391. .type = REDHAT_PCI_CAP_RESOURCE_RESERVE,
  392. .bus_res = res_reserve.bus,
  393. .io = res_reserve.io,
  394. .mem = res_reserve.mem_non_pref,
  395. .mem_pref_32 = res_reserve.mem_pref_32,
  396. .mem_pref_64 = res_reserve.mem_pref_64
  397. };
  398. int offset = pci_add_capability(dev, PCI_CAP_ID_VNDR,
  399. cap_offset, cap_len, errp);
  400. if (offset < 0) {
  401. return offset;
  402. }
  403. memcpy(dev->config + offset + PCI_CAP_FLAGS,
  404. (char *)&cap + PCI_CAP_FLAGS,
  405. cap_len - PCI_CAP_FLAGS);
  406. return 0;
  407. }
  408. static const TypeInfo pci_bridge_type_info = {
  409. .name = TYPE_PCI_BRIDGE,
  410. .parent = TYPE_PCI_DEVICE,
  411. .instance_size = sizeof(PCIBridge),
  412. .abstract = true,
  413. };
  414. static void pci_bridge_register_types(void)
  415. {
  416. type_register_static(&pci_bridge_type_info);
  417. }
  418. type_init(pci_bridge_register_types)