qcom-iommu-util.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #ifndef __QCOM_IOMMU_UTIL_H
  7. #define __QCOM_IOMMU_UTIL_H
  8. #include <linux/iommu.h>
  9. #include <linux/dma-mapping.h>
  10. #include <linux/iova.h>
  11. #include <soc/qcom/secure_buffer.h>
  12. /* IOMMU fault behaviors */
  13. #define QCOM_IOMMU_FAULT_MODEL_NON_FATAL BIT(0)
  14. #define QCOM_IOMMU_FAULT_MODEL_NO_CFRE BIT(1)
  15. #define QCOM_IOMMU_FAULT_MODEL_NO_STALL BIT(2)
  16. #define QCOM_IOMMU_FAULT_MODEL_HUPCF BIT(3)
  17. /* IOMMU mapping configurations */
  18. #define QCOM_IOMMU_MAPPING_CONF_S1_BYPASS BIT(0)
  19. #define QCOM_IOMMU_MAPPING_CONF_ATOMIC BIT(1)
  20. #define QCOM_IOMMU_MAPPING_CONF_FAST BIT(2)
  21. /* iommu transaction flags */
  22. /* 1 Write, 0 Read */
  23. #define QCOM_IOMMU_ATOS_TRANS_WRITE BIT(0)
  24. /* 1 Privileged, 0 Unprivileged */
  25. #define QCOM_IOMMU_ATOS_TRANS_PRIV BIT(1)
  26. /* 1 Instruction fetch, 0 Data access */
  27. #define QCOM_IOMMU_ATOS_TRANS_INST BIT(2)
  28. /* Non secure unprivileged Data read operation */
  29. #define QCOM_IOMMU_ATOS_TRANS_DEFAULT (0U)
  30. #ifndef IOMMU_SYS_CACHE
  31. /* Attributes are not supported, so render them ineffective. */
  32. #define IOMMU_SYS_CACHE (0)
  33. #define IOMMU_SYS_CACHE_NWA (0)
  34. #endif
  35. /* Use upstream device's bus attribute */
  36. #define IOMMU_USE_UPSTREAM_HINT (IOMMU_SYS_CACHE)
  37. /* Use upstream device's bus attribute with no write-allocate cache policy */
  38. #define IOMMU_USE_LLC_NWA (IOMMU_SYS_CACHE_NWA)
  39. /* vendor iommu fault flags */
  40. #define IOMMU_FAULT_TRANSLATION (1 << 2)
  41. #define IOMMU_FAULT_PERMISSION (1 << 3)
  42. #define IOMMU_FAULT_EXTERNAL (1 << 4)
  43. #define IOMMU_FAULT_TRANSACTION_STALLED (1 << 5)
  44. /* iommu transaction flags */
  45. #define IOMMU_TRANS_WRITE BIT(0) /* 1 Write, 0 Read */
  46. #define IOMMU_TRANS_PRIV BIT(1) /* 1 Privileged, 0 Unprivileged */
  47. #define IOMMU_TRANS_INST BIT(2) /* 1 Instruction fetch, 0 Data access */
  48. #define IOMMU_TRANS_SEC BIT(3) /* 1 Secure, 0 Non-secure access*/
  49. /* Non secure unprivileged Data read operation */
  50. #define IOMMU_TRANS_DEFAULT (0U)
  51. typedef void (*fault_handler_irq_t)(struct iommu_domain *, void *);
  52. struct iommu_pgtbl_info {
  53. void *ops;
  54. };
  55. struct qcom_iommu_atos_txn {
  56. u64 addr;
  57. u32 flags;
  58. u32 id;
  59. };
  60. enum sid_switch_direction {
  61. SID_ACQUIRE,
  62. SID_RELEASE,
  63. };
  64. struct qcom_iommu_fault_ids {
  65. u32 bid;
  66. u32 pid;
  67. u32 mid;
  68. };
  69. /*
  70. * @sid_switch: add/remove all SIDS in the iommu domain containing dev from
  71. * iommu registers.
  72. */
  73. struct qcom_iommu_ops {
  74. phys_addr_t (*iova_to_phys_hard)(struct iommu_domain *domain,
  75. struct qcom_iommu_atos_txn *txn);
  76. int (*sid_switch)(struct device *dev, enum sid_switch_direction dir);
  77. int (*get_fault_ids)(struct iommu_domain *domain,
  78. struct qcom_iommu_fault_ids *ids);
  79. int (*get_context_bank_nr)(struct iommu_domain *domain);
  80. int (*get_asid_nr)(struct iommu_domain *domain);
  81. int (*set_secure_vmid)(struct iommu_domain *domain, enum vmid vmid);
  82. int (*set_fault_model)(struct iommu_domain *domain, int fault_model);
  83. void (*set_fault_handler_irq)(struct iommu_domain *domain,
  84. fault_handler_irq_t handler_irq, void *token);
  85. int (*enable_s1_translation)(struct iommu_domain *domain);
  86. int (*get_mappings_configuration)(struct iommu_domain *domain);
  87. void (*skip_tlb_management)(struct iommu_domain *domain, bool skip);
  88. struct iommu_ops iommu_ops;
  89. struct iommu_domain_ops domain_ops;
  90. };
  91. #define to_qcom_iommu_ops(x) (container_of(x, struct qcom_iommu_ops, domain_ops))
  92. struct device_node *qcom_iommu_group_parse_phandle(struct device *dev);
  93. int qcom_iommu_generate_dma_regions(struct device *dev,
  94. struct list_head *head);
  95. void qcom_iommu_generate_resv_regions(struct device *dev,
  96. struct list_head *list);
  97. int qcom_iommu_get_fast_iova_range(struct device *dev,
  98. dma_addr_t *ret_iova_base,
  99. dma_addr_t *ret_iova_end);
  100. /* Remove once this function is exported by upstream kernel */
  101. void qcom_iommu_get_resv_regions(struct device *dev, struct list_head *list);
  102. phys_addr_t qcom_iommu_iova_to_phys_hard(struct iommu_domain *domain,
  103. struct qcom_iommu_atos_txn *txn);
  104. int qcom_iommu_sid_switch(struct device *dev, enum sid_switch_direction dir);
  105. int qcom_skip_tlb_management(struct device *dev, bool skip);
  106. extern int qcom_iommu_get_fault_ids(struct iommu_domain *domain,
  107. struct qcom_iommu_fault_ids *f_ids);
  108. extern int qcom_iommu_get_msi_size(struct device *dev, u32 *msi_size);
  109. int qcom_iommu_get_context_bank_nr(struct iommu_domain *domain);
  110. int qcom_iommu_get_asid_nr(struct iommu_domain *domain);
  111. int qcom_iommu_set_secure_vmid(struct iommu_domain *domain, enum vmid vmid);
  112. int qcom_iommu_set_fault_model(struct iommu_domain *domain, int fault_model);
  113. int qcom_iommu_set_fault_handler_irq(struct iommu_domain *domain,
  114. fault_handler_irq_t handler_irq, void *token);
  115. int qcom_iommu_enable_s1_translation(struct iommu_domain *domain);
  116. int qcom_iommu_get_mappings_configuration(struct iommu_domain *domain);
  117. #ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE
  118. int __init qcom_arm_lpae_do_selftests(void);
  119. #else
  120. static inline int __init qcom_arm_lpae_do_selftests(void)
  121. {
  122. return 0;
  123. }
  124. #endif
  125. #endif /* __QCOM_IOMMU_UTIL_H */