| 1 | /////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 2 | // File     : pmem.c | 
|---|
| 3 | // Date     : 01/07/2012 | 
|---|
| 4 | // Author   : alain greiner | 
|---|
| 5 | // Copyright (c) UPMC-LIP6 | 
|---|
| 6 | /////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 7 |  | 
|---|
| 8 | #include <tty0.h> | 
|---|
| 9 | #include <utils.h> | 
|---|
| 10 | #include <pmem.h> | 
|---|
| 11 | #include <giet_config.h> | 
|---|
| 12 |  | 
|---|
| 13 | /////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 14 | //     Global variable : array of physical memory allocators (one per cluster) | 
|---|
| 15 | /////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 16 |  | 
|---|
| 17 | extern pmem_alloc_t _boot_pmem_alloc[X_SIZE][Y_SIZE]; | 
|---|
| 18 |  | 
|---|
| 19 | //////////////////////////////////////// | 
|---|
| 20 | void _pmem_alloc_init( unsigned int x, | 
|---|
| 21 | unsigned int y, | 
|---|
| 22 | unsigned int base, | 
|---|
| 23 | unsigned int size ) | 
|---|
| 24 | { | 
|---|
| 25 | if ( (base & 0x1FFFFF) || (size & 0x1FFFFF) ) | 
|---|
| 26 | { | 
|---|
| 27 | _puts("\n[GIET ERROR] in _pmem_alloc_init() : pseg in cluster["); | 
|---|
| 28 | _putd( x ); | 
|---|
| 29 | _puts(","); | 
|---|
| 30 | _putd( y ); | 
|---|
| 31 | _puts("] not aligned on 2 Mbytes\n"); | 
|---|
| 32 | _exit(); | 
|---|
| 33 | } | 
|---|
| 34 |  | 
|---|
| 35 | pmem_alloc_t* p       = &_boot_pmem_alloc[x][y]; | 
|---|
| 36 |  | 
|---|
| 37 | unsigned int  bppi_min = base >> 21; | 
|---|
| 38 | unsigned int  bppi_max = (base + size) >> 21; | 
|---|
| 39 |  | 
|---|
| 40 | p->x        = x; | 
|---|
| 41 | p->y        = y; | 
|---|
| 42 |  | 
|---|
| 43 | p->nxt_bppi = bppi_min; | 
|---|
| 44 | p->max_bppi = bppi_max; | 
|---|
| 45 |  | 
|---|
| 46 | p->nxt_sppi = 0; | 
|---|
| 47 | p->max_sppi = 0; | 
|---|
| 48 |  | 
|---|
| 49 | // The two first BPPs in cluster[0][0] are reserved | 
|---|
| 50 | // for the segments used by the boot-loader | 
|---|
| 51 | if ( (x==0) && (y==0) ) p->nxt_bppi = p->nxt_bppi + 2; | 
|---|
| 52 |  | 
|---|
| 53 | } // end pmem_alloc_init() | 
|---|
| 54 |  | 
|---|
| 55 | ///////////////////////////////////////////// | 
|---|
| 56 | unsigned int _get_big_ppn( pmem_alloc_t*  p, | 
|---|
| 57 | unsigned int   n ) | 
|---|
| 58 | { | 
|---|
| 59 | unsigned int x   = p->x; | 
|---|
| 60 | unsigned int y   = p->y; | 
|---|
| 61 | unsigned int bpi = p->nxt_bppi;  // bpi : BPPI of the first allocated big page | 
|---|
| 62 |  | 
|---|
| 63 | if ( (bpi + n) > p->max_bppi ) | 
|---|
| 64 | { | 
|---|
| 65 | _puts("\n[GIET ERROR] in _get_big_ppn() : not enough BPP in cluster["); | 
|---|
| 66 | _putd( x ); | 
|---|
| 67 | _puts(","); | 
|---|
| 68 | _putd( y ); | 
|---|
| 69 | _puts("]\n"); | 
|---|
| 70 | _exit(); | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | // update allocator state | 
|---|
| 74 | p->nxt_bppi = bpi + n; | 
|---|
| 75 |  | 
|---|
| 76 | return (x << 24) + (y << 20) + (bpi << 9); | 
|---|
| 77 |  | 
|---|
| 78 | } // end get_big_ppn() | 
|---|
| 79 |  | 
|---|
| 80 | /////////////////////////////////////////////// | 
|---|
| 81 | unsigned int _get_small_ppn( pmem_alloc_t*  p, | 
|---|
| 82 | unsigned int   n ) | 
|---|
| 83 | { | 
|---|
| 84 | unsigned int x    = p->x; | 
|---|
| 85 | unsigned int y    = p->y; | 
|---|
| 86 | unsigned int spi  = p->nxt_sppi;   // spi : SPPI of the first allocated small page | 
|---|
| 87 | unsigned int bpi  = p->spp_bppi;   // bpi : BPPI of the first allocated small page | 
|---|
| 88 |  | 
|---|
| 89 | // get a new big page if not enough contiguous small pages | 
|---|
| 90 | // in the big page currently used to allocate small pages | 
|---|
| 91 | if ( spi + n > p->max_sppi ) | 
|---|
| 92 | { | 
|---|
| 93 | if ( p->nxt_bppi + 1 > p->max_bppi ) | 
|---|
| 94 | { | 
|---|
| 95 | _puts("\n[GIET ERROR] in _get_small_ppn() : not enough BPP in cluster["); | 
|---|
| 96 | _putd( x ); | 
|---|
| 97 | _puts(","); | 
|---|
| 98 | _putd( y ); | 
|---|
| 99 | _puts("]\n"); | 
|---|
| 100 | _exit(); | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | // update the allocator state for the new big page | 
|---|
| 104 | p->spp_bppi = p->nxt_bppi; | 
|---|
| 105 | p->nxt_bppi = p->nxt_bppi + 1; | 
|---|
| 106 | p->nxt_sppi = 0; | 
|---|
| 107 | p->max_sppi = 512; | 
|---|
| 108 |  | 
|---|
| 109 | // set the spi and bpi values | 
|---|
| 110 | spi = p->nxt_sppi; | 
|---|
| 111 | bpi = p->spp_bppi; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | // update allocator state for the n small pages | 
|---|
| 115 | p->nxt_sppi = p->nxt_sppi + n; | 
|---|
| 116 |  | 
|---|
| 117 | return (x << 24) + (y << 20) + (bpi << 9) + spi; | 
|---|
| 118 |  | 
|---|
| 119 | } // end _get_small_ppn() | 
|---|
| 120 |  | 
|---|
| 121 |  | 
|---|
| 122 |  | 
|---|
| 123 | // Local Variables: | 
|---|
| 124 | // tab-width: 4 | 
|---|
| 125 | // c-basic-offset: 4 | 
|---|
| 126 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) | 
|---|
| 127 | // indent-tabs-mode: nil | 
|---|
| 128 | // End: | 
|---|
| 129 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 | 
|---|
| 130 |  | 
|---|
| 131 |  | 
|---|