linux/drivers/uwb/allocator.c
<<
>>
Prefs
   1/*
   2 * UWB reservation management.
   3 *
   4 * Copyright (C) 2008 Cambridge Silicon Radio Ltd.
   5 *
   6 * This program is free software; you can redistribute it and/or
   7 * modify it under the terms of the GNU General Public License version
   8 * 2 as published by the Free Software Foundation.
   9 *
  10 * This program is distributed in the hope that it will be useful,
  11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13 * GNU General Public License for more details.
  14 *
  15 * You should have received a copy of the GNU General Public License
  16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17 */
  18#include <linux/kernel.h>
  19#include <linux/slab.h>
  20#include <linux/uwb.h>
  21
  22#include "uwb-internal.h"
  23
  24static void uwb_rsv_fill_column_alloc(struct uwb_rsv_alloc_info *ai)
  25{
  26        int col, mas, safe_mas, unsafe_mas;
  27        unsigned char *bm = ai->bm;
  28        struct uwb_rsv_col_info *ci = ai->ci;
  29        unsigned char c;
  30
  31        for (col = ci->csi.start_col; col < UWB_NUM_ZONES; col += ci->csi.interval) {
  32    
  33                safe_mas   = ci->csi.safe_mas_per_col;
  34                unsafe_mas = ci->csi.unsafe_mas_per_col;
  35    
  36                for (mas = 0; mas < UWB_MAS_PER_ZONE; mas++ ) {
  37                        if (bm[col * UWB_MAS_PER_ZONE + mas] == 0) {
  38        
  39                                if (safe_mas > 0) {
  40                                        safe_mas--;
  41                                        c = UWB_RSV_MAS_SAFE;
  42                                } else if (unsafe_mas > 0) {
  43                                        unsafe_mas--;
  44                                        c = UWB_RSV_MAS_UNSAFE;
  45                                } else {
  46                                        break;
  47                                }
  48                                bm[col * UWB_MAS_PER_ZONE + mas] = c;
  49                        }
  50                }
  51        }
  52}
  53
  54static void uwb_rsv_fill_row_alloc(struct uwb_rsv_alloc_info *ai)
  55{
  56        int mas, col, rows;
  57        unsigned char *bm = ai->bm;
  58        struct uwb_rsv_row_info *ri = &ai->ri;
  59        unsigned char c;
  60
  61        rows = 1;
  62        c = UWB_RSV_MAS_SAFE;
  63        for (mas = UWB_MAS_PER_ZONE - 1; mas >= 0; mas--) {
  64                if (ri->avail[mas] == 1) {
  65      
  66                        if (rows > ri->used_rows) {
  67                                break;
  68                        } else if (rows > 7) {
  69                                c = UWB_RSV_MAS_UNSAFE;
  70                        }
  71
  72                        for (col = 0; col < UWB_NUM_ZONES; col++) {
  73                                if (bm[col * UWB_NUM_ZONES + mas] != UWB_RSV_MAS_NOT_AVAIL) {
  74                                        bm[col * UWB_NUM_ZONES + mas] = c;
  75                                        if(c == UWB_RSV_MAS_SAFE)
  76                                                ai->safe_allocated_mases++;
  77                                        else
  78                                                ai->unsafe_allocated_mases++;
  79                                }
  80                        }
  81                        rows++;
  82                }
  83        }
  84        ai->total_allocated_mases = ai->safe_allocated_mases + ai->unsafe_allocated_mases;
  85}
  86
  87/*
  88 * Find the best column set for a given availability, interval, num safe mas and
  89 * num unsafe mas.
  90 *
  91 * The different sets are tried in order as shown below, depending on the interval.
  92 *
  93 * interval = 16
  94 *      deep = 0
  95 *              set 1 ->  {  8 }
  96 *      deep = 1
  97 *              set 1 ->  {  4 }
  98 *              set 2 ->  { 12 }
  99 *      deep = 2
 100 *              set 1 ->  {  2 }
 101 *              set 2 ->  {  6 }
 102 *              set 3 ->  { 10 }
 103 *              set 4 ->  { 14 }
 104 *      deep = 3
 105 *              set 1 ->  {  1 }
 106 *              set 2 ->  {  3 }
 107 *              set 3 ->  {  5 }
 108 *              set 4 ->  {  7 }
 109 *              set 5 ->  {  9 }
 110 *              set 6 ->  { 11 }
 111 *              set 7 ->  { 13 }
 112 *              set 8 ->  { 15 }
 113 *
 114 * interval = 8
 115 *      deep = 0
 116 *              set 1 ->  {  4  12 }
 117 *      deep = 1
 118 *              set 1 ->  {  2  10 }
 119 *              set 2 ->  {  6  14 }
 120 *      deep = 2
 121 *              set 1 ->  {  1   9 }
 122 *              set 2 ->  {  3  11 }
 123 *              set 3 ->  {  5  13 }
 124 *              set 4 ->  {  7  15 }
 125 *
 126 * interval = 4
 127 *      deep = 0
 128 *              set 1 ->  {  2   6  10  14 }
 129 *      deep = 1
 130 *              set 1 ->  {  1   5   9  13 }
 131 *              set 2 ->  {  3   7  11  15 }
 132 *
 133 * interval = 2
 134 *      deep = 0
 135 *              set 1 ->  {  1   3   5   7   9  11  13  15 }
 136 */
 137static int uwb_rsv_find_best_column_set(struct uwb_rsv_alloc_info *ai, int interval, 
 138                                        int num_safe_mas, int num_unsafe_mas)
 139{
 140        struct uwb_rsv_col_info *ci = ai->ci;
 141        struct uwb_rsv_col_set_info *csi = &ci->csi;
 142        struct uwb_rsv_col_set_info tmp_csi;
 143        int deep, set, col, start_col_deep, col_start_set;
 144        int start_col, max_mas_in_set, lowest_max_mas_in_deep;
 145        int n_mas;
 146        int found = UWB_RSV_ALLOC_NOT_FOUND; 
 147
 148        tmp_csi.start_col = 0;
 149        start_col_deep = interval;
 150        n_mas = num_unsafe_mas + num_safe_mas;
 151
 152        for (deep = 0; ((interval >> deep) & 0x1) == 0; deep++) {
 153                start_col_deep /= 2;
 154                col_start_set = 0;
 155                lowest_max_mas_in_deep = UWB_MAS_PER_ZONE;
 156
 157                for (set = 1; set <= (1 << deep); set++) {
 158                        max_mas_in_set = 0;
 159                        start_col = start_col_deep + col_start_set;
 160                        for (col = start_col; col < UWB_NUM_ZONES; col += interval) {
 161                
 162                                if (ci[col].max_avail_safe >= num_safe_mas &&
 163                                    ci[col].max_avail_unsafe >= n_mas) {
 164                                        if (ci[col].highest_mas[n_mas] > max_mas_in_set)
 165                                                max_mas_in_set = ci[col].highest_mas[n_mas];
 166                                } else {
 167                                        max_mas_in_set = 0;
 168                                        break;
 169                                }
 170                        }
 171                        if ((lowest_max_mas_in_deep > max_mas_in_set) && max_mas_in_set) {
 172                                lowest_max_mas_in_deep = max_mas_in_set;
 173
 174                                tmp_csi.start_col = start_col;
 175                        }
 176                        col_start_set += (interval >> deep);
 177                }
 178
 179                if (lowest_max_mas_in_deep < 8) {
 180                        csi->start_col = tmp_csi.start_col;
 181                        found = UWB_RSV_ALLOC_FOUND;
 182                        break;
 183                } else if ((lowest_max_mas_in_deep > 8) && 
 184                           (lowest_max_mas_in_deep != UWB_MAS_PER_ZONE) &&
 185                           (found == UWB_RSV_ALLOC_NOT_FOUND)) {
 186                        csi->start_col = tmp_csi.start_col;
 187                        found = UWB_RSV_ALLOC_FOUND;
 188                }
 189        }
 190
 191        if (found == UWB_RSV_ALLOC_FOUND) {
 192                csi->interval = interval;
 193                csi->safe_mas_per_col = num_safe_mas;
 194                csi->unsafe_mas_per_col = num_unsafe_mas;
 195
 196                ai->safe_allocated_mases = (UWB_NUM_ZONES / interval) * num_safe_mas;
 197                ai->unsafe_allocated_mases = (UWB_NUM_ZONES / interval) * num_unsafe_mas;
 198                ai->total_allocated_mases = ai->safe_allocated_mases + ai->unsafe_allocated_mases;
 199                ai->interval = interval;                
 200        }
 201        return found;
 202}
 203
 204static void get_row_descriptors(struct uwb_rsv_alloc_info *ai)
 205{
 206        unsigned char *bm = ai->bm;
 207        struct uwb_rsv_row_info *ri = &ai->ri;
 208        int col, mas;
 209  
 210        ri->free_rows = 16;
 211        for (mas = 0; mas < UWB_MAS_PER_ZONE; mas ++) {
 212                ri->avail[mas] = 1;
 213                for (col = 1; col < UWB_NUM_ZONES; col++) {
 214                        if (bm[col * UWB_NUM_ZONES + mas] == UWB_RSV_MAS_NOT_AVAIL) {
 215                                ri->free_rows--;
 216                                ri->avail[mas]=0;
 217                                break;
 218                        }
 219                }
 220        }
 221}
 222
 223static void uwb_rsv_fill_column_info(unsigned char *bm, int column, struct uwb_rsv_col_info *rci)
 224{
 225        int mas;
 226        int block_count = 0, start_block = 0; 
 227        int previous_avail = 0;
 228        int available = 0;
 229        int safe_mas_in_row[UWB_MAS_PER_ZONE] = {
 230                8, 7, 6, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 2, 1,
 231        };
 232
 233        rci->max_avail_safe = 0;
 234
 235        for (mas = 0; mas < UWB_MAS_PER_ZONE; mas ++) {
 236                if (!bm[column * UWB_NUM_ZONES + mas]) {
 237                        available++;
 238                        rci->max_avail_unsafe = available;
 239
 240                        rci->highest_mas[available] = mas;
 241
 242                        if (previous_avail) {
 243                                block_count++;
 244                                if ((block_count > safe_mas_in_row[start_block]) &&
 245                                    (!rci->max_avail_safe))
 246                                        rci->max_avail_safe = available - 1;
 247                        } else {
 248                                previous_avail = 1;
 249                                start_block = mas;
 250                                block_count = 1;
 251                        }
 252                } else {
 253                        previous_avail = 0;
 254                }
 255        }
 256        if (!rci->max_avail_safe)
 257                rci->max_avail_safe = rci->max_avail_unsafe;
 258}
 259
 260static void get_column_descriptors(struct uwb_rsv_alloc_info *ai)
 261{
 262        unsigned char *bm = ai->bm;
 263        struct uwb_rsv_col_info *ci = ai->ci;
 264        int col;
 265
 266        for (col = 1; col < UWB_NUM_ZONES; col++) {
 267                uwb_rsv_fill_column_info(bm, col, &ci[col]);
 268        }
 269}
 270
 271static int uwb_rsv_find_best_row_alloc(struct uwb_rsv_alloc_info *ai)
 272{
 273        int n_rows;
 274        int max_rows = ai->max_mas / UWB_USABLE_MAS_PER_ROW;
 275        int min_rows = ai->min_mas / UWB_USABLE_MAS_PER_ROW;
 276        if (ai->min_mas % UWB_USABLE_MAS_PER_ROW)
 277                min_rows++;
 278        for (n_rows = max_rows; n_rows >= min_rows; n_rows--) {
 279                if (n_rows <= ai->ri.free_rows) {
 280                        ai->ri.used_rows = n_rows;
 281                        ai->interval = 1; /* row reservation */
 282                        uwb_rsv_fill_row_alloc(ai);
 283                        return UWB_RSV_ALLOC_FOUND;
 284                }
 285        }  
 286        return UWB_RSV_ALLOC_NOT_FOUND;
 287}
 288
 289static int uwb_rsv_find_best_col_alloc(struct uwb_rsv_alloc_info *ai, int interval)
 290{
 291        int n_safe, n_unsafe, n_mas;  
 292        int n_column = UWB_NUM_ZONES / interval;
 293        int max_per_zone = ai->max_mas / n_column;
 294        int min_per_zone = ai->min_mas / n_column;
 295
 296        if (ai->min_mas % n_column)
 297                min_per_zone++;
 298
 299        if (min_per_zone > UWB_MAS_PER_ZONE) {
 300                return UWB_RSV_ALLOC_NOT_FOUND;
 301        }
 302    
 303        if (max_per_zone > UWB_MAS_PER_ZONE) {
 304                max_per_zone = UWB_MAS_PER_ZONE;
 305        }
 306    
 307        for (n_mas = max_per_zone; n_mas >= min_per_zone; n_mas--) {
 308                if (uwb_rsv_find_best_column_set(ai, interval, 0, n_mas) == UWB_RSV_ALLOC_NOT_FOUND)
 = 1;
 249     ef="d"L249" class="li323b/allocator.c#L285" id="L285" class="line07 csi 16;
31="drivers/uwb/allocatef="+code=n_mas" class>n_mas;  
 281        int n_mas;  
n_mas;  
mas <3 = 1; /* row rese#L292" id="L292" class="line" name=="L281"> 281        int n_mas;  
  class="sref">avail[3a hre3="+code=mas" class="sr=ai" class="sref">ai, interval, 0, n_mas) == UWB_RSV_ALLOmas" class="sref">n_mas;  
 192                csi->col<3a> &l3; /* row resea href="+code=ci" clr.c#L283" id="L283" class="line" =ci" clr.c#L3"> 283                        return col<3a> * 3a href="+code=UWB_NUM_ZONES" ccccccccc4" id="L284" class="line" name="L284"> 284                }
free_rows--;
3vail[ 218    or.c#L307" id="L307" class="line" nam-"sref">uw="line" name="L219"> 2193/a>  3             }or.c#L301" id="L301" class="line" name="L301"> 301        }
 220    3   }
31tor.c#L271" id="L271" class="line" name="3" name="L321"> 221}
u3L222"> 223
uwb_rslass="sio           uwb_rslass="sio i, int                         } els," class="sref">n_mas =  2233/a>static void intervresuls
(unsigned 3har *3a hrefi" class="sref">csi-> 223        int interval)
 226        a>        int max_per_zonestart_block = 0; 
max_per_zonemax_per_zoneuwrivers/uwb/allocator.c#L329" i3="L229" class=allocator.c#L290" id="L290" class="lin="L281"> 281  kzr.c#L283" id="L283" kzr.c#Li,izeof>, int interval)
 215      GFP_KERNE"rn [UWB_MAS="drivers/uwb/al"drivers/uwb/allocator.c#L273" id="L273" class="line" name=3f="driver3/uwb/allocator.c#L231" i3="L233" class="line" name="Lor.c#L301" id="L301" class="line" name="L301"> 301        }
 232
n_column)
 2233/a>        n_column)
max_ava3l_saf3 = 0;
n_column)
      ne" n href="drivers/uwbal                  int  223/a>        for (mas = 0; mas <3ai->column * <3 href="+code=U="L282"> 282         ine" the not         } vec="+ from the         } bm++;3 283         =itrivdex  int  263        struct uwode=max_avail_unsafe" cl3ss="s3ef">max_avail_unsafen_column)
 241
 215                               locator.c#L302" id="L302" class="lineew[                     3  rci->3a href="+3ode=highest_mas" class="3ref">3ighest_masn_column" class="sref">n_column)
      ne" n = 1csi" class="sref">csi-> 2423/a>                     3  if 34l = 1;  *ai83                        return previou3_avail) {
ai, ai83                        return  192                ++;
b3ock_count > max_avail_safegoto/ref="drivers/uwef="drnotr href="drivers/uwb/aef="drnotr href    locator.c#L302" id="L302" class="line=ode=colu34" class="sref">column 2434#L228_zone" class="sref">max_per_zonepre3ious_avail = 1;
uwb_rsv_alloc_info *ai83                        return [ = mhref="+code=UWB_RSV_ALLOC_FOUND" cl3f">block_3ount = 1;
csi-> 2523/a>  3             } else {
ss="sref">ai, n_column)
      ne" chref="+code=UWB_RSV_ALLOC_FOUND" cl353" class3"line" name="L253"> 253<3a>   3                    ai, aiclass="sref">n_mas) == UWB_RSV_ALac#L309" id="L309" class="line" na92"> 192                 255    3   }
3a href="drivers/uwb/alail_safegoto/ref="drivers/uwef="dr href="drivers/uwb/aef="dr href    locator.c#L302" id="L302" class="line" name="L356"> 256        if (3rci->ma3_avai3_saferci-> =="L282"> 282         try                 253
uwalloc_info *ai83                        return  2603/a>static void UWB_MAS_PER_ZONE) a href="+code=intev_alloc_info *ai83                        return  192                (struct  263        unsigned cha3 *ai-&3t;get_row_3/uwb/allo3ator.c#L265" id="L265" c3ass="3ine" namref="drivers/uwef="dr href="drivers/uwb/aef="dr href    :_descriptors" class="sref">get_row_3/ name="L3/a>        for (interval83         resulss="sref">intervresuls
 263        struct col <3interval83         resulss="sref">intervresuls
 263        st       f="+code=col" clal      f==uwbs/uwb/allocator.c93" clasMAr.c#L293" id="L293" clasMArtor.clocator.c#L302" id="L302" class="lineo id="L243ll_column_info( 282         ine" the      aef 269}
; ref="drivers/uwb/allocator.cMAr.c#L293" id="L293" clasMArtor.a href="+code=mas=itrivdex  int  273
rin_column)
> 241
        }
 2713/a>static int used_rows 283         =itrivdex  int intervresuls
 263        struct rin_column)
> 241
        }
ai);
 283         =itrivdex  int intervresuls
 263        st       f="+code=col" clal      f==uwbclocator.c#L302" id="L302" class="linebl_inforci->ai-3gt;interval83         resulss="sref">intervresuls
 263        struct intervresuls
 263        struct intervresuls
 263        st       f="+code=col" clal      f==uwbs/uwb/allocator.c93" clasMAr.c#L293" id="L293" clasMArtor.clocator.c#L302" id="L302" class="lineef="+code3ai" class="sref">ai-3gt;ai->min_mas 3#37; 37="+code=UWB_Nallocator.c#L29resulss="sref">intervresuls
 263        st2" id="L292" class=e" name== umn" class="sref">n_column;
intervresuls
 263        st      "+code=col" clal     ne" n href="drivers/uwn_column;
max_3ows;  273="+code=ai" class="sref"3ai83                        return aiocator.c#L302" id="L302" class="linehref="+co3e=ri" class="sref">ri 284                }
 =/uwb/allocator.c#L285" id="L285" cl3h="+code=3rsv_fill_row_alloc(<3 href3"+code=aref="drivers/uwef="drnotr href="drivers/uwb/aef="drnotr href    :_descriptors" class="sref">get_row_3ss="sref"3UWB_RSV_ALLOC_FOUND;3 = 0;
83                        return get_row_3sf="+code3name="L285"> 285    3   } 3
 301        }
 286        re3urn <3 href=class="sref">rci->


The original LXR software by the ass="srefhttp://sourceodege.net/projects/lxr">LXR /a> unity=uwbs/this experi al cilxr@ lxr.Redpill L/apro Artor., provider of L/auxab/asulsing aef