2
0

pci_bridge.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 "hw/pci/pci_bridge.h"
  30. #include "hw/pci/pci_bus.h"
  31. #include "qemu/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 PCIBridgeWindows *pci_bridge_region_init(PCIBridge *br)
  136. {
  137. PCIBus *parent = br->dev.bus;
  138. PCIBridgeWindows *w = g_new(PCIBridgeWindows, 1);
  139. uint16_t cmd = pci_get_word(br->dev.config + PCI_COMMAND);
  140. pci_bridge_init_alias(br, &w->alias_pref_mem,
  141. PCI_BASE_ADDRESS_MEM_PREFETCH,
  142. "pci_bridge_pref_mem",
  143. &br->address_space_mem,
  144. parent->address_space_mem,
  145. cmd & PCI_COMMAND_MEMORY);
  146. pci_bridge_init_alias(br, &w->alias_mem,
  147. PCI_BASE_ADDRESS_SPACE_MEMORY,
  148. "pci_bridge_mem",
  149. &br->address_space_mem,
  150. parent->address_space_mem,
  151. cmd & PCI_COMMAND_MEMORY);
  152. pci_bridge_init_alias(br, &w->alias_io,
  153. PCI_BASE_ADDRESS_SPACE_IO,
  154. "pci_bridge_io",
  155. &br->address_space_io,
  156. parent->address_space_io,
  157. cmd & PCI_COMMAND_IO);
  158. /* TODO: optinal VGA and VGA palette snooping support. */
  159. return w;
  160. }
  161. static void pci_bridge_region_del(PCIBridge *br, PCIBridgeWindows *w)
  162. {
  163. PCIBus *parent = br->dev.bus;
  164. memory_region_del_subregion(parent->address_space_io, &w->alias_io);
  165. memory_region_del_subregion(parent->address_space_mem, &w->alias_mem);
  166. memory_region_del_subregion(parent->address_space_mem, &w->alias_pref_mem);
  167. }
  168. static void pci_bridge_region_cleanup(PCIBridge *br, PCIBridgeWindows *w)
  169. {
  170. memory_region_destroy(&w->alias_io);
  171. memory_region_destroy(&w->alias_mem);
  172. memory_region_destroy(&w->alias_pref_mem);
  173. g_free(w);
  174. }
  175. static void pci_bridge_update_mappings(PCIBridge *br)
  176. {
  177. PCIBridgeWindows *w = br->windows;
  178. /* Make updates atomic to: handle the case of one VCPU updating the bridge
  179. * while another accesses an unaffected region. */
  180. memory_region_transaction_begin();
  181. pci_bridge_region_del(br, br->windows);
  182. br->windows = pci_bridge_region_init(br);
  183. memory_region_transaction_commit();
  184. pci_bridge_region_cleanup(br, w);
  185. }
  186. /* default write_config function for PCI-to-PCI bridge */
  187. void pci_bridge_write_config(PCIDevice *d,
  188. uint32_t address, uint32_t val, int len)
  189. {
  190. PCIBridge *s = container_of(d, PCIBridge, dev);
  191. uint16_t oldctl = pci_get_word(d->config + PCI_BRIDGE_CONTROL);
  192. uint16_t newctl;
  193. pci_default_write_config(d, address, val, len);
  194. if (ranges_overlap(address, len, PCI_COMMAND, 2) ||
  195. /* io base/limit */
  196. ranges_overlap(address, len, PCI_IO_BASE, 2) ||
  197. /* memory base/limit, prefetchable base/limit and
  198. io base/limit upper 16 */
  199. ranges_overlap(address, len, PCI_MEMORY_BASE, 20)) {
  200. pci_bridge_update_mappings(s);
  201. }
  202. newctl = pci_get_word(d->config + PCI_BRIDGE_CONTROL);
  203. if (~oldctl & newctl & PCI_BRIDGE_CTL_BUS_RESET) {
  204. /* Trigger hot reset on 0->1 transition. */
  205. pci_bus_reset(&s->sec_bus);
  206. }
  207. }
  208. void pci_bridge_disable_base_limit(PCIDevice *dev)
  209. {
  210. uint8_t *conf = dev->config;
  211. pci_byte_test_and_set_mask(conf + PCI_IO_BASE,
  212. PCI_IO_RANGE_MASK & 0xff);
  213. pci_byte_test_and_clear_mask(conf + PCI_IO_LIMIT,
  214. PCI_IO_RANGE_MASK & 0xff);
  215. pci_word_test_and_set_mask(conf + PCI_MEMORY_BASE,
  216. PCI_MEMORY_RANGE_MASK & 0xffff);
  217. pci_word_test_and_clear_mask(conf + PCI_MEMORY_LIMIT,
  218. PCI_MEMORY_RANGE_MASK & 0xffff);
  219. pci_word_test_and_set_mask(conf + PCI_PREF_MEMORY_BASE,
  220. PCI_PREF_RANGE_MASK & 0xffff);
  221. pci_word_test_and_clear_mask(conf + PCI_PREF_MEMORY_LIMIT,
  222. PCI_PREF_RANGE_MASK & 0xffff);
  223. pci_set_long(conf + PCI_PREF_BASE_UPPER32, 0);
  224. pci_set_long(conf + PCI_PREF_LIMIT_UPPER32, 0);
  225. }
  226. /* reset bridge specific configuration registers */
  227. void pci_bridge_reset(DeviceState *qdev)
  228. {
  229. PCIDevice *dev = PCI_DEVICE(qdev);
  230. uint8_t *conf = dev->config;
  231. conf[PCI_PRIMARY_BUS] = 0;
  232. conf[PCI_SECONDARY_BUS] = 0;
  233. conf[PCI_SUBORDINATE_BUS] = 0;
  234. conf[PCI_SEC_LATENCY_TIMER] = 0;
  235. /*
  236. * the default values for base/limit registers aren't specified
  237. * in the PCI-to-PCI-bridge spec. So we don't thouch them here.
  238. * Each implementation can override it.
  239. * typical implementation does
  240. * zero base/limit registers or
  241. * disable forwarding: pci_bridge_disable_base_limit()
  242. * If disable forwarding is wanted, call pci_bridge_disable_base_limit()
  243. * after this function.
  244. */
  245. pci_byte_test_and_clear_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_clear_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_clear_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. pci_set_word(conf + PCI_BRIDGE_CONTROL, 0);
  260. }
  261. /* default qdev initialization function for PCI-to-PCI bridge */
  262. int pci_bridge_initfn(PCIDevice *dev)
  263. {
  264. PCIBus *parent = dev->bus;
  265. PCIBridge *br = DO_UPCAST(PCIBridge, dev, dev);
  266. PCIBus *sec_bus = &br->sec_bus;
  267. pci_word_test_and_set_mask(dev->config + PCI_STATUS,
  268. PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
  269. pci_config_set_class(dev->config, PCI_CLASS_BRIDGE_PCI);
  270. dev->config[PCI_HEADER_TYPE] =
  271. (dev->config[PCI_HEADER_TYPE] & PCI_HEADER_TYPE_MULTI_FUNCTION) |
  272. PCI_HEADER_TYPE_BRIDGE;
  273. pci_set_word(dev->config + PCI_SEC_STATUS,
  274. PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
  275. /*
  276. * If we don't specify the name, the bus will be addressed as <id>.0, where
  277. * id is the device id.
  278. * Since PCI Bridge devices have a single bus each, we don't need the index:
  279. * let users address the bus using the device name.
  280. */
  281. if (!br->bus_name && dev->qdev.id && *dev->qdev.id) {
  282. br->bus_name = dev->qdev.id;
  283. }
  284. qbus_create_inplace(&sec_bus->qbus, TYPE_PCI_BUS, &dev->qdev,
  285. br->bus_name);
  286. sec_bus->parent_dev = dev;
  287. sec_bus->map_irq = br->map_irq;
  288. sec_bus->address_space_mem = &br->address_space_mem;
  289. memory_region_init(&br->address_space_mem, "pci_bridge_pci", INT64_MAX);
  290. sec_bus->address_space_io = &br->address_space_io;
  291. memory_region_init(&br->address_space_io, "pci_bridge_io", 65536);
  292. br->windows = pci_bridge_region_init(br);
  293. QLIST_INIT(&sec_bus->child);
  294. QLIST_INSERT_HEAD(&parent->child, sec_bus, sibling);
  295. return 0;
  296. }
  297. /* default qdev clean up function for PCI-to-PCI bridge */
  298. void pci_bridge_exitfn(PCIDevice *pci_dev)
  299. {
  300. PCIBridge *s = DO_UPCAST(PCIBridge, dev, pci_dev);
  301. assert(QLIST_EMPTY(&s->sec_bus.child));
  302. QLIST_REMOVE(&s->sec_bus, sibling);
  303. pci_bridge_region_del(s, s->windows);
  304. pci_bridge_region_cleanup(s, s->windows);
  305. memory_region_destroy(&s->address_space_mem);
  306. memory_region_destroy(&s->address_space_io);
  307. /* qbus_free() is called automatically by qdev_free() */
  308. }
  309. /*
  310. * before qdev initialization(qdev_init()), this function sets bus_name and
  311. * map_irq callback which are necessry for pci_bridge_initfn() to
  312. * initialize bus.
  313. */
  314. void pci_bridge_map_irq(PCIBridge *br, const char* bus_name,
  315. pci_map_irq_fn map_irq)
  316. {
  317. br->map_irq = map_irq;
  318. br->bus_name = bus_name;
  319. }