blob: 1d9de718fdb2984e8c60321e52d6c732add5f283 [file] [log] [blame]
Isaku Yamahata58a38e82010-10-28 15:54:35 +09001// helper functions to manage pci io/memory/prefetch memory region
2//
3// Copyright (C) 2009 Isaku Yamahata <yamahata at valinux co jp>
4//
5// This file may be distributed under the terms of the GNU LGPLv3 license.
6//
7//
8
9#include "util.h"
10
11#define PCI_REGION_DISABLED (-1)
12
13void pci_region_init(struct pci_region *r, u32 first, u32 last)
14{
15 r->first = first;
16 r->last = last;
17
18 r->cur_first = r->first;
19}
20
21// PCI_REGION_DISABLED represents that the region is in special state.
22// its value is chosen such that cur_first can't be PCI_REGION_DISABLED
23// normally.
24// NOTE: the area right below 4G is used for LAPIC, so such area can't
25// be used for PCI memory.
26u32 pci_region_disable(struct pci_region *r)
27{
28 return r->cur_first = PCI_REGION_DISABLED;
29}
30
31static int pci_region_disabled(const struct pci_region *r)
32{
33 return r->cur_first == PCI_REGION_DISABLED;
34}
35
36static u32 pci_region_alloc_align(struct pci_region *r, u32 size, u32 align)
37{
38 if (pci_region_disabled(r)) {
39 return 0;
40 }
41
42 u32 s = ALIGN(r->cur_first, align);
43 if (s > r->last || s < r->cur_first) {
44 return 0;
45 }
46 u32 e = s + size;
47 if (e < s || e - 1 > r->last) {
48 return 0;
49 }
50 r->cur_first = e;
51 return s;
52}
53
54u32 pci_region_alloc(struct pci_region *r, u32 size)
55{
56 return pci_region_alloc_align(r, size, size);
57}
58
59u32 pci_region_align(struct pci_region *r, u32 align)
60{
61 return pci_region_alloc_align(r, 0, align);
62}
63
64void pci_region_revert(struct pci_region *r, u32 addr)
65{
66 r->cur_first = addr;
67}
68
69u32 pci_region_addr(const struct pci_region *r)
70{
71 return r->cur_first;
72}
73
74u32 pci_region_size(const struct pci_region *r)
75{
76 return r->last - r->first + 1;
77}