From d0a6143febdf7668eaf049f13c89b52fc43d5501 Mon Sep 17 00:00:00 2001 From: Baptiste Wicht Date: Mon, 8 Aug 2016 20:36:45 +0200 Subject: [PATCH] Cache blocks from ATA --- kernel/src/ata.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/kernel/src/ata.cpp b/kernel/src/ata.cpp index 0a821c73..4acca096 100644 --- a/kernel/src/ata.cpp +++ b/kernel/src/ata.cpp @@ -17,6 +17,7 @@ #include "errors.hpp" #include "disks.hpp" #include "mutex.hpp" +#include "block_cache.hpp" namespace { @@ -29,6 +30,8 @@ mutex<> ata_lock; mutex<> primary_lock; mutex<> secondary_lock; +block_cache cache; + volatile bool primary_invoked = false; volatile bool secondary_invoked = false; @@ -331,6 +334,9 @@ void ata::detect_disks(){ primary_lock.set_name("ata_primary_lock"); secondary_lock.set_name("ata_secondary_lock"); + // Init the cache with 256 blocks + cache.init(BLOCK_SIZE, 256); + drives = new drive_descriptor[4]; drives[0] = {ATA_PRIMARY, 0xE0, false, MASTER_BIT, false, "", "", ""}; @@ -457,10 +463,18 @@ size_t ata::read_sectors(drive_descriptor& drive, uint64_t start, uint8_t count, auto buffer = reinterpret_cast(destination); for(size_t i = 0; i < count; ++i){ - if(!read_write_sector(drive, start + i, buffer, true)){ - return std::ERROR_FAILED; + bool valid; + auto block = cache.block((drive.controller << 8) + drive.drive, start + i, valid); + + if(!valid){ + if(!read_write_sector(drive, start + i, block, true)){ + return std::ERROR_FAILED; + } } + // Copy the block to the output buffer + std::copy_n(buffer, block, BLOCK_SIZE); + buffer += BLOCK_SIZE; read += BLOCK_SIZE; }