drv_virtio.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 BSP_USING_VIRTIO
  13. #include <virtio.h>
  14. #ifdef BSP_USING_VIRTIO_BLK
  15. #include <virtio_blk.h>
  16. #endif
  17. #include <board.h>
  18. static virtio_device_init_handler virtio_device_init_handlers[] =
  19. {
  20. #ifdef BSP_USING_VIRTIO_BLK
  21. [VIRTIO_DEVICE_ID_BLOCK] = rt_virtio_blk_init,
  22. #endif
  23. [VIRTIO_DEVICE_TYPE_SIZE] = RT_NULL
  24. };
  25. int rt_virtio_devices_init(void)
  26. {
  27. int i;
  28. rt_uint32_t irq = VIRTIO_IRQ_BASE;
  29. rt_ubase_t mmio_base = VIRTIO_MMIO_BASE;
  30. struct virtio_mmio_config *mmio_config;
  31. virtio_device_init_handler init_handler;
  32. if (sizeof(virtio_device_init_handlers) == 0)
  33. {
  34. /* The compiler will optimize the codes after here. */
  35. return 0;
  36. }
  37. #ifdef RT_USING_SMART
  38. mmio_base = (rt_ubase_t)rt_ioremap((void *)mmio_base, VIRTIO_MMIO_SIZE * VIRTIO_MAX_NR);
  39. if (mmio_base == RT_NULL)
  40. {
  41. return -RT_ERROR;
  42. }
  43. #endif
  44. for (i = 0; i < VIRTIO_MAX_NR; ++i, ++irq, mmio_base += VIRTIO_MMIO_SIZE)
  45. {
  46. mmio_config = (struct virtio_mmio_config *)mmio_base;
  47. if (mmio_config->magic != VIRTIO_MAGIC_VALUE ||
  48. mmio_config->version != RT_USING_VIRTIO_VERSION ||
  49. mmio_config->vendor_id != VIRTIO_VENDOR_ID)
  50. {
  51. continue;
  52. }
  53. init_handler = virtio_device_init_handlers[mmio_config->device_id];
  54. if (init_handler != RT_NULL)
  55. {
  56. init_handler((rt_ubase_t *)mmio_base, irq);
  57. }
  58. }
  59. return 0;
  60. }
  61. INIT_DEVICE_EXPORT(rt_virtio_devices_init);
  62. #endif /* BSP_USING_VIRTIO */