drv_virtio.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 <virt.h>
  12. #ifdef RT_USING_VIRTIO
  13. #include <virtio.h>
  14. #ifdef BSP_USING_VIRTIO_BLK
  15. #include <virtio_blk.h>
  16. #endif
  17. #ifdef BSP_USING_VIRTIO_NET
  18. #include <virtio_net.h>
  19. #endif
  20. #ifdef BSP_USING_VIRTIO_CONSOLE
  21. #include <virtio_console.h>
  22. #endif
  23. #ifdef BSP_USING_VIRTIO_GPU
  24. #include <virtio_gpu.h>
  25. #endif
  26. #ifdef BSP_USING_VIRTIO_INPUT
  27. #include <virtio_input.h>
  28. #endif
  29. #include <board.h>
  30. static virtio_device_init_handler virtio_device_init_handlers[] =
  31. {
  32. #ifdef BSP_USING_VIRTIO_BLK
  33. [VIRTIO_DEVICE_ID_BLOCK] = rt_virtio_blk_init,
  34. #endif
  35. #ifdef BSP_USING_VIRTIO_NET
  36. [VIRTIO_DEVICE_ID_NET] = rt_virtio_net_init,
  37. #endif
  38. #ifdef BSP_USING_VIRTIO_CONSOLE
  39. [VIRTIO_DEVICE_ID_CONSOLE] = rt_virtio_console_init,
  40. #endif
  41. #ifdef BSP_USING_VIRTIO_GPU
  42. [VIRTIO_DEVICE_ID_GPU] = rt_virtio_gpu_init,
  43. #endif
  44. #ifdef BSP_USING_VIRTIO_INPUT
  45. [VIRTIO_DEVICE_ID_INPUT] = rt_virtio_input_init,
  46. #endif
  47. [VIRTIO_DEVICE_TYPE_SIZE] = RT_NULL
  48. };
  49. int rt_virtio_devices_init(void)
  50. {
  51. int i;
  52. rt_uint32_t irq = VIRTIO_IRQ_BASE;
  53. rt_ubase_t mmio_base = VIRTIO_MMIO_BASE;
  54. struct virtio_mmio_config *mmio_config;
  55. virtio_device_init_handler init_handler;
  56. if (sizeof(virtio_device_init_handlers) == 0)
  57. {
  58. /* The compiler will optimize the codes after here. */
  59. return 0;
  60. }
  61. #ifdef RT_USING_SMART
  62. mmio_base = (rt_ubase_t)rt_ioremap((void *)mmio_base, VIRTIO_MMIO_SIZE * VIRTIO_MAX_NR);
  63. if (mmio_base == RT_NULL)
  64. {
  65. return -RT_ERROR;
  66. }
  67. #endif
  68. for (i = 0; i < VIRTIO_MAX_NR; ++i, ++irq, mmio_base += VIRTIO_MMIO_SIZE)
  69. {
  70. mmio_config = (struct virtio_mmio_config *)mmio_base;
  71. if (mmio_config->magic != VIRTIO_MAGIC_VALUE ||
  72. mmio_config->version != RT_USING_VIRTIO_VERSION ||
  73. mmio_config->vendor_id != VIRTIO_VENDOR_ID)
  74. {
  75. continue;
  76. }
  77. init_handler = virtio_device_init_handlers[mmio_config->device_id];
  78. if (init_handler != RT_NULL)
  79. {
  80. init_handler((rt_ubase_t *)mmio_base, irq);
  81. }
  82. }
  83. return 0;
  84. }
  85. INIT_DEVICE_EXPORT(rt_virtio_devices_init);
  86. #endif /* RT_USING_VIRTIO */