aboutsummaryrefslogtreecommitdiffstats
path: root/src/pci_region.c
blob: 1d9de718fdb2984e8c60321e52d6c732add5f283 (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
// helper functions to manage pci io/memory/prefetch memory region
//
// Copyright (C) 2009 Isaku Yamahata <yamahata at valinux co jp>
//
// This file may be distributed under the terms of the GNU LGPLv3 license.
//
//

#include "util.h"

#define PCI_REGION_DISABLED     (-1)

void pci_region_init(struct pci_region *r, u32 first, u32 last)
{
    r->first = first;
    r->last = last;

    r->cur_first = r->first;
}

// PCI_REGION_DISABLED represents that the region is in special state.
// its value is chosen such that cur_first can't be PCI_REGION_DISABLED
// normally.
// NOTE: the area right below 4G is used for LAPIC, so such area can't
//       be used for PCI memory.
u32 pci_region_disable(struct pci_region *r)
{
    return r->cur_first = PCI_REGION_DISABLED;
}

static int pci_region_disabled(const struct pci_region *r)
{
    return r->cur_first == PCI_REGION_DISABLED;
}

static u32 pci_region_alloc_align(struct pci_region *r, u32 size, u32 align)
{
    if (pci_region_disabled(r)) {
        return 0;
    }

    u32 s = ALIGN(r->cur_first, align);
    if (s > r->last || s < r->cur_first) {
        return 0;
    }
    u32 e = s + size;
    if (e < s || e - 1 > r->last) {
        return 0;
    }
    r->cur_first = e;
    return s;
}

u32 pci_region_alloc(struct pci_region *r, u32 size)
{
    return pci_region_alloc_align(r, size, size);
}

u32 pci_region_align(struct pci_region *r, u32 align)
{
    return pci_region_alloc_align(r, 0, align);
}

void pci_region_revert(struct pci_region *r, u32 addr)
{
    r->cur_first = addr;
}

u32 pci_region_addr(const struct pci_region *r)
{
    return r->cur_first;
}

u32 pci_region_size(const struct pci_region *r)
{
    return r->last - r->first + 1;
}