test_pciutils.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import os
  2. import infra.basetest
  3. class TestPCIUtils(infra.basetest.BRTest):
  4. config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
  5. """
  6. BR2_PACKAGE_PCIUTILS=y
  7. BR2_TARGET_ROOTFS_CPIO=y
  8. # BR2_TARGET_ROOTFS_TAR is not set
  9. """
  10. def test_run(self):
  11. cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
  12. # Note: we add a qemu pci-testdev in order to have a stable
  13. # device ID, and for writing in configuration space without
  14. # interfering with the rest of the emulation. See:
  15. # https://www.qemu.org/docs/master/specs/pci-testdev.html
  16. self.emulator.boot(arch="armv5",
  17. kernel="builtin",
  18. options=["-initrd", cpio_file,
  19. "-device", "pci-testdev"])
  20. self.emulator.login()
  21. # Check the program executes. This test also check that we
  22. # have "lspci" from the pciutils package, rather than the
  23. # busybox applet (which does not recognize the --version
  24. # option)"
  25. self.assertRunOk("lspci --version")
  26. # Check few program invocations.
  27. self.assertRunOk("lspci")
  28. for lspci_opt in ["-t", "-n", "-v", "-vv", "-x"]:
  29. self.assertRunOk(f"lspci {lspci_opt}")
  30. # Check we can see the qemu pci-testdev.
  31. # Vendor: 1b36: Red Hat, Inc.
  32. # Device: 0005: QEMU PCI Test Device
  33. pci_vendor_id = "1b36"
  34. pci_device_id = "0005"
  35. pci_dev = f"{pci_vendor_id}:{pci_device_id}"
  36. cmd = f"lspci -d {pci_dev}"
  37. output, exit_code = self.emulator.run(cmd)
  38. self.assertEqual(exit_code, 0)
  39. self.assertIn("Red Hat, Inc.", output[0])
  40. self.assertIn("QEMU PCI Test Device", output[0])
  41. # We disable INTx emulation by setting bit 10 of the COMMAND
  42. # register in the configuration space. See:
  43. # https://git.kernel.org/pub/scm/utils/pciutils/pciutils.git/tree/lib/header.h?h=v3.10.0#n26
  44. dis_int_x = 0x400
  45. data_mask = f"{hex(dis_int_x)}:{hex(dis_int_x)}"
  46. cmd = f"setpci -d {pci_dev} COMMAND.w={data_mask}"
  47. self.assertRunOk(cmd)
  48. # We read back and check the value.
  49. cmd = f"setpci -d {pci_dev} COMMAND.w"
  50. output, exit_code = self.emulator.run(cmd)
  51. read_value = int(output[0], 16)
  52. self.assertEqual(exit_code, 0)
  53. self.assertTrue((read_value & dis_int_x) == dis_int_x)
  54. # We check lspci now see the disabled INTx emulation.
  55. cmd = f"lspci -vv -d {pci_dev} | grep -F 'DisINTx+'"
  56. self.assertRunOk(cmd)
  57. # We re-enable the INTx emulation by clearing the bit 10.
  58. data_mask = f"0x0:{hex(dis_int_x)}"
  59. cmd = f"setpci -d {pci_dev} COMMAND.w={data_mask}"
  60. self.assertRunOk(cmd)
  61. # We read back and check the value, again.
  62. cmd = f"setpci -d {pci_dev} COMMAND.w"
  63. output, exit_code = self.emulator.run(cmd)
  64. read_value = int(output[0], 16)
  65. self.assertEqual(exit_code, 0)
  66. self.assertTrue((read_value & dis_int_x) == 0)
  67. # We check lspci now see the enabled INTx emulation.
  68. cmd = f"lspci -vv -d {pci_dev} | grep -F 'DisINTx-'"
  69. self.assertRunOk(cmd)