drv_virtio.c 2.2 KB

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