1 /* 2 * Copyright 2021, Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _VIRTIODEVICE_H_ 6 #define _VIRTIODEVICE_H_ 7 8 9 #include <virtio.h> 10 #include <virtio_defs.h> 11 #include <AutoDeleter.h> 12 #include <AutoDeleterOS.h> 13 #include <Referenceable.h> 14 #include <util/AutoLock.h> 15 16 17 //#define TRACE_VIRTIO 18 #ifdef TRACE_VIRTIO 19 # define TRACE(x...) dprintf("virtio_mmio: " x) 20 #else 21 # define TRACE(x...) ; 22 #endif 23 24 #define TRACE_ALWAYS(x...) dprintf("virtio_mmio: " x) 25 #define ERROR(x...) dprintf("virtio_mmio: " x) 26 27 28 struct VirtioIrqHandler; 29 struct VirtioDevice; 30 31 struct VirtioQueue { 32 VirtioDevice *fDev; 33 int32 fId; 34 size_t fQueueLen; 35 AreaDeleter fArea; 36 volatile VirtioDesc *fDescs; 37 volatile VirtioAvail *fAvail; 38 volatile VirtioUsed *fUsed; 39 ArrayDeleter<uint32> fFreeDescs; 40 uint16 fLastUsed; 41 ArrayDeleter<void*> fCookies; 42 43 BReference<VirtioIrqHandler> fQueueHandlerRef; 44 virtio_callback_func fQueueHandler; 45 void *fQueueHandlerCookie; 46 47 VirtioQueue(VirtioDevice *dev, int32 id); 48 ~VirtioQueue(); 49 status_t Init(); 50 51 int32 AllocDesc(); 52 void FreeDesc(int32 idx); 53 54 status_t Enqueue( 55 const physical_entry* vector, 56 size_t readVectorCount, size_t writtenVectorCount, 57 void* cookie 58 ); 59 60 bool Dequeue(void** _cookie, uint32* _usedLength); 61 }; 62 63 64 struct VirtioIrqHandler: public BReferenceable 65 { 66 VirtioDevice* fDev; 67 68 VirtioIrqHandler(VirtioDevice* dev); 69 70 virtual void FirstReferenceAcquired(); 71 virtual void LastReferenceReleased(); 72 73 static int32 Handle(void* data); 74 }; 75 76 77 struct VirtioDevice 78 { 79 AreaDeleter fRegsArea; 80 volatile VirtioRegs *fRegs; 81 int32 fIrq; 82 int32 fQueueCnt; 83 ArrayDeleter<ObjectDeleter<VirtioQueue> > fQueues; 84 85 VirtioIrqHandler fIrqHandler; 86 87 BReference<VirtioIrqHandler> fConfigHandlerRef; 88 virtio_intr_func fConfigHandler; 89 void* fConfigHandlerCookie; 90 91 VirtioDevice(); 92 status_t Init(phys_addr_t regs, size_t regsLen, int32 irq, int32 queueCnt); 93 }; 94 95 96 #endif // _VIRTIODEVICE_H_ 97