pci_svc.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2021, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <stdint.h>
  8. #include <common/debug.h>
  9. #include <common/runtime_svc.h>
  10. #include <services/pci_svc.h>
  11. #include <services/std_svc.h>
  12. #include <smccc_helpers.h>
  13. static uint64_t validate_rw_addr_sz(uint32_t addr, uint64_t off, uint64_t sz)
  14. {
  15. uint32_t nseg;
  16. uint32_t ret;
  17. uint32_t start_end_bus;
  18. ret = pci_get_bus_for_seg(PCI_ADDR_SEG(addr), &start_end_bus, &nseg);
  19. if (ret != SMC_PCI_CALL_SUCCESS) {
  20. return SMC_PCI_CALL_INVAL_PARAM;
  21. }
  22. switch (sz) {
  23. case SMC_PCI_SZ_8BIT:
  24. case SMC_PCI_SZ_16BIT:
  25. case SMC_PCI_SZ_32BIT:
  26. break;
  27. default:
  28. return SMC_PCI_CALL_INVAL_PARAM;
  29. }
  30. if ((off + sz) > (PCI_OFFSET_MASK + 1U)) {
  31. return SMC_PCI_CALL_INVAL_PARAM;
  32. }
  33. return SMC_PCI_CALL_SUCCESS;
  34. }
  35. uint64_t pci_smc_handler(uint32_t smc_fid,
  36. u_register_t x1,
  37. u_register_t x2,
  38. u_register_t x3,
  39. u_register_t x4,
  40. void *cookie,
  41. void *handle,
  42. u_register_t flags)
  43. {
  44. switch (smc_fid) {
  45. case SMC_PCI_VERSION: {
  46. pcie_version ver;
  47. ver.major = 1U;
  48. ver.minor = 0U;
  49. SMC_RET4(handle, ver.val, 0U, 0U, 0U);
  50. }
  51. case SMC_PCI_FEATURES:
  52. switch (x1) {
  53. case SMC_PCI_VERSION:
  54. case SMC_PCI_FEATURES:
  55. case SMC_PCI_READ:
  56. case SMC_PCI_WRITE:
  57. case SMC_PCI_SEG_INFO:
  58. SMC_RET1(handle, SMC_PCI_CALL_SUCCESS);
  59. default:
  60. SMC_RET1(handle, SMC_PCI_CALL_NOT_SUPPORTED);
  61. }
  62. break;
  63. case SMC_PCI_READ: {
  64. uint32_t ret;
  65. if (validate_rw_addr_sz(x1, x2, x3) != SMC_PCI_CALL_SUCCESS) {
  66. SMC_RET2(handle, SMC_PCI_CALL_INVAL_PARAM, 0U);
  67. }
  68. if (x4 != 0U) {
  69. SMC_RET2(handle, SMC_PCI_CALL_INVAL_PARAM, 0U);
  70. }
  71. if (pci_read_config(x1, x2, x3, &ret) != 0U) {
  72. SMC_RET2(handle, SMC_PCI_CALL_INVAL_PARAM, 0U);
  73. } else {
  74. SMC_RET2(handle, SMC_PCI_CALL_SUCCESS, ret);
  75. }
  76. break;
  77. }
  78. case SMC_PCI_WRITE: {
  79. uint32_t ret;
  80. if (validate_rw_addr_sz(x1, x2, x3) != SMC_PCI_CALL_SUCCESS) {
  81. SMC_RET1(handle, SMC_PCI_CALL_INVAL_PARAM);
  82. }
  83. ret = pci_write_config(x1, x2, x3, x4);
  84. SMC_RET1(handle, ret);
  85. break;
  86. }
  87. case SMC_PCI_SEG_INFO: {
  88. uint32_t nseg;
  89. uint32_t ret;
  90. uint32_t start_end_bus;
  91. if ((x2 != 0U) || (x3 != 0U) || (x4 != 0U)) {
  92. SMC_RET3(handle, SMC_PCI_CALL_INVAL_PARAM, 0U, 0U);
  93. }
  94. ret = pci_get_bus_for_seg(x1, &start_end_bus, &nseg);
  95. SMC_RET3(handle, ret, start_end_bus, nseg);
  96. break;
  97. }
  98. default:
  99. /* should be unreachable */
  100. WARN("Unimplemented PCI Service Call: 0x%x\n", smc_fid);
  101. SMC_RET1(handle, SMC_PCI_CALL_NOT_SUPPORTED);
  102. }
  103. }