pci.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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 deal
  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. #include "hw.h"
  25. #include "pci.h"
  26. #include "console.h"
  27. #include "net.h"
  28. #include "virtio-net.h"
  29. //#define DEBUG_PCI
  30. struct PCIBus {
  31. int bus_num;
  32. int devfn_min;
  33. pci_set_irq_fn set_irq;
  34. pci_map_irq_fn map_irq;
  35. uint32_t config_reg; /* XXX: suppress */
  36. /* low level pic */
  37. SetIRQFunc *low_set_irq;
  38. qemu_irq *irq_opaque;
  39. PCIDevice *devices[256];
  40. PCIDevice *parent_dev;
  41. PCIBus *next;
  42. /* The bus IRQ state is the logical OR of the connected devices.
  43. Keep a count of the number of devices with raised IRQs. */
  44. int nirq;
  45. int irq_count[];
  46. };
  47. static void pci_update_mappings(PCIDevice *d);
  48. static void pci_set_irq(void *opaque, int irq_num, int level);
  49. target_phys_addr_t pci_mem_base;
  50. static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
  51. static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
  52. static int pci_irq_index;
  53. static PCIBus *first_bus;
  54. static void pcibus_save(QEMUFile *f, void *opaque)
  55. {
  56. PCIBus *bus = (PCIBus *)opaque;
  57. int i;
  58. qemu_put_be32(f, bus->nirq);
  59. for (i = 0; i < bus->nirq; i++)
  60. qemu_put_be32(f, bus->irq_count[i]);
  61. }
  62. static int pcibus_load(QEMUFile *f, void *opaque, int version_id)
  63. {
  64. PCIBus *bus = (PCIBus *)opaque;
  65. int i, nirq;
  66. if (version_id != 1)
  67. return -EINVAL;
  68. nirq = qemu_get_be32(f);
  69. if (bus->nirq != nirq) {
  70. fprintf(stderr, "pcibus_load: nirq mismatch: src=%d dst=%d\n",
  71. nirq, bus->nirq);
  72. return -EINVAL;
  73. }
  74. for (i = 0; i < nirq; i++)
  75. bus->irq_count[i] = qemu_get_be32(f);
  76. return 0;
  77. }
  78. PCIBus *pci_register_bus(pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
  79. qemu_irq *pic, int devfn_min, int nirq)
  80. {
  81. PCIBus *bus;
  82. static int nbus = 0;
  83. bus = qemu_mallocz(sizeof(PCIBus) + (nirq * sizeof(int)));
  84. bus->set_irq = set_irq;
  85. bus->map_irq = map_irq;
  86. bus->irq_opaque = pic;
  87. bus->devfn_min = devfn_min;
  88. bus->nirq = nirq;
  89. first_bus = bus;
  90. register_savevm("PCIBUS", nbus++, 1, pcibus_save, pcibus_load, bus);
  91. return bus;
  92. }
  93. static PCIBus *pci_register_secondary_bus(PCIDevice *dev, pci_map_irq_fn map_irq)
  94. {
  95. PCIBus *bus;
  96. bus = qemu_mallocz(sizeof(PCIBus));
  97. bus->map_irq = map_irq;
  98. bus->parent_dev = dev;
  99. bus->next = dev->bus->next;
  100. dev->bus->next = bus;
  101. return bus;
  102. }
  103. int pci_bus_num(PCIBus *s)
  104. {
  105. return s->bus_num;
  106. }
  107. void pci_device_save(PCIDevice *s, QEMUFile *f)
  108. {
  109. int i;
  110. qemu_put_be32(f, 2); /* PCI device version */
  111. qemu_put_buffer(f, s->config, 256);
  112. for (i = 0; i < 4; i++)
  113. qemu_put_be32(f, s->irq_state[i]);
  114. }
  115. int pci_device_load(PCIDevice *s, QEMUFile *f)
  116. {
  117. uint32_t version_id;
  118. int i;
  119. version_id = qemu_get_be32(f);
  120. if (version_id > 2)
  121. return -EINVAL;
  122. qemu_get_buffer(f, s->config, 256);
  123. pci_update_mappings(s);
  124. if (version_id >= 2)
  125. for (i = 0; i < 4; i ++)
  126. s->irq_state[i] = qemu_get_be32(f);
  127. return 0;
  128. }
  129. static int pci_set_default_subsystem_id(PCIDevice *pci_dev)
  130. {
  131. uint16_t *id;
  132. id = (void*)(&pci_dev->config[PCI_SUBVENDOR_ID]);
  133. id[0] = cpu_to_le16(pci_default_sub_vendor_id);
  134. id[1] = cpu_to_le16(pci_default_sub_device_id);
  135. return 0;
  136. }
  137. /* -1 for devfn means auto assign */
  138. PCIDevice *pci_register_device(PCIBus *bus, const char *name,
  139. int instance_size, int devfn,
  140. PCIConfigReadFunc *config_read,
  141. PCIConfigWriteFunc *config_write)
  142. {
  143. PCIDevice *pci_dev;
  144. if (pci_irq_index >= PCI_DEVICES_MAX)
  145. return NULL;
  146. if (devfn < 0) {
  147. for(devfn = bus->devfn_min ; devfn < 256; devfn += 8) {
  148. if (!bus->devices[devfn])
  149. goto found;
  150. }
  151. return NULL;
  152. found: ;
  153. }
  154. pci_dev = qemu_mallocz(instance_size);
  155. pci_dev->bus = bus;
  156. pci_dev->devfn = devfn;
  157. pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
  158. memset(pci_dev->irq_state, 0, sizeof(pci_dev->irq_state));
  159. pci_set_default_subsystem_id(pci_dev);
  160. if (!config_read)
  161. config_read = pci_default_read_config;
  162. if (!config_write)
  163. config_write = pci_default_write_config;
  164. pci_dev->config_read = config_read;
  165. pci_dev->config_write = config_write;
  166. pci_dev->irq_index = pci_irq_index++;
  167. bus->devices[devfn] = pci_dev;
  168. pci_dev->irq = qemu_allocate_irqs(pci_set_irq, pci_dev, 4);
  169. return pci_dev;
  170. }
  171. void pci_register_io_region(PCIDevice *pci_dev, int region_num,
  172. uint32_t size, int type,
  173. PCIMapIORegionFunc *map_func)
  174. {
  175. PCIIORegion *r;
  176. uint32_t addr;
  177. if ((unsigned int)region_num >= PCI_NUM_REGIONS)
  178. return;
  179. r = &pci_dev->io_regions[region_num];
  180. r->addr = -1;
  181. r->size = size;
  182. r->type = type;
  183. r->map_func = map_func;
  184. if (region_num == PCI_ROM_SLOT) {
  185. addr = 0x30;
  186. } else {
  187. addr = 0x10 + region_num * 4;
  188. }
  189. *(uint32_t *)(pci_dev->config + addr) = cpu_to_le32(type);
  190. }
  191. static target_phys_addr_t pci_to_cpu_addr(target_phys_addr_t addr)
  192. {
  193. return addr + pci_mem_base;
  194. }
  195. static void pci_update_mappings(PCIDevice *d)
  196. {
  197. PCIIORegion *r;
  198. int cmd, i;
  199. uint32_t last_addr, new_addr, config_ofs;
  200. cmd = le16_to_cpu(*(uint16_t *)(d->config + PCI_COMMAND));
  201. for(i = 0; i < PCI_NUM_REGIONS; i++) {
  202. r = &d->io_regions[i];
  203. if (i == PCI_ROM_SLOT) {
  204. config_ofs = 0x30;
  205. } else {
  206. config_ofs = 0x10 + i * 4;
  207. }
  208. if (r->size != 0) {
  209. if (r->type & PCI_ADDRESS_SPACE_IO) {
  210. if (cmd & PCI_COMMAND_IO) {
  211. new_addr = le32_to_cpu(*(uint32_t *)(d->config +
  212. config_ofs));
  213. new_addr = new_addr & ~(r->size - 1);
  214. last_addr = new_addr + r->size - 1;
  215. /* NOTE: we have only 64K ioports on PC */
  216. if (last_addr <= new_addr || new_addr == 0 ||
  217. last_addr >= 0x10000) {
  218. new_addr = -1;
  219. }
  220. } else {
  221. new_addr = -1;
  222. }
  223. } else {
  224. if (cmd & PCI_COMMAND_MEMORY) {
  225. new_addr = le32_to_cpu(*(uint32_t *)(d->config +
  226. config_ofs));
  227. /* the ROM slot has a specific enable bit */
  228. if (i == PCI_ROM_SLOT && !(new_addr & 1))
  229. goto no_mem_map;
  230. new_addr = new_addr & ~(r->size - 1);
  231. last_addr = new_addr + r->size - 1;
  232. /* NOTE: we do not support wrapping */
  233. /* XXX: as we cannot support really dynamic
  234. mappings, we handle specific values as invalid
  235. mappings. */
  236. if (last_addr <= new_addr || new_addr == 0 ||
  237. last_addr == -1) {
  238. new_addr = -1;
  239. }
  240. } else {
  241. no_mem_map:
  242. new_addr = -1;
  243. }
  244. }
  245. /* now do the real mapping */
  246. if (new_addr != r->addr) {
  247. if (r->addr != -1) {
  248. if (r->type & PCI_ADDRESS_SPACE_IO) {
  249. int class;
  250. /* NOTE: specific hack for IDE in PC case:
  251. only one byte must be mapped. */
  252. class = d->config[0x0a] | (d->config[0x0b] << 8);
  253. if (class == 0x0101 && r->size == 4) {
  254. isa_unassign_ioport(r->addr + 2, 1);
  255. } else {
  256. isa_unassign_ioport(r->addr, r->size);
  257. }
  258. } else {
  259. cpu_register_physical_memory(pci_to_cpu_addr(r->addr),
  260. r->size,
  261. IO_MEM_UNASSIGNED);
  262. qemu_unregister_coalesced_mmio(r->addr, r->size);
  263. }
  264. }
  265. r->addr = new_addr;
  266. if (r->addr != -1) {
  267. r->map_func(d, i, r->addr, r->size, r->type);
  268. }
  269. }
  270. }
  271. }
  272. }
  273. uint32_t pci_default_read_config(PCIDevice *d,
  274. uint32_t address, int len)
  275. {
  276. uint32_t val;
  277. switch(len) {
  278. default:
  279. case 4:
  280. if (address <= 0xfc) {
  281. val = le32_to_cpu(*(uint32_t *)(d->config + address));
  282. break;
  283. }
  284. /* fall through */
  285. case 2:
  286. if (address <= 0xfe) {
  287. val = le16_to_cpu(*(uint16_t *)(d->config + address));
  288. break;
  289. }
  290. /* fall through */
  291. case 1:
  292. val = d->config[address];
  293. break;
  294. }
  295. return val;
  296. }
  297. void pci_default_write_config(PCIDevice *d,
  298. uint32_t address, uint32_t val, int len)
  299. {
  300. int can_write, i;
  301. uint32_t end, addr;
  302. if (len == 4 && ((address >= 0x10 && address < 0x10 + 4 * 6) ||
  303. (address >= 0x30 && address < 0x34))) {
  304. PCIIORegion *r;
  305. int reg;
  306. if ( address >= 0x30 ) {
  307. reg = PCI_ROM_SLOT;
  308. }else{
  309. reg = (address - 0x10) >> 2;
  310. }
  311. r = &d->io_regions[reg];
  312. if (r->size == 0)
  313. goto default_config;
  314. /* compute the stored value */
  315. if (reg == PCI_ROM_SLOT) {
  316. /* keep ROM enable bit */
  317. val &= (~(r->size - 1)) | 1;
  318. } else {
  319. val &= ~(r->size - 1);
  320. val |= r->type;
  321. }
  322. *(uint32_t *)(d->config + address) = cpu_to_le32(val);
  323. pci_update_mappings(d);
  324. return;
  325. }
  326. default_config:
  327. /* not efficient, but simple */
  328. addr = address;
  329. for(i = 0; i < len; i++) {
  330. /* default read/write accesses */
  331. switch(d->config[0x0e]) {
  332. case 0x00:
  333. case 0x80:
  334. switch(addr) {
  335. case 0x00:
  336. case 0x01:
  337. case 0x02:
  338. case 0x03:
  339. case 0x08:
  340. case 0x09:
  341. case 0x0a:
  342. case 0x0b:
  343. case 0x0e:
  344. case 0x10 ... 0x27: /* base */
  345. case 0x2c ... 0x2f: /* read-only subsystem ID & vendor ID */
  346. case 0x30 ... 0x33: /* rom */
  347. case 0x3d:
  348. can_write = 0;
  349. break;
  350. default:
  351. can_write = 1;
  352. break;
  353. }
  354. break;
  355. default:
  356. case 0x01:
  357. switch(addr) {
  358. case 0x00:
  359. case 0x01:
  360. case 0x02:
  361. case 0x03:
  362. case 0x08:
  363. case 0x09:
  364. case 0x0a:
  365. case 0x0b:
  366. case 0x0e:
  367. case 0x2c ... 0x2f: /* read-only subsystem ID & vendor ID */
  368. case 0x38 ... 0x3b: /* rom */
  369. case 0x3d:
  370. can_write = 0;
  371. break;
  372. default:
  373. can_write = 1;
  374. break;
  375. }
  376. break;
  377. }
  378. if (can_write) {
  379. /* Mask out writes to reserved bits in registers */
  380. switch (addr) {
  381. case 0x05:
  382. val &= ~PCI_COMMAND_RESERVED_MASK_HI;
  383. break;
  384. case 0x06:
  385. val &= ~PCI_STATUS_RESERVED_MASK_LO;
  386. break;
  387. case 0x07:
  388. val &= ~PCI_STATUS_RESERVED_MASK_HI;
  389. break;
  390. }
  391. d->config[addr] = val;
  392. }
  393. if (++addr > 0xff)
  394. break;
  395. val >>= 8;
  396. }
  397. end = address + len;
  398. if (end > PCI_COMMAND && address < (PCI_COMMAND + 2)) {
  399. /* if the command register is modified, we must modify the mappings */
  400. pci_update_mappings(d);
  401. }
  402. }
  403. void pci_data_write(void *opaque, uint32_t addr, uint32_t val, int len)
  404. {
  405. PCIBus *s = opaque;
  406. PCIDevice *pci_dev;
  407. int config_addr, bus_num;
  408. #if defined(DEBUG_PCI) && 0
  409. printf("pci_data_write: addr=%08x val=%08x len=%d\n",
  410. addr, val, len);
  411. #endif
  412. bus_num = (addr >> 16) & 0xff;
  413. while (s && s->bus_num != bus_num)
  414. s = s->next;
  415. if (!s)
  416. return;
  417. pci_dev = s->devices[(addr >> 8) & 0xff];
  418. if (!pci_dev)
  419. return;
  420. config_addr = addr & 0xff;
  421. #if defined(DEBUG_PCI)
  422. printf("pci_config_write: %s: addr=%02x val=%08x len=%d\n",
  423. pci_dev->name, config_addr, val, len);
  424. #endif
  425. pci_dev->config_write(pci_dev, config_addr, val, len);
  426. }
  427. uint32_t pci_data_read(void *opaque, uint32_t addr, int len)
  428. {
  429. PCIBus *s = opaque;
  430. PCIDevice *pci_dev;
  431. int config_addr, bus_num;
  432. uint32_t val;
  433. bus_num = (addr >> 16) & 0xff;
  434. while (s && s->bus_num != bus_num)
  435. s= s->next;
  436. if (!s)
  437. goto fail;
  438. pci_dev = s->devices[(addr >> 8) & 0xff];
  439. if (!pci_dev) {
  440. fail:
  441. switch(len) {
  442. case 1:
  443. val = 0xff;
  444. break;
  445. case 2:
  446. val = 0xffff;
  447. break;
  448. default:
  449. case 4:
  450. val = 0xffffffff;
  451. break;
  452. }
  453. goto the_end;
  454. }
  455. config_addr = addr & 0xff;
  456. val = pci_dev->config_read(pci_dev, config_addr, len);
  457. #if defined(DEBUG_PCI)
  458. printf("pci_config_read: %s: addr=%02x val=%08x len=%d\n",
  459. pci_dev->name, config_addr, val, len);
  460. #endif
  461. the_end:
  462. #if defined(DEBUG_PCI) && 0
  463. printf("pci_data_read: addr=%08x val=%08x len=%d\n",
  464. addr, val, len);
  465. #endif
  466. return val;
  467. }
  468. /***********************************************************/
  469. /* generic PCI irq support */
  470. /* 0 <= irq_num <= 3. level must be 0 or 1 */
  471. static void pci_set_irq(void *opaque, int irq_num, int level)
  472. {
  473. PCIDevice *pci_dev = (PCIDevice *)opaque;
  474. PCIBus *bus;
  475. int change;
  476. change = level - pci_dev->irq_state[irq_num];
  477. if (!change)
  478. return;
  479. pci_dev->irq_state[irq_num] = level;
  480. for (;;) {
  481. bus = pci_dev->bus;
  482. irq_num = bus->map_irq(pci_dev, irq_num);
  483. if (bus->set_irq)
  484. break;
  485. pci_dev = bus->parent_dev;
  486. }
  487. bus->irq_count[irq_num] += change;
  488. bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
  489. }
  490. /***********************************************************/
  491. /* monitor info on PCI */
  492. typedef struct {
  493. uint16_t class;
  494. const char *desc;
  495. } pci_class_desc;
  496. static const pci_class_desc pci_class_descriptions[] =
  497. {
  498. { 0x0100, "SCSI controller"},
  499. { 0x0101, "IDE controller"},
  500. { 0x0102, "Floppy controller"},
  501. { 0x0103, "IPI controller"},
  502. { 0x0104, "RAID controller"},
  503. { 0x0106, "SATA controller"},
  504. { 0x0107, "SAS controller"},
  505. { 0x0180, "Storage controller"},
  506. { 0x0200, "Ethernet controller"},
  507. { 0x0201, "Token Ring controller"},
  508. { 0x0202, "FDDI controller"},
  509. { 0x0203, "ATM controller"},
  510. { 0x0280, "Network controller"},
  511. { 0x0300, "VGA controller"},
  512. { 0x0301, "XGA controller"},
  513. { 0x0302, "3D controller"},
  514. { 0x0380, "Display controller"},
  515. { 0x0400, "Video controller"},
  516. { 0x0401, "Audio controller"},
  517. { 0x0402, "Phone"},
  518. { 0x0480, "Multimedia controller"},
  519. { 0x0500, "RAM controller"},
  520. { 0x0501, "Flash controller"},
  521. { 0x0580, "Memory controller"},
  522. { 0x0600, "Host bridge"},
  523. { 0x0601, "ISA bridge"},
  524. { 0x0602, "EISA bridge"},
  525. { 0x0603, "MC bridge"},
  526. { 0x0604, "PCI bridge"},
  527. { 0x0605, "PCMCIA bridge"},
  528. { 0x0606, "NUBUS bridge"},
  529. { 0x0607, "CARDBUS bridge"},
  530. { 0x0608, "RACEWAY bridge"},
  531. { 0x0680, "Bridge"},
  532. { 0x0c03, "USB controller"},
  533. { 0, NULL}
  534. };
  535. static void pci_info_device(PCIDevice *d)
  536. {
  537. int i, class;
  538. PCIIORegion *r;
  539. const pci_class_desc *desc;
  540. term_printf(" Bus %2d, device %3d, function %d:\n",
  541. d->bus->bus_num, d->devfn >> 3, d->devfn & 7);
  542. class = le16_to_cpu(*((uint16_t *)(d->config + PCI_CLASS_DEVICE)));
  543. term_printf(" ");
  544. desc = pci_class_descriptions;
  545. while (desc->desc && class != desc->class)
  546. desc++;
  547. if (desc->desc) {
  548. term_printf("%s", desc->desc);
  549. } else {
  550. term_printf("Class %04x", class);
  551. }
  552. term_printf(": PCI device %04x:%04x\n",
  553. le16_to_cpu(*((uint16_t *)(d->config + PCI_VENDOR_ID))),
  554. le16_to_cpu(*((uint16_t *)(d->config + PCI_DEVICE_ID))));
  555. if (d->config[PCI_INTERRUPT_PIN] != 0) {
  556. term_printf(" IRQ %d.\n", d->config[PCI_INTERRUPT_LINE]);
  557. }
  558. if (class == 0x0604) {
  559. term_printf(" BUS %d.\n", d->config[0x19]);
  560. }
  561. for(i = 0;i < PCI_NUM_REGIONS; i++) {
  562. r = &d->io_regions[i];
  563. if (r->size != 0) {
  564. term_printf(" BAR%d: ", i);
  565. if (r->type & PCI_ADDRESS_SPACE_IO) {
  566. term_printf("I/O at 0x%04x [0x%04x].\n",
  567. r->addr, r->addr + r->size - 1);
  568. } else {
  569. term_printf("32 bit memory at 0x%08x [0x%08x].\n",
  570. r->addr, r->addr + r->size - 1);
  571. }
  572. }
  573. }
  574. if (class == 0x0604 && d->config[0x19] != 0) {
  575. pci_for_each_device(d->config[0x19], pci_info_device);
  576. }
  577. }
  578. void pci_for_each_device(int bus_num, void (*fn)(PCIDevice *d))
  579. {
  580. PCIBus *bus = first_bus;
  581. PCIDevice *d;
  582. int devfn;
  583. while (bus && bus->bus_num != bus_num)
  584. bus = bus->next;
  585. if (bus) {
  586. for(devfn = 0; devfn < 256; devfn++) {
  587. d = bus->devices[devfn];
  588. if (d)
  589. fn(d);
  590. }
  591. }
  592. }
  593. void pci_info(void)
  594. {
  595. pci_for_each_device(0, pci_info_device);
  596. }
  597. static const char * const pci_nic_models[] = {
  598. "ne2k_pci",
  599. "i82551",
  600. "i82557b",
  601. "i82559er",
  602. "rtl8139",
  603. "e1000",
  604. "pcnet",
  605. "virtio",
  606. NULL
  607. };
  608. typedef PCIDevice *(*PCINICInitFn)(PCIBus *, NICInfo *, int);
  609. static PCINICInitFn pci_nic_init_fns[] = {
  610. pci_ne2000_init,
  611. pci_i82551_init,
  612. pci_i82557b_init,
  613. pci_i82559er_init,
  614. pci_rtl8139_init,
  615. pci_e1000_init,
  616. pci_pcnet_init,
  617. virtio_net_init,
  618. NULL
  619. };
  620. /* Initialize a PCI NIC. */
  621. PCIDevice *pci_nic_init(PCIBus *bus, NICInfo *nd, int devfn,
  622. const char *default_model)
  623. {
  624. PCIDevice *pci_dev;
  625. int i;
  626. qemu_check_nic_model_list(nd, pci_nic_models, default_model);
  627. for (i = 0; pci_nic_models[i]; i++)
  628. if (strcmp(nd->model, pci_nic_models[i]) == 0) {
  629. pci_dev = pci_nic_init_fns[i](bus, nd, devfn);
  630. if (pci_dev)
  631. nd->private = pci_dev;
  632. return pci_dev;
  633. }
  634. return NULL;
  635. }
  636. typedef struct {
  637. PCIDevice dev;
  638. PCIBus *bus;
  639. } PCIBridge;
  640. static void pci_bridge_write_config(PCIDevice *d,
  641. uint32_t address, uint32_t val, int len)
  642. {
  643. PCIBridge *s = (PCIBridge *)d;
  644. if (address == 0x19 || (address == 0x18 && len > 1)) {
  645. if (address == 0x19)
  646. s->bus->bus_num = val & 0xff;
  647. else
  648. s->bus->bus_num = (val >> 8) & 0xff;
  649. #if defined(DEBUG_PCI)
  650. printf ("pci-bridge: %s: Assigned bus %d\n", d->name, s->bus->bus_num);
  651. #endif
  652. }
  653. pci_default_write_config(d, address, val, len);
  654. }
  655. PCIBus *pci_find_bus(int bus_num)
  656. {
  657. PCIBus *bus = first_bus;
  658. while (bus && bus->bus_num != bus_num)
  659. bus = bus->next;
  660. return bus;
  661. }
  662. PCIDevice *pci_find_device(int bus_num, int slot, int function)
  663. {
  664. PCIBus *bus = pci_find_bus(bus_num);
  665. if (!bus)
  666. return NULL;
  667. return bus->devices[PCI_DEVFN(slot, function)];
  668. }
  669. PCIBus *pci_bridge_init(PCIBus *bus, int devfn, uint16_t vid, uint16_t did,
  670. pci_map_irq_fn map_irq, const char *name)
  671. {
  672. PCIBridge *s;
  673. s = (PCIBridge *)pci_register_device(bus, name, sizeof(PCIBridge),
  674. devfn, NULL, pci_bridge_write_config);
  675. pci_config_set_vendor_id(s->dev.config, vid);
  676. pci_config_set_device_id(s->dev.config, did);
  677. s->dev.config[0x04] = 0x06; // command = bus master, pci mem
  678. s->dev.config[0x05] = 0x00;
  679. s->dev.config[0x06] = 0xa0; // status = fast back-to-back, 66MHz, no error
  680. s->dev.config[0x07] = 0x00; // status = fast devsel
  681. s->dev.config[0x08] = 0x00; // revision
  682. s->dev.config[0x09] = 0x00; // programming i/f
  683. pci_config_set_class(s->dev.config, PCI_CLASS_BRIDGE_PCI);
  684. s->dev.config[0x0D] = 0x10; // latency_timer
  685. s->dev.config[0x0E] = 0x81; // header_type
  686. s->dev.config[0x1E] = 0xa0; // secondary status
  687. s->bus = pci_register_secondary_bus(&s->dev, map_irq);
  688. return s->bus;
  689. }