2
0

pci_bridge.c 17 KB

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