virtio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-11-11 GuEe-GUI the first version
  9. * 2023-10-12 fangjianzhou support SDL2
  10. */
  11. #include <rtthread.h>
  12. #include <cpuport.h>
  13. #include <virtio.h>
  14. rt_inline void _virtio_dev_check(struct virtio_device *dev)
  15. {
  16. RT_ASSERT(dev != RT_NULL);
  17. RT_ASSERT(dev->mmio_config != RT_NULL);
  18. }
  19. void virtio_reset_device(struct virtio_device *dev)
  20. {
  21. _virtio_dev_check(dev);
  22. dev->mmio_config->status = 0;
  23. }
  24. void virtio_status_acknowledge_driver(struct virtio_device *dev)
  25. {
  26. _virtio_dev_check(dev);
  27. dev->mmio_config->status |= VIRTIO_STATUS_ACKNOWLEDGE | VIRTIO_STATUS_DRIVER;
  28. }
  29. void virtio_status_driver_ok(struct virtio_device *dev)
  30. {
  31. _virtio_dev_check(dev);
  32. dev->mmio_config->status |= VIRTIO_STATUS_FEATURES_OK | VIRTIO_STATUS_DRIVER_OK;
  33. }
  34. void virtio_interrupt_ack(struct virtio_device *dev)
  35. {
  36. rt_uint32_t status;
  37. _virtio_dev_check(dev);
  38. status = dev->mmio_config->interrupt_status;
  39. if (status != 0)
  40. {
  41. dev->mmio_config->interrupt_ack = status;
  42. }
  43. }
  44. rt_bool_t virtio_has_feature(struct virtio_device *dev, rt_uint32_t feature_bit)
  45. {
  46. _virtio_dev_check(dev);
  47. return !!(dev->mmio_config->device_features & (1UL << feature_bit));
  48. }
  49. rt_err_t virtio_queues_alloc(struct virtio_device *dev, rt_size_t queues_num)
  50. {
  51. _virtio_dev_check(dev);
  52. dev->queues = rt_malloc(sizeof(struct virtq) * queues_num);
  53. if (dev->queues != RT_NULL)
  54. {
  55. dev->queues_num = queues_num;
  56. return RT_EOK;
  57. }
  58. return -RT_ENOMEM;
  59. }
  60. void virtio_queues_free(struct virtio_device *dev)
  61. {
  62. if (dev->queues != RT_NULL)
  63. {
  64. dev->queues_num = 0;
  65. rt_free(dev->queues);
  66. }
  67. }
  68. rt_err_t virtio_queue_init(struct virtio_device *dev, rt_uint32_t queue_index, rt_size_t ring_size)
  69. {
  70. int i;
  71. void *pages;
  72. rt_size_t pages_total_size;
  73. struct virtq *queue;
  74. _virtio_dev_check(dev);
  75. RT_ASSERT(dev->mmio_config->queue_num_max > 0);
  76. RT_ASSERT(dev->mmio_config->queue_num_max > queue_index);
  77. /* ring_size is power of 2 */
  78. RT_ASSERT(ring_size > 0);
  79. RT_ASSERT(((ring_size - 1) & ring_size) == 0);
  80. queue = &dev->queues[queue_index];
  81. pages_total_size = VIRTIO_PAGE_ALIGN(
  82. VIRTQ_DESC_TOTAL_SIZE(ring_size) + VIRTQ_AVAIL_TOTAL_SIZE(ring_size)) + VIRTQ_USED_TOTAL_SIZE(ring_size);
  83. pages = rt_malloc_align(pages_total_size, VIRTIO_PAGE_SIZE);
  84. if (pages == RT_NULL)
  85. {
  86. return -RT_ENOMEM;
  87. }
  88. queue->free = rt_malloc(sizeof(rt_bool_t) * ring_size);
  89. if (queue->free == RT_NULL)
  90. {
  91. rt_free_align(pages);
  92. return -RT_ENOMEM;
  93. }
  94. rt_memset(pages, 0, pages_total_size);
  95. dev->mmio_config->guest_page_size = VIRTIO_PAGE_SIZE;
  96. dev->mmio_config->queue_sel = queue_index;
  97. dev->mmio_config->queue_num = ring_size;
  98. dev->mmio_config->queue_align = VIRTIO_PAGE_SIZE;
  99. dev->mmio_config->queue_pfn = VIRTIO_VA2PA(pages) >> VIRTIO_PAGE_SHIFT;
  100. queue->num = ring_size;
  101. queue->desc = (struct virtq_desc *)((rt_ubase_t)pages);
  102. queue->avail = (struct virtq_avail *)(((rt_ubase_t)pages) + VIRTQ_DESC_TOTAL_SIZE(ring_size));
  103. queue->used = (struct virtq_used *)VIRTIO_PAGE_ALIGN(
  104. (rt_ubase_t)&queue->avail->ring[ring_size] + VIRTQ_AVAIL_RES_SIZE);
  105. queue->used_idx = 0;
  106. /* All descriptors start out unused */
  107. for (i = 0; i < ring_size; ++i)
  108. {
  109. queue->free[i] = RT_TRUE;
  110. }
  111. queue->free_count = ring_size;
  112. return RT_EOK;
  113. }
  114. void virtio_queue_destroy(struct virtio_device *dev, rt_uint32_t queue_index)
  115. {
  116. struct virtq *queue;
  117. _virtio_dev_check(dev);
  118. RT_ASSERT(dev->mmio_config->queue_num_max > 0);
  119. RT_ASSERT(dev->mmio_config->queue_num_max > queue_index);
  120. queue = &dev->queues[queue_index];
  121. RT_ASSERT(queue->num > 0);
  122. rt_free(queue->free);
  123. rt_free_align((void *)queue->desc);
  124. dev->mmio_config->queue_sel = queue_index;
  125. dev->mmio_config->queue_pfn = RT_NULL;
  126. queue->num = 0;
  127. queue->desc = RT_NULL;
  128. queue->avail = RT_NULL;
  129. queue->used = RT_NULL;
  130. }
  131. void virtio_queue_notify(struct virtio_device *dev, rt_uint32_t queue_index)
  132. {
  133. _virtio_dev_check(dev);
  134. dev->mmio_config->queue_notify = queue_index;
  135. }
  136. void virtio_submit_chain(struct virtio_device *dev, rt_uint32_t queue_index, rt_uint16_t desc_index)
  137. {
  138. rt_size_t ring_size;
  139. struct virtq *queue;
  140. _virtio_dev_check(dev);
  141. queue = &dev->queues[queue_index];
  142. ring_size = queue->num;
  143. /* Tell the device the first index in our chain of descriptors */
  144. queue->avail->ring[queue->avail->idx % ring_size] = desc_index;
  145. rt_hw_dsb();
  146. /* Tell the device another avail ring entry is available */
  147. queue->avail->idx++;
  148. rt_hw_dsb();
  149. }
  150. rt_uint16_t virtio_alloc_desc(struct virtio_device *dev, rt_uint32_t queue_index)
  151. {
  152. int i;
  153. struct virtq *queue;
  154. _virtio_dev_check(dev);
  155. RT_ASSERT(queue_index < dev->queues_num);
  156. queue = &dev->queues[queue_index];
  157. if (queue->free_count > 0)
  158. {
  159. rt_size_t ring_size = queue->num;
  160. for (i = 0; i < ring_size; ++i)
  161. {
  162. if (queue->free[i])
  163. {
  164. queue->free[i] = RT_FALSE;
  165. queue->free_count--;
  166. return (rt_uint16_t)i;
  167. }
  168. }
  169. }
  170. return VIRTQ_INVALID_DESC_ID;
  171. }
  172. void virtio_free_desc(struct virtio_device *dev, rt_uint32_t queue_index, rt_uint16_t desc_index)
  173. {
  174. struct virtq *queue;
  175. _virtio_dev_check(dev);
  176. queue = &dev->queues[queue_index];
  177. RT_ASSERT(queue_index < dev->queues_num);
  178. RT_ASSERT(!queue->free[desc_index]);
  179. queue->desc[desc_index].addr = 0;
  180. queue->desc[desc_index].len = 0;
  181. queue->desc[desc_index].flags = 0;
  182. queue->desc[desc_index].next = 0;
  183. queue->free[desc_index] = RT_TRUE;
  184. queue->free_count++;
  185. }
  186. rt_err_t virtio_alloc_desc_chain(struct virtio_device *dev, rt_uint32_t queue_index, rt_size_t count,
  187. rt_uint16_t *indexs)
  188. {
  189. int i, j;
  190. _virtio_dev_check(dev);
  191. RT_ASSERT(indexs != RT_NULL);
  192. if (dev->queues[queue_index].free_count < count)
  193. {
  194. return -RT_ERROR;
  195. }
  196. for (i = 0; i < count; ++i)
  197. {
  198. indexs[i] = virtio_alloc_desc(dev, queue_index);
  199. if (indexs[i] == VIRTQ_INVALID_DESC_ID)
  200. {
  201. for (j = 0; j < i; ++j)
  202. {
  203. virtio_free_desc(dev, queue_index, indexs[j]);
  204. }
  205. return -RT_ERROR;
  206. }
  207. }
  208. return RT_EOK;
  209. }
  210. void virtio_free_desc_chain(struct virtio_device *dev, rt_uint32_t queue_index, rt_uint16_t desc_index)
  211. {
  212. rt_uint16_t flags, next;
  213. struct virtq_desc *desc;
  214. _virtio_dev_check(dev);
  215. desc = &dev->queues[queue_index].desc[0];
  216. for (;;)
  217. {
  218. flags = desc[desc_index].flags;
  219. next = desc[desc_index].next;
  220. virtio_free_desc(dev, queue_index, desc_index);
  221. if (flags & VIRTQ_DESC_F_NEXT)
  222. {
  223. desc_index = next;
  224. }
  225. else
  226. {
  227. break;
  228. }
  229. }
  230. }
  231. void virtio_fill_desc(struct virtio_device *dev, rt_uint32_t queue_index, rt_uint16_t desc_index,
  232. rt_uint64_t addr, rt_uint32_t len, rt_uint16_t flags, rt_uint16_t next)
  233. {
  234. struct virtq_desc *desc;
  235. _virtio_dev_check(dev);
  236. desc = &dev->queues[queue_index].desc[desc_index];
  237. desc->addr = addr;
  238. desc->len = len;
  239. desc->flags = flags;
  240. desc->next = next;
  241. }
  242. #ifdef RT_USING_SMART
  243. #ifdef RT_USING_VIRTIO_GPU
  244. #include <virtio_gpu.h>
  245. #include "drivers/lcd.h"
  246. #include <dfs_file.h>
  247. #include <lwp_user_mm.h>
  248. static struct rt_device_graphic_info _graphic_info;
  249. static struct rt_device_rect_info _rect_info;
  250. static struct rt_device _fb = {};
  251. static rt_device_t _gpu_dev = RT_NULL;
  252. static rt_err_t fb_open(rt_device_t dev, rt_uint16_t oflag)
  253. {
  254. return RT_EOK;
  255. }
  256. static rt_err_t fb_close(rt_device_t dev)
  257. {
  258. return RT_EOK;
  259. }
  260. static rt_err_t fb_control(rt_device_t dev, int cmd, void *args)
  261. {
  262. switch(cmd)
  263. {
  264. case FBIOPAN_DISPLAY:
  265. {
  266. rt_hw_cpu_dcache_clean(_graphic_info.framebuffer, _graphic_info.smem_len);
  267. rt_device_control(_gpu_dev, RTGRAPHIC_CTRL_RECT_UPDATE, &_rect_info);
  268. break;
  269. }
  270. case FBIOGET_FSCREENINFO:
  271. {
  272. struct fb_fix_screeninfo *info = (struct fb_fix_screeninfo *)args;
  273. strncpy(info->id, "lcd", sizeof(info->id));
  274. info->smem_len = _graphic_info.smem_len;
  275. break;
  276. }
  277. case FBIOGET_VSCREENINFO:
  278. {
  279. struct fb_var_screeninfo *info = (struct fb_var_screeninfo *)args;
  280. info->bits_per_pixel = _graphic_info.bits_per_pixel;
  281. info->xres = _graphic_info.width;
  282. info->yres = _graphic_info.height;
  283. info->yres_virtual = _graphic_info.height;
  284. info->xres_virtual = _graphic_info.width;
  285. info->transp.offset = 24;
  286. info->transp.length = 8;
  287. info->red.offset = 0;
  288. info->red.length = 8;
  289. info->green.offset = 8;
  290. info->green.length = 8;
  291. info->blue.offset = 16;
  292. info->blue.length = 8;
  293. break;
  294. }
  295. case RT_FIOMMAP2:
  296. {
  297. struct dfs_mmap2_args *mmap2 = (struct dfs_mmap2_args *)args;
  298. if(mmap2)
  299. {
  300. mmap2->ret = lwp_map_user_phy(lwp_self(), RT_NULL, rt_kmem_v2p(_graphic_info.framebuffer), mmap2->length, 1);
  301. }
  302. else
  303. {
  304. return -EIO;
  305. }
  306. break;
  307. }
  308. default:
  309. break;
  310. }
  311. return RT_EOK;
  312. }
  313. #ifdef RT_USING_DEVICE_OPS
  314. const static struct rt_device_ops fb_ops =
  315. {
  316. RT_NULL,
  317. fb_open,
  318. fb_close,
  319. RT_NULL,
  320. RT_NULL,
  321. fb_control
  322. };
  323. #endif
  324. static int fb_init()
  325. {
  326. _gpu_dev = rt_device_find("virtio-gpu0");
  327. if(_gpu_dev == RT_NULL)
  328. {
  329. return -RT_ERROR;
  330. }
  331. if(_gpu_dev != RT_NULL && rt_device_open(_gpu_dev, 0) == RT_EOK)
  332. {
  333. rt_memset(&_graphic_info, 0, sizeof(_graphic_info));
  334. rt_memset(&_rect_info, 0, sizeof(_rect_info));
  335. rt_device_control(_gpu_dev, VIRTIO_DEVICE_CTRL_GPU_SET_PRIMARY, RT_NULL);
  336. rt_device_control(_gpu_dev, VIRTIO_DEVICE_CTRL_GPU_CREATE_2D, (void *)RTGRAPHIC_PIXEL_FORMAT_RGB888);
  337. rt_device_control(_gpu_dev, RTGRAPHIC_CTRL_GET_INFO, &_graphic_info);
  338. _rect_info.x = 0;
  339. _rect_info.y = 0;
  340. _rect_info.width = _graphic_info.width;
  341. _rect_info.height = _graphic_info.height;
  342. memset(_graphic_info.framebuffer, 0xff, _graphic_info.smem_len);
  343. rt_device_control(_gpu_dev, RTGRAPHIC_CTRL_RECT_UPDATE, &_rect_info);
  344. }
  345. if(rt_device_find("fb0") != RT_NULL)
  346. {
  347. rt_kprintf("a device named fb0 already exists\n");
  348. return -RT_ERROR;
  349. }
  350. _fb.type = RT_Device_Class_Miscellaneous;
  351. #ifdef RT_USING_DEVICE_OPS
  352. _fb.ops = &fb_ops;
  353. #else
  354. _fb.init = RT_NULL;
  355. _fb.open = fb_open;
  356. _fb.close = fb_close;
  357. _fb.read = RT_NULL;
  358. _fb.write = RT_NULL;
  359. _fb.control = fb_control;
  360. _fb.user_data = RT_NULL;
  361. #endif
  362. rt_device_register(&_fb, "fb0", RT_DEVICE_FLAG_RDWR);
  363. return RT_EOK;
  364. }
  365. INIT_COMPONENT_EXPORT(fb_init);
  366. #endif
  367. #endif