1 /* 2 * Copyright 2013, 2018, Jérôme Duval, jerome.duval@gmail.com. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "VirtioPrivate.h" 8 9 10 static inline uint32 11 round_to_pagesize(uint32 size) 12 { 13 return (size + B_PAGE_SIZE - 1) & ~(B_PAGE_SIZE - 1); 14 } 15 16 17 area_id 18 alloc_mem(void **virt, phys_addr_t *phy, size_t size, uint32 protection, 19 const char *name) 20 { 21 physical_entry pe; 22 void * virtadr; 23 area_id areaid; 24 status_t rv; 25 26 TRACE("allocating %ld bytes for %s\n", size, name); 27 28 size = round_to_pagesize(size); 29 areaid = create_area(name, &virtadr, B_ANY_KERNEL_ADDRESS, size, 30 B_CONTIGUOUS, protection); 31 if (areaid < B_OK) { 32 ERROR("couldn't allocate area %s\n", name); 33 return B_ERROR; 34 } 35 rv = get_memory_map(virtadr, size, &pe, 1); 36 if (rv < B_OK) { 37 delete_area(areaid); 38 ERROR("couldn't get mapping for %s\n", name); 39 return B_ERROR; 40 } 41 if (virt) 42 *virt = virtadr; 43 if (phy) 44 *phy = pe.address; 45 TRACE("area = %" B_PRId32 ", size = %ld, virt = %p, phy = %#" B_PRIxPHYSADDR "\n", 46 areaid, size, virtadr, pe.address); 47 return areaid; 48 } 49 50 51 class TransferDescriptor { 52 public: 53 TransferDescriptor(VirtioQueue* queue, 54 uint16 indirectMaxSize); 55 ~TransferDescriptor(); 56 57 status_t InitCheck() { return fStatus; } 58 59 uint16 Size() { return fDescriptorCount; } 60 void SetTo(uint16 size, void *cookie); 61 void* Cookie() { return fCookie; } 62 void Unset(); 63 struct vring_desc* Indirect() { return fIndirect; } 64 phys_addr_t PhysAddr() { return fPhysAddr; } 65 private: 66 status_t fStatus; 67 VirtioQueue* fQueue; 68 void* fCookie; 69 70 struct vring_desc* fIndirect; 71 size_t fAreaSize; 72 area_id fArea; 73 phys_addr_t fPhysAddr; 74 uint16 fDescriptorCount; 75 }; 76 77 78 TransferDescriptor::TransferDescriptor(VirtioQueue* queue, uint16 indirectMaxSize) 79 : fQueue(queue), 80 fCookie(NULL), 81 fIndirect(NULL), 82 fAreaSize(0), 83 fArea(-1), 84 fPhysAddr(0), 85 fDescriptorCount(0) 86 { 87 fStatus = B_OK; 88 struct vring_desc* virtAddr; 89 phys_addr_t physAddr; 90 91 if (indirectMaxSize > 0) { 92 fAreaSize = indirectMaxSize * sizeof(struct vring_desc); 93 fArea = alloc_mem((void **)&virtAddr, &physAddr, fAreaSize, 0, 94 "virtqueue"); 95 if (fArea < B_OK) { 96 fStatus = fArea; 97 return; 98 } 99 memset(virtAddr, 0, fAreaSize); 100 fIndirect = virtAddr; 101 fPhysAddr = physAddr; 102 103 for (uint16 i = 0; i < indirectMaxSize - 1; i++) 104 fIndirect[i].next = i + 1; 105 fIndirect[indirectMaxSize - 1].next = UINT16_MAX; 106 } 107 } 108 109 110 TransferDescriptor::~TransferDescriptor() 111 { 112 if (fArea > B_OK) 113 delete_area(fArea); 114 } 115 116 117 void 118 TransferDescriptor::SetTo(uint16 size, void *cookie) 119 { 120 fCookie = cookie; 121 fDescriptorCount = size; 122 } 123 124 125 void 126 TransferDescriptor::Unset() 127 { 128 fCookie = NULL; 129 fDescriptorCount = 0; 130 } 131 132 133 // #pragma mark - 134 135 136 VirtioQueue::VirtioQueue(VirtioDevice* device, uint16 queueNumber, 137 uint16 ringSize) 138 : 139 fDevice(device), 140 fQueueNumber(queueNumber), 141 fRingSize(ringSize), 142 fRingFree(ringSize), 143 fRingHeadIndex(0), 144 fRingUsedIndex(0), 145 fStatus(B_OK), 146 fIndirectMaxSize(0), 147 fCallback(NULL), 148 fCookie(NULL) 149 { 150 fDescriptors = new(std::nothrow) TransferDescriptor*[fRingSize]; 151 if (fDescriptors == NULL) { 152 fStatus = B_NO_MEMORY; 153 return; 154 } 155 156 uint8* virtAddr; 157 phys_addr_t physAddr; 158 fAreaSize = vring_size(fRingSize, device->Alignment()); 159 fArea = alloc_mem((void **)&virtAddr, &physAddr, fAreaSize, 0, 160 "virtqueue"); 161 if (fArea < B_OK) { 162 fStatus = fArea; 163 return; 164 } 165 memset(virtAddr, 0, fAreaSize); 166 vring_init(&fRing, fRingSize, virtAddr, device->Alignment()); 167 168 for (uint16 i = 0; i < fRingSize - 1; i++) 169 fRing.desc[i].next = i + 1; 170 fRing.desc[fRingSize - 1].next = UINT16_MAX; 171 172 if ((fDevice->Features() & VIRTIO_FEATURE_RING_INDIRECT_DESC) != 0) 173 fIndirectMaxSize = 128; 174 175 for (uint16 i = 0; i < fRingSize; i++) { 176 fDescriptors[i] = new TransferDescriptor(this, fIndirectMaxSize); 177 if (fDescriptors[i] == NULL || fDescriptors[i]->InitCheck() != B_OK) { 178 fStatus = B_NO_MEMORY; 179 return; 180 } 181 } 182 183 DisableInterrupt(); 184 185 device->SetupQueue(fQueueNumber, physAddr); 186 } 187 188 189 VirtioQueue::~VirtioQueue() 190 { 191 delete_area(fArea); 192 for (uint16 i = 0; i < fRingSize; i++) { 193 delete fDescriptors[i]; 194 } 195 delete[] fDescriptors; 196 } 197 198 199 status_t 200 VirtioQueue::SetupInterrupt(virtio_callback_func handler, void *cookie) 201 { 202 fCallback = handler; 203 fCookie = cookie; 204 205 return B_OK; 206 } 207 208 209 210 void 211 VirtioQueue::DisableInterrupt() 212 { 213 if ((fDevice->Features() & VIRTIO_FEATURE_RING_EVENT_IDX) == 0) 214 fRing.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT; 215 } 216 217 218 void 219 VirtioQueue::EnableInterrupt() 220 { 221 if ((fDevice->Features() & VIRTIO_FEATURE_RING_EVENT_IDX) == 0) 222 fRing.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT; 223 } 224 225 226 void 227 VirtioQueue::NotifyHost() 228 { 229 fDevice->NotifyQueue(fQueueNumber); 230 } 231 232 233 status_t 234 VirtioQueue::Interrupt() 235 { 236 CALLED(); 237 238 DisableInterrupt(); 239 240 if (fCallback != NULL) 241 fCallback(Device()->DriverCookie(), fCookie); 242 243 EnableInterrupt(); 244 return B_OK; 245 } 246 247 248 void* 249 VirtioQueue::Dequeue(uint32* _usedLength) 250 { 251 TRACE("Dequeue() fRingUsedIndex: %u\n", fRingUsedIndex); 252 253 if (fRingUsedIndex == fRing.used->idx) 254 return NULL; 255 256 uint16 usedIndex = fRingUsedIndex++ & (fRingSize - 1); 257 TRACE("Dequeue() usedIndex: %u\n", usedIndex); 258 struct vring_used_elem *element = &fRing.used->ring[usedIndex]; 259 uint16 descriptorIndex = element->id; 260 if (_usedLength != NULL) 261 *_usedLength = element->len; 262 263 void* cookie = fDescriptors[descriptorIndex]->Cookie(); 264 uint16 size = fDescriptors[descriptorIndex]->Size(); 265 if (size == 0) 266 panic("VirtioQueue::Dequeue() size is zero\n"); 267 fDescriptors[descriptorIndex]->Unset(); 268 fRingFree += size; 269 size--; 270 271 uint16 index = descriptorIndex; 272 if ((fRing.desc[index].flags & VRING_DESC_F_INDIRECT) == 0) { 273 while ((fRing.desc[index].flags & VRING_DESC_F_NEXT) != 0) { 274 index = fRing.desc[index].next; 275 size--; 276 } 277 } 278 279 if (size > 0) 280 panic("VirtioQueue::Dequeue() descriptors left %d\n", size); 281 282 fRing.desc[index].next = fRingHeadIndex; 283 fRingHeadIndex = descriptorIndex; 284 TRACE("Dequeue() fRingHeadIndex: %u\n", fRingHeadIndex); 285 286 return cookie; 287 } 288 289 290 status_t 291 VirtioQueue::QueueRequest(const physical_entry* vector, size_t readVectorCount, 292 size_t writtenVectorCount, void *cookie) 293 { 294 CALLED(); 295 size_t count = readVectorCount + writtenVectorCount; 296 if (count < 1) 297 return B_BAD_VALUE; 298 if ((fDevice->Features() & VIRTIO_FEATURE_RING_INDIRECT_DESC) != 0) { 299 return QueueRequestIndirect(vector, readVectorCount, 300 writtenVectorCount, cookie); 301 } 302 303 if (count > fRingFree) 304 return B_BUSY; 305 306 uint16 insertIndex = fRingHeadIndex; 307 fDescriptors[insertIndex]->SetTo(count, cookie); 308 309 // enqueue 310 uint16 index = QueueVector(insertIndex, fRing.desc, vector, 311 readVectorCount, writtenVectorCount); 312 313 fRingHeadIndex = index; 314 fRingFree -= count; 315 316 UpdateAvailable(insertIndex); 317 318 NotifyHost(); 319 320 return B_OK; 321 } 322 323 324 status_t 325 VirtioQueue::QueueRequestIndirect(const physical_entry* vector, 326 size_t readVectorCount, size_t writtenVectorCount, 327 void *cookie) 328 { 329 CALLED(); 330 size_t count = readVectorCount + writtenVectorCount; 331 if (count > fRingFree || count > fIndirectMaxSize) 332 return B_BUSY; 333 334 uint16 insertIndex = fRingHeadIndex; 335 fDescriptors[insertIndex]->SetTo(1, cookie); 336 337 // enqueue 338 uint16 index = QueueVector(0, fDescriptors[insertIndex]->Indirect(), 339 vector, readVectorCount, writtenVectorCount); 340 341 fRing.desc[insertIndex].addr = fDescriptors[insertIndex]->PhysAddr(); 342 fRing.desc[insertIndex].len = index * sizeof(struct vring_desc); 343 fRing.desc[insertIndex].flags = VRING_DESC_F_INDIRECT; 344 fRingHeadIndex = fRing.desc[insertIndex].next; 345 fRingFree--; 346 347 UpdateAvailable(insertIndex); 348 349 NotifyHost(); 350 351 return B_OK; 352 } 353 354 355 void 356 VirtioQueue::UpdateAvailable(uint16 index) 357 { 358 CALLED(); 359 uint16 available = fRing.avail->idx & (fRingSize - 1); 360 fRing.avail->ring[available] = index; 361 fRing.avail->idx++; 362 } 363 364 365 uint16 366 VirtioQueue::QueueVector(uint16 insertIndex, struct vring_desc *desc, 367 const physical_entry* vector, size_t readVectorCount, 368 size_t writtenVectorCount) 369 { 370 CALLED(); 371 uint16 index = insertIndex; 372 size_t total = readVectorCount + writtenVectorCount; 373 for (size_t i = 0; i < total; i++, index = desc[index].next) { 374 desc[index].addr = vector[i].address; 375 desc[index].len = vector[i].size; 376 desc[index].flags = 0; 377 if (i < total - 1) 378 desc[index].flags |= VRING_DESC_F_NEXT; 379 if (i >= readVectorCount) 380 desc[index].flags |= VRING_DESC_F_WRITE; 381 } 382 383 return index; 384 } 385