2
0

pci_bridge.c 18 KB

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