Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / core / memblock.c
1 /*
2  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 /** @file
23  *
24  * Largest memory block
25  *
26  */
27
28 #include <stdint.h>
29 #include <ipxe/uaccess.h>
30 #include <ipxe/io.h>
31 #include <ipxe/memblock.h>
32
33 /**
34  * Find largest usable memory region
35  *
36  * @ret start           Start of region
37  * @ret len             Length of region
38  */
39 size_t largest_memblock ( userptr_t *start ) {
40         struct memory_map memmap;
41         struct memory_region *region;
42         physaddr_t max = ~( ( physaddr_t ) 0 );
43         physaddr_t region_start;
44         physaddr_t region_end;
45         size_t region_len;
46         unsigned int i;
47         size_t len = 0;
48
49         /* Avoid returning uninitialised data on error */
50         *start = UNULL;
51
52         /* Scan through all memory regions */
53         get_memmap ( &memmap );
54         for ( i = 0 ; i < memmap.count ; i++ ) {
55                 region = &memmap.regions[i];
56                 DBG ( "Considering [%llx,%llx)\n", region->start, region->end );
57
58                 /* Truncate block to maximum physical address */
59                 if ( region->start > max ) {
60                         DBG ( "...starts after maximum address %lx\n", max );
61                         continue;
62                 }
63                 region_start = region->start;
64                 if ( region->end > max ) {
65                         DBG ( "...end truncated to maximum address %lx\n", max);
66                         region_end = 0; /* =max, given the wraparound */
67                 } else {
68                         region_end = region->end;
69                 }
70                 region_len = ( region_end - region_start );
71
72                 /* Use largest block */
73                 if ( region_len > len ) {
74                         DBG ( "...new best block found\n" );
75                         *start = phys_to_user ( region_start );
76                         len = region_len;
77                 }
78         }
79
80         return len;
81 }