summaryrefslogtreecommitdiffhomepage
path: root/dev/kernel/HALKit/AMD64/PCI/DMA.cc
blob: 4a63625e91f173c9b146681d3a56ee1dc419b088 (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
85
/* -------------------------------------------

	Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved.

------------------------------------------- */

#include <KernelKit/PCI/DMA.h>
#include <ArchKit/ArchKit.h>

namespace Kernel
{
	DMAWrapper::operator bool()
	{
		return this->fAddress;
	}

	bool DMAWrapper::operator!()
	{
		return !this->fAddress;
	}

	Boolean DMAWrapper::Check(UIntPtr offset) const
	{
		if (!this->fAddress)
			return false;

		if (offset == 0)
			return false;

		kout << "[DMAWrapper::IsIn] Checking offset...\r";
		return reinterpret_cast<UIntPtr>(this->fAddress) >= offset;
	}

	bool DMAWrapper::Write(UIntPtr& bit, const UInt32& offset)
	{
		kout << "[DMAWrapper::Read] Checking this->fAddress...\r";

		if (!this->fAddress)
			return false;

		(void)(kout << "[DMAWrapper::Write] Writing at address: " << hex_number(reinterpret_cast<UIntPtr>(this->fAddress) + offset) << kendl);

		ke_dma_write<UInt32>(reinterpret_cast<UIntPtr>(this->fAddress), offset, bit);

		return true;
	}

	UIntPtr DMAWrapper::Read(const UInt32& offset)
	{
		kout << "[DMAWrapper::Read] Checking this->fAddress...\r";

		if (!this->fAddress)
			return ~0;

		(void)(kout << "[DMAWrapper::Write] Writing at address: " << hex_number(reinterpret_cast<UIntPtr>(this->fAddress) + offset) << kendl);

		return (UIntPtr)ke_dma_read<UInt32>(reinterpret_cast<UIntPtr>(this->fAddress), offset);
	}

	UIntPtr DMAWrapper::operator[](UIntPtr& offset)
	{
		return this->Read(offset);
	}

	OwnPtr<IOBuf<Char*>> DMAFactory::Construct(OwnPtr<DMAWrapper>& dma)
	{
		if (!dma)
			return {};

		OwnPtr<IOBuf<Char*>> dmaOwnPtr =
			mm_make_own_ptr<IOBuf<Char*>, char*>(reinterpret_cast<char*>(dma->fAddress));

		if (!dmaOwnPtr)
			return {};

		kout << "Returning the new OwnPtr<IOBuf<Char*>>!\r";
		return dmaOwnPtr;
	}

	DMAWrapper& DMAWrapper::operator=(voidPtr Ptr)
	{
		this->fAddress = Ptr;
		return *this;
	}
} // namespace Kernel