accessors.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* SPDX-License-Identifier: MIT */
  2. /* SPDX-FileCopyrightText: 2023 Nicholas Chin */
  3. #if defined(__linux__)
  4. #include <sys/io.h>
  5. #endif
  6. #if defined(__OpenBSD__)
  7. #include <machine/sysarch.h>
  8. #include <sys/types.h>
  9. #if defined(__amd64__)
  10. #include <amd64/pio.h>
  11. #elif defined(__i386__)
  12. #include <i386/pio.h>
  13. #endif /* __i386__ */
  14. #endif /* __OpenBSD__ */
  15. #include <errno.h>
  16. #include "accessors.h"
  17. uint32_t
  18. pci_read_32(uint32_t dev, uint8_t reg)
  19. {
  20. sys_outl(PCI_CFG_ADDR, dev | reg);
  21. return sys_inl(PCI_CFG_DATA);
  22. }
  23. void
  24. pci_write_32(uint32_t dev, uint8_t reg, uint32_t value)
  25. {
  26. sys_outl(PCI_CFG_ADDR, dev | reg);
  27. sys_outl(PCI_CFG_DATA, value);
  28. }
  29. void
  30. sys_outb(unsigned int port, uint8_t data)
  31. {
  32. #if defined(__linux__)
  33. outb(data, port);
  34. #endif
  35. #if defined(__OpenBSD__)
  36. outb(port, data);
  37. #endif
  38. }
  39. void
  40. sys_outl(unsigned int port, uint32_t data)
  41. {
  42. #if defined(__linux__)
  43. outl(data, port);
  44. #endif
  45. #if defined(__OpenBSD__)
  46. outl(port, data);
  47. #endif
  48. }
  49. uint8_t
  50. sys_inb(unsigned int port)
  51. {
  52. #if defined(__linux__) || defined (__OpenBSD__)
  53. return inb(port);
  54. #endif
  55. return 0;
  56. }
  57. uint32_t
  58. sys_inl(unsigned int port)
  59. {
  60. #if defined(__linux__) || defined (__OpenBSD__)
  61. return inl(port);
  62. #endif
  63. return 0;
  64. }
  65. int
  66. sys_iopl(int level)
  67. {
  68. #if defined(__linux__)
  69. return iopl(level);
  70. #endif
  71. #if defined(__OpenBSD__)
  72. #if defined(__i386__)
  73. return i386_iopl(level);
  74. #elif defined(__amd64__)
  75. return amd64_iopl(level);
  76. #endif /* __amd64__ */
  77. #endif /* __OpenBSD__ */
  78. errno = ENOSYS;
  79. return -1;
  80. }