xref: /haiku/src/system/kernel/device_manager/IOCache.h (revision f14480bc2c86a3af8f802c7478bc7a54ae445339)
1e91e4ee0SIngo Weinhold /*
2e91e4ee0SIngo Weinhold  * Copyright 2010, Ingo Weinhold, ingo_weinhold@gmx.de.
3e91e4ee0SIngo Weinhold  * Distributed under the terms of the MIT License.
4e91e4ee0SIngo Weinhold  */
5e91e4ee0SIngo Weinhold #ifndef IO_CACHE_H
6e91e4ee0SIngo Weinhold #define IO_CACHE_H
7e91e4ee0SIngo Weinhold 
8e91e4ee0SIngo Weinhold 
9e91e4ee0SIngo Weinhold #include <lock.h>
10*f14480bcSIngo Weinhold #include <vm/vm_page.h>
11e91e4ee0SIngo Weinhold 
12e91e4ee0SIngo Weinhold #include "dma_resources.h"
13e91e4ee0SIngo Weinhold #include "IOCallback.h"
14e91e4ee0SIngo Weinhold #include "IORequest.h"
15e91e4ee0SIngo Weinhold 
16e91e4ee0SIngo Weinhold 
17*f14480bcSIngo Weinhold struct VMCache;
18*f14480bcSIngo Weinhold struct vm_page;
19*f14480bcSIngo Weinhold 
20*f14480bcSIngo Weinhold 
21e91e4ee0SIngo Weinhold class IOCache {
22e91e4ee0SIngo Weinhold public:
23e91e4ee0SIngo Weinhold 								IOCache(DMAResource* resource,
24e91e4ee0SIngo Weinhold 									size_t cacheLineSize);
25e91e4ee0SIngo Weinhold 								~IOCache();
26e91e4ee0SIngo Weinhold 
27e91e4ee0SIngo Weinhold 			status_t			Init(const char* name);
28e91e4ee0SIngo Weinhold 
29e91e4ee0SIngo Weinhold 			void				SetCallback(IOCallback& callback);
30e91e4ee0SIngo Weinhold 			void				SetCallback(io_callback callback, void* data);
31e91e4ee0SIngo Weinhold 
32e91e4ee0SIngo Weinhold 			void				SetDeviceCapacity(off_t deviceCapacity);
33e91e4ee0SIngo Weinhold 
34e91e4ee0SIngo Weinhold 			status_t			ScheduleRequest(IORequest* request);
35e91e4ee0SIngo Weinhold 
36e91e4ee0SIngo Weinhold 			void				OperationCompleted(IOOperation* operation,
37e91e4ee0SIngo Weinhold 									status_t status, size_t transferredBytes);
38e91e4ee0SIngo Weinhold 
39e91e4ee0SIngo Weinhold private:
40e91e4ee0SIngo Weinhold 			struct Operation;
41e91e4ee0SIngo Weinhold 
42e91e4ee0SIngo Weinhold private:
43e91e4ee0SIngo Weinhold 			status_t			_DoRequest(IORequest* request,
44e91e4ee0SIngo Weinhold 									size_t& _bytesTransferred);
45e91e4ee0SIngo Weinhold 			status_t			_TransferRequestLine(IORequest* request,
46*f14480bcSIngo Weinhold 									off_t lineOffset, size_t lineSize,
47*f14480bcSIngo Weinhold 									off_t requestOffset, size_t requestLength);
48*f14480bcSIngo Weinhold 			status_t			_TransferRequestLineUncached(IORequest* request,
49e91e4ee0SIngo Weinhold 									off_t lineOffset, off_t requestOffset,
50e91e4ee0SIngo Weinhold 									size_t requestLength);
51e91e4ee0SIngo Weinhold 			status_t			_DoOperation(Operation& operation);
52e91e4ee0SIngo Weinhold 
53*f14480bcSIngo Weinhold 			status_t			_TransferPages(size_t firstPage,
54*f14480bcSIngo Weinhold 									size_t pageCount, bool isWrite, bool isVIP);
55*f14480bcSIngo Weinhold 			void				_DiscardPages(size_t firstPage,
56*f14480bcSIngo Weinhold 									size_t pageCount);
57*f14480bcSIngo Weinhold 			void				_CachePages(size_t firstPage, size_t pageCount);
58e91e4ee0SIngo Weinhold 
59*f14480bcSIngo Weinhold 			status_t			_CopyPages(IORequest* request,
60*f14480bcSIngo Weinhold 									size_t pagesRelativeOffset,
61*f14480bcSIngo Weinhold 									off_t requestOffset, size_t requestLength,
62*f14480bcSIngo Weinhold 									bool toRequest);
63*f14480bcSIngo Weinhold 
64*f14480bcSIngo Weinhold 			status_t			_MapPages(size_t firstPage, size_t endPage);
65*f14480bcSIngo Weinhold 			void				_UnmapPages(size_t firstPage, size_t endPage);
66e91e4ee0SIngo Weinhold 
67e91e4ee0SIngo Weinhold private:
68e91e4ee0SIngo Weinhold 			mutex				fSerializationLock;
69e91e4ee0SIngo Weinhold 			off_t				fDeviceCapacity;
70e91e4ee0SIngo Weinhold 			size_t				fLineSize;
71e91e4ee0SIngo Weinhold 			uint32				fLineSizeShift;
72*f14480bcSIngo Weinhold 			size_t				fPagesPerLine;
73e91e4ee0SIngo Weinhold 			DMAResource*		fDMAResource;
74e91e4ee0SIngo Weinhold 			io_callback			fIOCallback;
75e91e4ee0SIngo Weinhold 			void*				fIOCallbackData;
76*f14480bcSIngo Weinhold 			area_id				fArea;
77*f14480bcSIngo Weinhold 			void*				fAreaBase;
78*f14480bcSIngo Weinhold 			vm_page_reservation	fMappingReservation;
79*f14480bcSIngo Weinhold 			VMCache*			fCache;
80*f14480bcSIngo Weinhold 			vm_page**			fPages;
81*f14480bcSIngo Weinhold 			iovec*				fVecs;
82e91e4ee0SIngo Weinhold };
83e91e4ee0SIngo Weinhold 
84e91e4ee0SIngo Weinhold 
85e91e4ee0SIngo Weinhold #endif	// IO_CACHE_H
86