freemyipod r251 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r250‎ | r251 | r252 >
Date:20:28, 4 November 2010
Author:theseven
Status:new
Tags:
Comment:
emBIOS: iPod Nano 2G FTL: Get rid of some avoidable division operations and cache number of pages per block
Modified paths:
  • /embios/trunk/target/ipodnano2g/ftl.c (modified) (history)
  • /embios/trunk/target/ipodnano2g/nand.c (modified) (history)

Diff [purge]

Index: embios/trunk/target/ipodnano2g/nand.c
@@ -232,7 +232,7 @@
233233 static uint32_t nand_transfer_data_collect(uint32_t direction)
234234 {
235235 uint32_t timeout = USEC_TIMER + 20000;
236 - while ((DMAALLST & DMAALLST_DMABUSY3))
 236+ while (DMAALLST & DMAALLST_DMABUSY3)
237237 if (nand_timeout(timeout)) return 1;
238238 if (!direction) invalidate_dcache();
239239 if (nand_wait_addrdone()) return 1;
@@ -409,8 +409,6 @@
410410 {
411411 if (nand_transfer_data(bank, 0, spare, 0x40))
412412 return nand_unlock(1);
413 - if (sparebuffer)
414 - memcpy(sparebuffer, spare, 0x800);
415413 if (checkempty)
416414 rc = nand_check_empty((uint8_t*)sparebuffer) << 1;
417415 }
Index: embios/trunk/target/ipodnano2g/ftl.c
@@ -364,6 +364,9 @@
365365 /* Number of banks we detected a chip on */
366366 uint32_t ftl_banks;
367367
 368+/* Number of banks we detected a chip on (power of two) */
 369+uint32_t ftl_banks_exponent;
 370+
368371 /* Block map, used vor pBlock to vBlock mapping */
369372 static uint16_t ftl_map[0x2000] CACHEALIGN_ATTR;
370373
@@ -433,8 +436,17 @@
434437 static struct mutex ftl_mtx;
435438 bool ftl_initialized = false;
436439
 440+/* Pages per hyperblock (ftl_nand_type->pagesperblock * ftl_banks) */
 441+static uint32_t ppb;
437442
 443+/* Pages per hyperblock (power of two) */
 444+static uint32_t ppb_exponent;
438445
 446+/* Reserved hyperblocks (ftl_nand_type->blocks
 447+ - ftl_nand_type->userblocks - 0x17) */
 448+static uint32_t syshyperblocks;
 449+
 450+
439451 /* Finds a device info page for the specified bank and returns its number.
440452 Used to check if one is present, and to read the lowlevel BBT. */
441453 static uint32_t ftl_find_devinfo(uint32_t bank) INITCODE_ATTR;
@@ -853,9 +865,6 @@
854866 DEBUGF("FTL: VFL: Reading page %d", vpage);
855867 #endif
856868
857 - uint32_t ppb = ftl_nand_type->pagesperblock * ftl_banks;
858 - uint32_t syshyperblocks = ftl_nand_type->blocks
859 - - ftl_nand_type->userblocks - 0x17;
860869 uint32_t abspage = vpage + ppb * syshyperblocks;
861870 if (abspage >= ftl_nand_type->blocks * ppb || abspage < ppb)
862871 {
@@ -863,9 +872,9 @@
864873 return 4;
865874 }
866875
867 - uint32_t bank = abspage % ftl_banks;
868 - uint32_t block = abspage / (ftl_nand_type->pagesperblock * ftl_banks);
869 - uint32_t page = (abspage / ftl_banks) % ftl_nand_type->pagesperblock;
 876+ uint32_t bank = abspage & (ftl_banks - 1);
 877+ uint32_t block = abspage >> ppb_exponent;
 878+ uint32_t page = (abspage >> ftl_banks_exponent) & ((1 << ftl_nand_type->blocksizeexponent) - 1);
870879 uint32_t physblock = ftl_vfl_get_physical_block(bank, block);
871880 uint32_t physpage = physblock * ftl_nand_type->pagesperblock + page;
872881
@@ -902,9 +911,6 @@
903912 #endif
904913
905914 uint32_t i, rc = 0;
906 - uint32_t ppb = ftl_nand_type->pagesperblock * ftl_banks;
907 - uint32_t syshyperblocks = ftl_nand_type->blocks
908 - - ftl_nand_type->userblocks - 0x17;
909915 uint32_t abspage = vpage + ppb * syshyperblocks;
910916 if (abspage + ftl_banks - 1 >= ftl_nand_type->blocks * ppb || abspage < ppb)
911917 {
@@ -912,9 +918,9 @@
913919 return 4;
914920 }
915921
916 - uint32_t bank = abspage % ftl_banks;
917 - uint32_t block = abspage / (ftl_nand_type->pagesperblock * ftl_banks);
918 - uint32_t page = (abspage / ftl_banks) % ftl_nand_type->pagesperblock;
 922+ uint32_t bank = abspage & (ftl_banks - 1);
 923+ uint32_t block = abspage >> ppb_exponent;
 924+ uint32_t page = (abspage >> ftl_banks_exponent) & ((1 << ftl_nand_type->blocksizeexponent) - 1);
919925 uint32_t remapped = 0;
920926 for (i = 0; i < ftl_banks; i++)
921927 if (ftl_vfl_get_physical_block(i, block) != block)
@@ -978,9 +984,6 @@
979985 DEBUGF("FTL: VFL: Writing page %d", vpage);
980986 #endif
981987
982 - uint32_t ppb = ftl_nand_type->pagesperblock * ftl_banks;
983 - uint32_t syshyperblocks = ftl_nand_type->blocks
984 - - ftl_nand_type->userblocks - 0x17;
985988 uint32_t abspage = vpage + ppb * syshyperblocks;
986989 if (abspage + count > ftl_nand_type->blocks * ppb || abspage < ppb)
987990 {
@@ -1065,9 +1068,6 @@
10661069 uint8_t bbt[0x410];
10671070 #endif
10681071
1069 - uint32_t syshyperblocks = ftl_nand_type->blocks
1070 - - ftl_nand_type->userblocks - 0x18;
1071 -
10721072 for (i = 0; i < ftl_banks; i++)
10731073 #ifndef FTL_READONLY
10741074 if (ftl_load_bbt(i, ftl_bbt[i]) == 0)
@@ -1152,7 +1152,6 @@
11531153 {
11541154 uint32_t i;
11551155 uint32_t ret;
1156 - uint32_t ppb = ftl_nand_type->pagesperblock * ftl_banks;
11571156 struct ftl_vfl_cxt_type* cxt = ftl_vfl_get_newest_cxt();
11581157
11591158 uint32_t ftlcxtblock = 0xffffffff;
@@ -1316,7 +1315,6 @@
13171316 uint32_t ftl_read(uint32_t sector, uint32_t count, void* buffer)
13181317 {
13191318 uint32_t i, j;
1320 - uint32_t ppb = ftl_nand_type->pagesperblock * ftl_banks;
13211319 uint32_t error = 0;
13221320
13231321 if (!ftl_initialized) return -1;
@@ -1571,7 +1569,6 @@
15721570 static uint32_t ftl_next_ctrl_pool_page(void)
15731571 {
15741572 uint32_t i;
1575 - uint32_t ppb = ftl_nand_type->pagesperblock * ftl_banks;
15761573 if (++ftl_cxt.ftlctrlpage % ppb != 0) return 0;
15771574 for (i = 0; i < 3; i++)
15781575 if ((ftl_cxt.ftlctrlblocks[i] + 1) * ppb == ftl_cxt.ftlctrlpage)
@@ -1610,7 +1607,6 @@
16111608 static uint32_t ftl_copy_page(uint32_t source, uint32_t destination,
16121609 uint32_t lpn, uint32_t type)
16131610 {
1614 - uint32_t ppb = ftl_nand_type->pagesperblock * ftl_banks;
16151611 uint32_t rc = ftl_vfl_read(source, ftl_copybuffer[0],
16161612 &ftl_copyspare[0], 1, 1) & 0x11F;
16171613 memset(&ftl_copyspare[0], 0xFF, 0x40);
@@ -1631,7 +1627,6 @@
16321628 static uint32_t ftl_copy_block(uint32_t source, uint32_t destination)
16331629 {
16341630 uint32_t i, j;
1635 - uint32_t ppb = ftl_nand_type->pagesperblock * ftl_banks;
16361631 uint32_t error = 0;
16371632 ftl_cxt.nextblockusn++;
16381633 for (i = 0; i < ppb; i += FTL_COPYBUF_SIZE)
@@ -1690,7 +1685,6 @@
16911686 static uint32_t ftl_compact_scattered(struct ftl_log_type* entry)
16921687 {
16931688 uint32_t i, j;
1694 - uint32_t ppb = ftl_nand_type->pagesperblock * ftl_banks;
16951689 uint32_t error;
16961690 struct ftl_log_type backup;
16971691 if (entry->pagescurrent == 0)
@@ -1774,7 +1768,6 @@
17751769 static uint32_t ftl_commit_sequential(struct ftl_log_type* entry)
17761770 {
17771771 uint32_t i;
1778 - uint32_t ppb = ftl_nand_type->pagesperblock * ftl_banks;
17791772
17801773 if (entry->issequential != 1
17811774 || entry->pagescurrent != entry->pagesused)
@@ -1820,7 +1813,6 @@
18211814 static uint32_t ftl_remove_scattered_block(struct ftl_log_type* entry)
18221815 {
18231816 uint32_t i;
1824 - uint32_t ppb = ftl_nand_type->pagesperblock * ftl_banks;
18251817 uint32_t age = 0xFFFFFFFF, used = 0;
18261818 if (entry == NULL)
18271819 {
@@ -1917,7 +1909,6 @@
19181910 static uint32_t ftl_commit_cxt(void)
19191911 {
19201912 uint32_t i;
1921 - uint32_t ppb = ftl_nand_type->pagesperblock * ftl_banks;
19221913 uint32_t mappages = (ftl_nand_type->userblocks + 0x3ff) >> 10;
19231914 uint32_t ctrpages = (ftl_nand_type->userblocks + 23 + 0x3ff) >> 10;
19241915 uint32_t endpage = ftl_cxt.ftlctrlpage + mappages + ctrpages + 1;
@@ -2002,7 +1993,6 @@
20031994 uint32_t ftl_write(uint32_t sector, uint32_t count, const void* buffer)
20041995 {
20051996 uint32_t i, j, k;
2006 - uint32_t ppb = ftl_nand_type->pagesperblock * ftl_banks;
20071997
20081998 if (!ftl_initialized) return -1;
20091999
@@ -2197,8 +2187,6 @@
21982188 {
21992189 uint32_t i;
22002190 uint32_t rc = 0;
2201 - uint32_t ppb = ftl_nand_type->pagesperblock * ftl_banks;
2202 -
22032191 if (!ftl_initialized) return 0;
22042192
22052193 if (ftl_cxt.clean_flag == 1) return 0;
@@ -2591,6 +2579,10 @@
25922580 for (i = 0; i < 4; i++)
25932581 if (nand_get_device_type(i) != 0) ftl_banks = i + 1;
25942582 ftl_nand_type = nand_get_device_type(0);
 2583+ ftl_banks_exponent = ftl_banks == 4 ? 2 : ftl_banks == 2 ? 1 : 0;
 2584+ ppb_exponent = ftl_nand_type->blocksizeexponent + ftl_banks_exponent;
 2585+ ppb = (1 << ppb_exponent);
 2586+ syshyperblocks = ftl_nand_type->blocks - ftl_nand_type->userblocks - 0x17;
25952587
25962588 if (!ftl_has_devinfo())
25972589 {