PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/base/Kernel/Singularity/Io/PciPort.cs

#
C# | 128 lines | 94 code | 24 blank | 10 comment | 8 complexity | f98d047d3027d1becfc9fdfcbd67a1ec MD5 | raw file
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Microsoft Research Singularity
  4. //
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. //
  7. // File: PciConfig.cs
  8. //
  9. // Note:
  10. //
  11. #define DEBUG_PCI
  12. using System;
  13. using StringBuilder = System.Text.StringBuilder;
  14. using System.Collections;
  15. using Microsoft.Singularity;
  16. namespace Microsoft.Singularity.Io
  17. {
  18. [CLSCompliant(false)]
  19. public class PciPort
  20. {
  21. private readonly IoPort addressPort;
  22. private readonly IoPort dataPort;
  23. private readonly ushort identifier;
  24. public PciPort(IoPort addressPort, IoPort dataPort, ushort identifier)
  25. {
  26. this.addressPort = addressPort;
  27. this.dataPort = dataPort;
  28. this.identifier = identifier;
  29. }
  30. public ushort Identifier { get { return this.identifier; } }
  31. public ushort Function
  32. {
  33. get { return (ushort)(Identifier & ((1 << 3) - 1)); }
  34. }
  35. public ushort Device
  36. {
  37. get { return (ushort)((Identifier >> 3) & ((1 << 5) - 1)); }
  38. }
  39. public ushort Bus
  40. {
  41. get { return (ushort)(Identifier >> 8); }
  42. }
  43. public virtual uint Read32(int offset)
  44. {
  45. if ((offset & 0x3) != 0) {
  46. throw new Exception("BAD_OFFSET");
  47. }
  48. uint config = (((uint)offset & 0xfc) |
  49. ((uint)identifier << 8) |
  50. ((uint)1 << 31));
  51. addressPort.Write32(config);
  52. return dataPort.Read32();
  53. }
  54. public virtual ushort Read16(int offset)
  55. {
  56. if ((offset & 0x1) != 0) {
  57. throw new Exception("BAD_OFFSET");
  58. }
  59. uint config = (((uint)offset & 0xfc) |
  60. ((uint)identifier << 8) |
  61. ((uint)1 << 31));
  62. addressPort.Write32(config);
  63. return dataPort.Read16((uint)(offset & 0x2));
  64. }
  65. public virtual byte Read8(int offset)
  66. {
  67. uint config = (((uint)offset & 0xfc) |
  68. ((uint)identifier << 8) |
  69. ((uint)1 << 31));
  70. addressPort.Write32(config);
  71. return dataPort.Read8((uint)(offset & 0x3));
  72. }
  73. public virtual void Write32(int offset, uint value)
  74. {
  75. if ((offset & 0x3) != 0) {
  76. throw new Exception("BAD_OFFSET");
  77. }
  78. uint config = (((uint)offset & 0xfc) |
  79. ((uint)identifier << 8) |
  80. ((uint)1 << 31));
  81. addressPort.Write32(config);
  82. dataPort.Write32(value);
  83. }
  84. public virtual void Write16(int offset, ushort value)
  85. {
  86. if ((offset & 0x1) != 0) {
  87. throw new Exception("BAD_OFFSET");
  88. }
  89. uint config = (((uint)offset & 0xfc) |
  90. ((uint)identifier << 8) |
  91. ((uint)1 << 31));
  92. addressPort.Write32(config);
  93. dataPort.Write16((uint)(offset & 0x2), value);
  94. }
  95. public virtual void Write8(int offset, byte value)
  96. {
  97. uint config = (((uint)offset & 0xff) |
  98. ((uint)identifier << 8) |
  99. ((uint)1 << 31));
  100. addressPort.Write32(config);
  101. dataPort.Write8((uint)(offset & 0x3), value);
  102. }
  103. }
  104. } // end namespace Microsoft.Singularity.Io