drv_virtio.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (c) 2006-2021, 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. */
  10. #include <rtthread.h>
  11. #include <virtio.h>
  12. #ifdef BSP_USING_VIRTIO_BLK
  13. #include <virtio_blk.h>
  14. #endif
  15. #ifdef BSP_USING_VIRTIO_NET
  16. #include <virtio_net.h>
  17. #endif
  18. #ifdef BSP_USING_VIRTIO_GPU
  19. #include <virtio_gpu.h>
  20. #endif
  21. #ifdef BSP_USING_VIRTIO_INPUT
  22. #include <virtio_input.h>
  23. #endif
  24. #include <board.h>
  25. static virtio_device_init_handler virtio_device_init_handlers[] =
  26. {
  27. #ifdef BSP_USING_VIRTIO_BLK
  28. [VIRTIO_DEVICE_ID_BLOCK] = rt_virtio_blk_init,
  29. #endif
  30. #ifdef BSP_USING_VIRTIO_NET
  31. [VIRTIO_DEVICE_ID_NET] = rt_virtio_net_init,
  32. #endif
  33. #ifdef BSP_USING_VIRTIO_GPU
  34. [VIRTIO_DEVICE_ID_GPU] = rt_virtio_gpu_init,
  35. #endif
  36. #ifdef BSP_USING_VIRTIO_INPUT
  37. [VIRTIO_DEVICE_ID_INPUT] = rt_virtio_input_init,
  38. #endif
  39. };
  40. int rt_virtio_devices_init(void)
  41. {
  42. int i;
  43. rt_uint32_t irq = VIRTIO_IRQ_BASE;
  44. rt_ubase_t mmio_base = VIRTIO_MMIO_BASE;
  45. struct virtio_mmio_config *mmio_config;
  46. virtio_device_init_handler init_handler;
  47. if (sizeof(virtio_device_init_handlers) == 0)
  48. {
  49. /* The compiler will optimize the codes after here. */
  50. return 0;
  51. }
  52. for (i = 0; i < VIRTIO_MAX_NR; ++i, ++irq, mmio_base += VIRTIO_MMIO_SIZE)
  53. {
  54. mmio_config = (struct virtio_mmio_config *)mmio_base;
  55. if (mmio_config->magic != VIRTIO_MAGIC_VALUE ||
  56. mmio_config->version != RT_USING_VIRTIO_VERSION ||
  57. mmio_config->vendor_id != VIRTIO_VENDOR_ID)
  58. {
  59. continue;
  60. }
  61. init_handler = virtio_device_init_handlers[mmio_config->device_id];
  62. if (init_handler != RT_NULL)
  63. {
  64. init_handler((rt_ubase_t *)mmio_base, irq);
  65. }
  66. }
  67. return 0;
  68. }
  69. INIT_DEVICE_EXPORT(rt_virtio_devices_init);