summaryrefslogtreecommitdiffhomepage
path: root/Private/KernelKit/PCI/Device.hpp
blob: d3c58e9694fca97ec5417eece73595455ee68b99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 *	========================================================
 *
 *	HCore
 * 	Copyright Mahrouss Logic, all rights reserved.
 *
 * 	========================================================
 */
#pragma once

#include <NewKit/Defines.hpp>

namespace HCore::PCI
{
    enum class PciConfigKind : UShort
    {
        ConfigAddress = 0xCF8,
        ConfigData = 0xCFC,
        Invalid = 0xFFF
    };

    class Device final
    {
    public:
        Device() = default;

    public:
        explicit Device(UShort bus, UShort device, UShort function, UShort bar);

        Device &operator=(const Device &) = default;

        Device(const Device &) = default;

        ~Device();

    public:
        UInt Read(UInt bar, Size szData);
        void Write(UInt bar, UIntPtr data, Size szData);

    public:
        operator bool();

    public:
        template<typename T>
        UInt Read(UInt bar)
        {
            static_assert(sizeof(T) <= 4, "64-bit PCI addressing is unsupported");
            return Read(bar, sizeof(T));
        }

        template<typename T>
        void Write(UInt bar, UIntPtr data)
        {
            static_assert(sizeof(T) <= 4, "64-bit PCI addressing is unsupported");
            Write(bar, data, sizeof(T));
        }

    public:
        UShort DeviceId();
        UShort VendorId();
        UShort InterfaceId();
        UChar Class();
        UChar Subclass();
        UChar ProgIf();
        UChar HeaderType();

    public:
        void EnableMmio();
        void BecomeBusMaster(); // for PCI-DMA, PC-DMA does not need that.

        UShort Vendor();

    private:
        UShort m_Bus;
        UShort m_Device;
        UShort m_Function;
        UShort m_Bar;

    };
} // namespace HCore::PCI


extern "C" void LumiaPCISetCfgTarget(HCore::UInt bar);
extern "C" HCore::UInt LumiaPCIReadRaw(HCore::UInt bar);