aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/HOS/Services/Nv/NvGpuAS/NvGpuASCtx.cs
blob: 70275b2a97ff062b947b52050f9e838ac2e9f497 (plain) (blame)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using Ryujinx.Graphics.Memory;
using System.Collections.Generic;

namespace Ryujinx.HLE.HOS.Services.Nv.NvGpuAS
{
    class NvGpuASCtx
    {
        public NvGpuVmm Vmm { get; private set; }

        private class Range
        {
            public ulong Start  { get; private set; }
            public ulong End    { get; private set; }

            public Range(long Position, long Size)
            {
                Start = (ulong)Position;
                End   = (ulong)Size + Start;
            }
        }

        private class MappedMemory : Range
        {
            public long PhysicalAddress { get; private set; }
            public bool VaAllocated  { get; private set; }

            public MappedMemory(
                long Position,
                long Size,
                long PhysicalAddress,
                bool VaAllocated) : base(Position, Size)
            {
                this.PhysicalAddress = PhysicalAddress;
                this.VaAllocated     = VaAllocated;
            }
        }

        private SortedList<long, Range> Maps;
        private SortedList<long, Range> Reservations;

        public NvGpuASCtx(ServiceCtx Context)
        {
            Vmm = new NvGpuVmm(Context.Memory);

            Maps         = new SortedList<long, Range>();
            Reservations = new SortedList<long, Range>();
        }

        public bool ValidateFixedBuffer(long Position, long Size)
        {
            long MapEnd = Position + Size;

            //Check if size is valid (0 is also not allowed).
            if ((ulong)MapEnd <= (ulong)Position)
            {
                return false;
            }

            //Check if address is page aligned.
            if ((Position & NvGpuVmm.PageMask) != 0)
            {
                return false;
            }

            //Check if region is reserved.
            if (BinarySearch(Reservations, Position) == null)
            {
                return false;
            }

            //Check for overlap with already mapped buffers.
            Range Map = BinarySearchLt(Maps, MapEnd);

            if (Map != null && Map.End > (ulong)Position)
            {
                return false;
            }

            return true;
        }

        public void AddMap(
            long Position,
            long Size,
            long PhysicalAddress,
            bool VaAllocated)
        {
            Maps.Add(Position, new MappedMemory(Position, Size, PhysicalAddress, VaAllocated));
        }

        public bool RemoveMap(long Position, out long Size)
        {
            Size = 0;

            if (Maps.Remove(Position, out Range Value))
            {
                MappedMemory Map = (MappedMemory)Value;

                if (Map.VaAllocated)
                {
                    Size = (long)(Map.End - Map.Start);
                }

                return true;
            }

            return false;
        }

        public bool TryGetMapPhysicalAddress(long Position, out long PhysicalAddress)
        {
            Range Map = BinarySearch(Maps, Position);

            if (Map != null)
            {
                PhysicalAddress = ((MappedMemory)Map).PhysicalAddress;

                return true;
            }

            PhysicalAddress = 0;

            return false;
        }

        public void AddReservation(long Position, long Size)
        {
            Reservations.Add(Position, new Range(Position, Size));
        }

        public bool RemoveReservation(long Position)
        {
            return Reservations.Remove(Position);
        }

        private Range BinarySearch(SortedList<long, Range> Lst, long Position)
        {
            int Left  = 0;
            int Right = Lst.Count - 1;

            while (Left <= Right)
            {
                int Size = Right - Left;

                int Middle = Left + (Size >> 1);

                Range Rg = Lst.Values[Middle];

                if ((ulong)Position >= Rg.Start && (ulong)Position < Rg.End)
                {
                    return Rg;
                }

                if ((ulong)Position < Rg.Start)
                {
                    Right = Middle - 1;
                }
                else
                {
                    Left = Middle + 1;
                }
            }

            return null;
        }

        private Range BinarySearchLt(SortedList<long, Range> Lst, long Position)
        {
            Range LtRg = null;

            int Left  = 0;
            int Right = Lst.Count - 1;

            while (Left <= Right)
            {
                int Size = Right - Left;

                int Middle = Left + (Size >> 1);

                Range Rg = Lst.Values[Middle];

                if ((ulong)Position < Rg.Start)
                {
                    Right = Middle - 1;
                }
                else
                {
                    Left = Middle + 1;

                    if ((ulong)Position > Rg.Start)
                    {
                        LtRg = Rg;
                    }
                }
            }

            return LtRg;
        }
    }
}