Patchwork [V2,01/10] hw/sd.c: convert wp_groups in SDState to bitfield

login
register
mail settings
Submitter Mitsyanko Igor
Date April 5, 2012, 3:48 p.m.
Message ID <1333640913-16028-2-git-send-email-i.mitsyanko@samsung.com>
Download mbox | patch
Permalink /patch/151170/
State New
Headers show

Comments

Mitsyanko Igor - April 5, 2012, 3:48 p.m.
Representing each group write protection flag with only one bit instead of int
variable significantly reduces memory consumption.

Signed-off-by: Igor Mitsyanko <i.mitsyanko@samsung.com>
---
 hw/sd.c |   33 +++++++++++++++++++--------------
 1 files changed, 19 insertions(+), 14 deletions(-)
Peter Maydell - April 11, 2012, 10:12 a.m.
On 5 April 2012 16:48, Igor Mitsyanko <i.mitsyanko@samsung.com> wrote:
> @@ -536,8 +541,8 @@ static void sd_function_switch(SDState *sd, uint32_t arg)
>
>  static inline int sd_wp_addr(SDState *sd, uint32_t addr)
>  {
> -    return sd->wp_groups[addr >>
> -            (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT)];
> +    return test_bit(addr >> (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT),
> +            sd->wp_groups);

Looking at how often the expression "addr >> (HWBLOCK_SHIFT +
SECTOR_SHIFT + WPGROUP_SHIFT)"
turns up in this file, I suspect that it would be helpful to have
a function
static uint32_t addr_to_wpnum(uint64_t addr) {
    return addr >> (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT);
}

But that should be in a separate patch, and it's optional.

>  }
>
>  static void sd_lock_command(SDState *sd)
> @@ -560,8 +565,8 @@ static void sd_lock_command(SDState *sd)
>             sd->card_status |= LOCK_UNLOCK_FAILED;
>             return;
>         }
> -        memset(sd->wp_groups, 0, sizeof(int) * (sd->size >>
> -                        (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT)));
> +        bitmap_zero(sd->wp_groups, BITS_TO_LONGS((sd->size >> (HWBLOCK_SHIFT +
> +                SECTOR_SHIFT + WPGROUP_SHIFT)) + 1));

This is wrong -- bitmap_zero() takes a bit count, so you don't need
to do BITS_TO_LONGS. Also where has the + 1 come from?

Otherwise looks good.

-- PMM
Mitsyanko Igor - April 11, 2012, 11:57 a.m.
On 04/11/2012 02:12 PM, Peter Maydell wrote:
> On 5 April 2012 16:48, Igor Mitsyanko<i.mitsyanko@samsung.com>  wrote:
>> @@ -536,8 +541,8 @@ static void sd_function_switch(SDState *sd, uint32_t arg)
>>
>>   static inline int sd_wp_addr(SDState *sd, uint32_t addr)
>>   {

I've just noticed that it truncates addr to 32 bits... And test_bit() 
and bitmap_new() takes int argument. Do we care about this?

>> -    return sd->wp_groups[addr>>
>> -            (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT)];
>> +    return test_bit(addr>>  (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT),
>> +            sd->wp_groups);
>
> Looking at how often the expression "addr>>  (HWBLOCK_SHIFT +
> SECTOR_SHIFT + WPGROUP_SHIFT)"
> turns up in this file, I suspect that it would be helpful to have
> a function
> static uint32_t addr_to_wpnum(uint64_t addr) {
>      return addr>>  (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT);
> }
>

This implicitly limits max address to 0xFFFFFFFF << (HWBLOCK_SHIFT + 
SECTOR_SHIFT + WPGROUP_SHIFT), have you done this on purpose?

> But that should be in a separate patch, and it's optional.
>

OK, I like it.

>>   }
>>
>>   static void sd_lock_command(SDState *sd)
>> @@ -560,8 +565,8 @@ static void sd_lock_command(SDState *sd)
>>              sd->card_status |= LOCK_UNLOCK_FAILED;
>>              return;
>>          }
>> -        memset(sd->wp_groups, 0, sizeof(int) * (sd->size>>
>> -                        (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT)));
>> +        bitmap_zero(sd->wp_groups, BITS_TO_LONGS((sd->size>>  (HWBLOCK_SHIFT +
>> +                SECTOR_SHIFT + WPGROUP_SHIFT)) + 1));
>
> This is wrong -- bitmap_zero() takes a bit count, so you don't need
> to do BITS_TO_LONGS. Also where has the + 1 come from?
>
Will fix it.
> Otherwise looks good.
>
> -- PMM
>
>
Peter Maydell - April 11, 2012, 12:15 p.m.
On 11 April 2012 12:57, Igor Mitsyanko <i.mitsyanko@samsung.com> wrote:
> On 04/11/2012 02:12 PM, Peter Maydell wrote:
>>
>> On 5 April 2012 16:48, Igor Mitsyanko<i.mitsyanko@samsung.com>  wrote:
>>>
>>> @@ -536,8 +541,8 @@ static void sd_function_switch(SDState *sd, uint32_t
>>> arg)
>>>
>>>  static inline int sd_wp_addr(SDState *sd, uint32_t addr)
>>>  {
>
>
> I've just noticed that it truncates addr to 32 bits... And test_bit() and
> bitmap_new() takes int argument. Do we care about this?

We definitely don't want to restrict the SD card image size to
32 bits, so any byte addresses must be uint64_t, and you're
correct that sd_wp_addr is wrong here.

I don't think we care that the bitmap size is limited to
possibly be 32 bits only on a 32 bit system, because there's
only one bit per wp group, and so it doesn't impose much of a
restriction on the file size.

>>> -    return sd->wp_groups[addr>>
>>> -            (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT)];
>>> +    return test_bit(addr>>  (HWBLOCK_SHIFT + SECTOR_SHIFT +
>>> WPGROUP_SHIFT),
>>> +            sd->wp_groups);
>>
>>
>> Looking at how often the expression "addr>>  (HWBLOCK_SHIFT +
>> SECTOR_SHIFT + WPGROUP_SHIFT)"
>> turns up in this file, I suspect that it would be helpful to have
>> a function
>> static uint32_t addr_to_wpnum(uint64_t addr) {
>>     return addr>>  (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT);
>> }
>>
>
> This implicitly limits max address to 0xFFFFFFFF << (HWBLOCK_SHIFT +
> SECTOR_SHIFT + WPGROUP_SHIFT), have you done this on purpose?

You could argue for uint64_t return type, but we have to pass
the result to the bitmap functions anyway, so there is implicitly
a limit. I don't think it's a particularly severe one.

If you'd rather uint64_t return here I'm happy with that: I guess
that means the sd.c code isn't putting any extra limits beyond
what the bitmap code is, so it's probably better.

-- PMM
Paul Brook - April 11, 2012, 2:05 p.m.
> >> static uint32_t addr_to_wpnum(uint64_t addr) {
> >>     return addr>>  (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT);
> >> }
> > 
> > This implicitly limits max address to 0xFFFFFFFF << (HWBLOCK_SHIFT +
> > SECTOR_SHIFT + WPGROUP_SHIFT), have you done this on purpose?
> 
> You could argue for uint64_t return type, but we have to pass
> the result to the bitmap functions anyway, so there is implicitly
> a limit. I don't think it's a particularly severe one.

Isn't the correct answer the same type as the bitmap code uses for bit 
numbers?  By my reading this is "int".

Paul

Patch

diff --git a/hw/sd.c b/hw/sd.c
index 07eb263..f405b8d 100644
--- a/hw/sd.c
+++ b/hw/sd.c
@@ -32,6 +32,7 @@ 
 #include "hw.h"
 #include "block.h"
 #include "sd.h"
+#include "bitmap.h"
 
 //#define DEBUG_SD 1
 
@@ -81,7 +82,7 @@  struct SDState {
     uint8_t sd_status[64];
     uint32_t vhs;
     int wp_switch;
-    int *wp_groups;
+    unsigned long *wp_groups;
     uint64_t size;
     int blk_len;
     uint32_t erase_start;
@@ -415,7 +416,7 @@  static void sd_reset(SDState *sd, BlockDriverState *bdrv)
     if (sd->wp_groups)
         g_free(sd->wp_groups);
     sd->wp_switch = bdrv ? bdrv_is_read_only(bdrv) : 0;
-    sd->wp_groups = (int *) g_malloc0(sizeof(int) * sect);
+    sd->wp_groups = bitmap_new(sect);
     memset(sd->function_group, 0, sizeof(int) * 6);
     sd->erase_start = 0;
     sd->erase_end = 0;
@@ -484,9 +485,11 @@  static void sd_erase(SDState *sd)
     sd->erase_end = 0;
     sd->csd[14] |= 0x40;
 
-    for (i = start; i <= end; i ++)
-        if (sd->wp_groups[i])
+    for (i = start; i <= end; i++) {
+        if (test_bit(i, sd->wp_groups)) {
             sd->card_status |= WP_ERASE_SKIP;
+        }
+    }
 }
 
 static uint32_t sd_wpbits(SDState *sd, uint64_t addr)
@@ -496,9 +499,11 @@  static uint32_t sd_wpbits(SDState *sd, uint64_t addr)
 
     wpnum = addr >> (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT);
 
-    for (i = 0; i < 32; i ++, wpnum ++, addr += WPGROUP_SIZE)
-        if (addr < sd->size && sd->wp_groups[wpnum])
+    for (i = 0; i < 32; i++, wpnum++, addr += WPGROUP_SIZE) {
+        if (addr < sd->size && test_bit(wpnum, sd->wp_groups)) {
             ret |= (1 << i);
+        }
+    }
 
     return ret;
 }
@@ -536,8 +541,8 @@  static void sd_function_switch(SDState *sd, uint32_t arg)
 
 static inline int sd_wp_addr(SDState *sd, uint32_t addr)
 {
-    return sd->wp_groups[addr >>
-            (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT)];
+    return test_bit(addr >> (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT),
+            sd->wp_groups);
 }
 
 static void sd_lock_command(SDState *sd)
@@ -560,8 +565,8 @@  static void sd_lock_command(SDState *sd)
             sd->card_status |= LOCK_UNLOCK_FAILED;
             return;
         }
-        memset(sd->wp_groups, 0, sizeof(int) * (sd->size >>
-                        (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT)));
+        bitmap_zero(sd->wp_groups, BITS_TO_LONGS((sd->size >> (HWBLOCK_SHIFT +
+                SECTOR_SHIFT + WPGROUP_SHIFT)) + 1));
         sd->csd[14] &= ~0x10;
         sd->card_status &= ~CARD_IS_LOCKED;
         sd->pwd_len = 0;
@@ -1007,8 +1012,8 @@  static sd_rsp_type_t sd_normal_command(SDState *sd,
             }
 
             sd->state = sd_programming_state;
-            sd->wp_groups[addr >> (HWBLOCK_SHIFT +
-                            SECTOR_SHIFT + WPGROUP_SHIFT)] = 1;
+            set_bit(addr >> (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT),
+                    sd->wp_groups);
             /* Bzzzzzzztt .... Operation complete.  */
             sd->state = sd_transfer_state;
             return sd_r1b;
@@ -1027,8 +1032,8 @@  static sd_rsp_type_t sd_normal_command(SDState *sd,
             }
 
             sd->state = sd_programming_state;
-            sd->wp_groups[addr >> (HWBLOCK_SHIFT +
-                            SECTOR_SHIFT + WPGROUP_SHIFT)] = 0;
+            clear_bit(addr >> (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT),
+                    sd->wp_groups);
             /* Bzzzzzzztt .... Operation complete.  */
             sd->state = sd_transfer_state;
             return sd_r1b;