pci_bridge.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 "pci_bridge.h"
  30. #include "pci_internals.h"
  31. #include "range.h"
  32. /* PCI bridge subsystem vendor ID helper functions */
  33. #define PCI_SSVID_SIZEOF 8
  34. #define PCI_SSVID_SVID 4
  35. #define PCI_SSVID_SSID 6
  36. int pci_bridge_ssvid_init(PCIDevice *dev, uint8_t offset,
  37. uint16_t svid, uint16_t ssid)
  38. {
  39. int pos;
  40. pos = pci_add_capability(dev, PCI_CAP_ID_SSVID, offset, PCI_SSVID_SIZEOF);
  41. if (pos < 0) {
  42. return pos;
  43. }
  44. pci_set_word(dev->config + pos + PCI_SSVID_SVID, svid);
  45. pci_set_word(dev->config + pos + PCI_SSVID_SSID, ssid);
  46. return pos;
  47. }
  48. /* Accessor function to get parent bridge device from pci bus. */
  49. PCIDevice *pci_bridge_get_device(PCIBus *bus)
  50. {
  51. return bus->parent_dev;
  52. }
  53. /* Accessor function to get secondary bus from pci-to-pci bridge device */
  54. PCIBus *pci_bridge_get_sec_bus(PCIBridge *br)
  55. {
  56. return &br->sec_bus;
  57. }
  58. static uint32_t pci_config_get_io_base(const PCIDevice *d,
  59. uint32_t base, uint32_t base_upper16)
  60. {
  61. uint32_t val;
  62. val = ((uint32_t)d->config[base] & PCI_IO_RANGE_MASK) << 8;
  63. if (d->config[base] & PCI_IO_RANGE_TYPE_32) {
  64. val |= (uint32_t)pci_get_word(d->config + base_upper16) << 16;
  65. }
  66. return val;
  67. }
  68. static pcibus_t pci_config_get_memory_base(const PCIDevice *d, uint32_t base)
  69. {
  70. return ((pcibus_t)pci_get_word(d->config + base) & PCI_MEMORY_RANGE_MASK)
  71. << 16;
  72. }
  73. static pcibus_t pci_config_get_pref_base(const PCIDevice *d,
  74. uint32_t base, uint32_t upper)
  75. {
  76. pcibus_t tmp;
  77. pcibus_t val;
  78. tmp = (pcibus_t)pci_get_word(d->config + base);
  79. val = (tmp & PCI_PREF_RANGE_MASK) << 16;
  80. if (tmp & PCI_PREF_RANGE_TYPE_64) {
  81. val |= (pcibus_t)pci_get_long(d->config + upper) << 32;
  82. }
  83. return val;
  84. }
  85. /* accessor function to get bridge filtering base address */
  86. pcibus_t pci_bridge_get_base(const PCIDevice *bridge, uint8_t type)
  87. {
  88. pcibus_t base;
  89. if (type & PCI_BASE_ADDRESS_SPACE_IO) {
  90. base = pci_config_get_io_base(bridge,
  91. PCI_IO_BASE, PCI_IO_BASE_UPPER16);
  92. } else {
  93. if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
  94. base = pci_config_get_pref_base(
  95. bridge, PCI_PREF_MEMORY_BASE, PCI_PREF_BASE_UPPER32);
  96. } else {
  97. base = pci_config_get_memory_base(bridge, PCI_MEMORY_BASE);
  98. }
  99. }
  100. return base;
  101. }
  102. /* accessor funciton to get bridge filtering limit */
  103. pcibus_t pci_bridge_get_limit(const PCIDevice *bridge, uint8_t type)
  104. {
  105. pcibus_t limit;
  106. if (type & PCI_BASE_ADDRESS_SPACE_IO) {
  107. limit = pci_config_get_io_base(bridge,
  108. PCI_IO_LIMIT, PCI_IO_LIMIT_UPPER16);
  109. limit |= 0xfff; /* PCI bridge spec 3.2.5.6. */
  110. } else {
  111. if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
  112. limit = pci_config_get_pref_base(
  113. bridge, PCI_PREF_MEMORY_LIMIT, PCI_PREF_LIMIT_UPPER32);
  114. } else {
  115. limit = pci_config_get_memory_base(bridge, PCI_MEMORY_LIMIT);
  116. }
  117. limit |= 0xfffff; /* PCI bridge spec 3.2.5.{1, 8}. */
  118. }
  119. return limit;
  120. }
  121. static void pci_bridge_init_alias(PCIBridge *bridge, MemoryRegion *alias,
  122. uint8_t type, const char *name,
  123. MemoryRegion *space,
  124. MemoryRegion *parent_space,
  125. bool enabled)
  126. {
  127. pcibus_t base = pci_bridge_get_base(&bridge->dev, type);
  128. pcibus_t limit = pci_bridge_get_limit(&bridge->dev, type);
  129. /* TODO: this doesn't handle base = 0 limit = 2^64 - 1 correctly.
  130. * Apparently no way to do this with existing memory APIs. */
  131. pcibus_t size = enabled && limit >= base ? limit + 1 - base : 0;
  132. memory_region_init_alias(alias, name, space, base, size);
  133. memory_region_add_subregion_overlap(parent_space, base, alias, 1);
  134. }
  135. static void pci_bridge_cleanup_alias(MemoryRegion *alias,
  136. MemoryRegion *parent_space)
  137. {
  138. memory_region_del_subregion(parent_space, alias);
  139. memory_region_destroy(alias);
  140. }
  141. static void pci_bridge_region_init(PCIBridge *br)
  142. {
  143. PCIBus *parent = br->dev.bus;
  144. uint16_t cmd = pci_get_word(br->dev.config + PCI_COMMAND);
  145. pci_bridge_init_alias(br, &br->alias_pref_mem,
  146. PCI_BASE_ADDRESS_MEM_PREFETCH,
  147. "pci_bridge_pref_mem",
  148. &br->address_space_mem,
  149. parent->address_space_mem,
  150. cmd & PCI_COMMAND_MEMORY);
  151. pci_bridge_init_alias(br, &br->alias_mem,
  152. PCI_BASE_ADDRESS_SPACE_MEMORY,
  153. "pci_bridge_mem",
  154. &br->address_space_mem,
  155. parent->address_space_mem,
  156. cmd & PCI_COMMAND_MEMORY);
  157. pci_bridge_init_alias(br, &br->alias_io,
  158. PCI_BASE_ADDRESS_SPACE_IO,
  159. "pci_bridge_io",
  160. &br->address_space_io,
  161. parent->address_space_io,
  162. cmd & PCI_COMMAND_IO);
  163. /* TODO: optinal VGA and VGA palette snooping support. */
  164. }
  165. static void pci_bridge_region_cleanup(PCIBridge *br)
  166. {
  167. PCIBus *parent = br->dev.bus;
  168. pci_bridge_cleanup_alias(&br->alias_io,
  169. parent->address_space_io);
  170. pci_bridge_cleanup_alias(&br->alias_mem,
  171. parent->address_space_mem);
  172. pci_bridge_cleanup_alias(&br->alias_pref_mem,
  173. parent->address_space_mem);
  174. }
  175. static void pci_bridge_update_mappings(PCIBridge *br)
  176. {
  177. /* Make updates atomic to: handle the case of one VCPU updating the bridge
  178. * while another accesses an unaffected region. */
  179. memory_region_transaction_begin();
  180. pci_bridge_region_cleanup(br);
  181. pci_bridge_region_init(br);
  182. memory_region_transaction_commit();
  183. }
  184. /* default write_config function for PCI-to-PCI bridge */
  185. void pci_bridge_write_config(PCIDevice *d,
  186. uint32_t address, uint32_t val, int len)
  187. {
  188. PCIBridge *s = container_of(d, PCIBridge, dev);
  189. uint16_t oldctl = pci_get_word(d->config + PCI_BRIDGE_CONTROL);
  190. uint16_t newctl;
  191. pci_default_write_config(d, address, val, len);
  192. if (ranges_overlap(address, len, PCI_COMMAND, 2) ||
  193. /* io base/limit */
  194. ranges_overlap(address, len, PCI_IO_BASE, 2) ||
  195. /* memory base/limit, prefetchable base/limit and
  196. io base/limit upper 16 */
  197. ranges_overlap(address, len, PCI_MEMORY_BASE, 20)) {
  198. pci_bridge_update_mappings(s);
  199. }
  200. newctl = pci_get_word(d->config + PCI_BRIDGE_CONTROL);
  201. if (~oldctl & newctl & PCI_BRIDGE_CTL_BUS_RESET) {
  202. /* Trigger hot reset on 0->1 transition. */
  203. pci_bus_reset(&s->sec_bus);
  204. }
  205. }
  206. void pci_bridge_disable_base_limit(PCIDevice *dev)
  207. {
  208. uint8_t *conf = dev->config;
  209. pci_byte_test_and_set_mask(conf + PCI_IO_BASE,
  210. PCI_IO_RANGE_MASK & 0xff);
  211. pci_byte_test_and_clear_mask(conf + PCI_IO_LIMIT,
  212. PCI_IO_RANGE_MASK & 0xff);
  213. pci_word_test_and_set_mask(conf + PCI_MEMORY_BASE,
  214. PCI_MEMORY_RANGE_MASK & 0xffff);
  215. pci_word_test_and_clear_mask(conf + PCI_MEMORY_LIMIT,
  216. PCI_MEMORY_RANGE_MASK & 0xffff);
  217. pci_word_test_and_set_mask(conf + PCI_PREF_MEMORY_BASE,
  218. PCI_PREF_RANGE_MASK & 0xffff);
  219. pci_word_test_and_clear_mask(conf + PCI_PREF_MEMORY_LIMIT,
  220. PCI_PREF_RANGE_MASK & 0xffff);
  221. pci_set_word(conf + PCI_PREF_BASE_UPPER32, 0);
  222. pci_set_word(conf + PCI_PREF_LIMIT_UPPER32, 0);
  223. }
  224. /* reset bridge specific configuration registers */
  225. void pci_bridge_reset_reg(PCIDevice *dev)
  226. {
  227. uint8_t *conf = dev->config;
  228. conf[PCI_PRIMARY_BUS] = 0;
  229. conf[PCI_SECONDARY_BUS] = 0;
  230. conf[PCI_SUBORDINATE_BUS] = 0;
  231. conf[PCI_SEC_LATENCY_TIMER] = 0;
  232. /*
  233. * the default values for base/limit registers aren't specified
  234. * in the PCI-to-PCI-bridge spec. So we don't thouch them here.
  235. * Each implementation can override it.
  236. * typical implementation does
  237. * zero base/limit registers or
  238. * disable forwarding: pci_bridge_disable_base_limit()
  239. * If disable forwarding is wanted, call pci_bridge_disable_base_limit()
  240. * after this function.
  241. */
  242. pci_byte_test_and_clear_mask(conf + PCI_IO_BASE,
  243. PCI_IO_RANGE_MASK & 0xff);
  244. pci_byte_test_and_clear_mask(conf + PCI_IO_LIMIT,
  245. PCI_IO_RANGE_MASK & 0xff);
  246. pci_word_test_and_clear_mask(conf + PCI_MEMORY_BASE,
  247. PCI_MEMORY_RANGE_MASK & 0xffff);
  248. pci_word_test_and_clear_mask(conf + PCI_MEMORY_LIMIT,
  249. PCI_MEMORY_RANGE_MASK & 0xffff);
  250. pci_word_test_and_clear_mask(conf + PCI_PREF_MEMORY_BASE,
  251. PCI_PREF_RANGE_MASK & 0xffff);
  252. pci_word_test_and_clear_mask(conf + PCI_PREF_MEMORY_LIMIT,
  253. PCI_PREF_RANGE_MASK & 0xffff);
  254. pci_set_word(conf + PCI_PREF_BASE_UPPER32, 0);
  255. pci_set_word(conf + PCI_PREF_LIMIT_UPPER32, 0);
  256. pci_set_word(conf + PCI_BRIDGE_CONTROL, 0);
  257. }
  258. /* default reset function for PCI-to-PCI bridge */
  259. void pci_bridge_reset(DeviceState *qdev)
  260. {
  261. PCIDevice *dev = DO_UPCAST(PCIDevice, qdev, qdev);
  262. pci_bridge_reset_reg(dev);
  263. }
  264. /* default qdev initialization function for PCI-to-PCI bridge */
  265. int pci_bridge_initfn(PCIDevice *dev)
  266. {
  267. PCIBus *parent = dev->bus;
  268. PCIBridge *br = DO_UPCAST(PCIBridge, dev, dev);
  269. PCIBus *sec_bus = &br->sec_bus;
  270. pci_set_word(dev->config + PCI_STATUS,
  271. PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
  272. pci_config_set_class(dev->config, PCI_CLASS_BRIDGE_PCI);
  273. dev->config[PCI_HEADER_TYPE] =
  274. (dev->config[PCI_HEADER_TYPE] & PCI_HEADER_TYPE_MULTI_FUNCTION) |
  275. PCI_HEADER_TYPE_BRIDGE;
  276. pci_set_word(dev->config + PCI_SEC_STATUS,
  277. PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
  278. qbus_create_inplace(&sec_bus->qbus, &pci_bus_info, &dev->qdev,
  279. br->bus_name);
  280. sec_bus->parent_dev = dev;
  281. sec_bus->map_irq = br->map_irq;
  282. sec_bus->address_space_mem = &br->address_space_mem;
  283. memory_region_init(&br->address_space_mem, "pci_bridge_pci", INT64_MAX);
  284. sec_bus->address_space_io = &br->address_space_io;
  285. memory_region_init(&br->address_space_io, "pci_bridge_io", 65536);
  286. pci_bridge_region_init(br);
  287. QLIST_INIT(&sec_bus->child);
  288. QLIST_INSERT_HEAD(&parent->child, sec_bus, sibling);
  289. return 0;
  290. }
  291. /* default qdev clean up function for PCI-to-PCI bridge */
  292. int pci_bridge_exitfn(PCIDevice *pci_dev)
  293. {
  294. PCIBridge *s = DO_UPCAST(PCIBridge, dev, pci_dev);
  295. assert(QLIST_EMPTY(&s->sec_bus.child));
  296. QLIST_REMOVE(&s->sec_bus, sibling);
  297. pci_bridge_region_cleanup(s);
  298. memory_region_destroy(&s->address_space_mem);
  299. memory_region_destroy(&s->address_space_io);
  300. /* qbus_free() is called automatically by qdev_free() */
  301. return 0;
  302. }
  303. /*
  304. * before qdev initialization(qdev_init()), this function sets bus_name and
  305. * map_irq callback which are necessry for pci_bridge_initfn() to
  306. * initialize bus.
  307. */
  308. void pci_bridge_map_irq(PCIBridge *br, const char* bus_name,
  309. pci_map_irq_fn map_irq)
  310. {
  311. br->map_irq = map_irq;
  312. br->bus_name = bus_name;
  313. }