msm_iommu.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* Copyright (c) 2013, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/errno.h>
  14. #include <linux/err.h>
  15. #include <linux/list.h>
  16. #include <linux/mutex.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/export.h>
  19. #include <linux/iommu.h>
  20. #include <mach/iommu.h>
  21. static DEFINE_MUTEX(iommu_list_lock);
  22. static LIST_HEAD(iommu_list);
  23. static struct iommu_access_ops *iommu_access_ops;
  24. struct bus_type msm_iommu_sec_bus_type = {
  25. .name = "msm_iommu_sec_bus",
  26. };
  27. void msm_set_iommu_access_ops(struct iommu_access_ops *ops)
  28. {
  29. iommu_access_ops = ops;
  30. }
  31. struct iommu_access_ops *msm_get_iommu_access_ops()
  32. {
  33. BUG_ON(iommu_access_ops == NULL);
  34. return iommu_access_ops;
  35. }
  36. EXPORT_SYMBOL(msm_get_iommu_access_ops);
  37. void msm_iommu_add_drv(struct msm_iommu_drvdata *drv)
  38. {
  39. mutex_lock(&iommu_list_lock);
  40. list_add(&drv->list, &iommu_list);
  41. mutex_unlock(&iommu_list_lock);
  42. }
  43. void msm_iommu_remove_drv(struct msm_iommu_drvdata *drv)
  44. {
  45. mutex_lock(&iommu_list_lock);
  46. list_del(&drv->list);
  47. mutex_unlock(&iommu_list_lock);
  48. }
  49. static int find_iommu_ctx(struct device *dev, void *data)
  50. {
  51. struct msm_iommu_ctx_drvdata *c;
  52. c = dev_get_drvdata(dev);
  53. if (!c || !c->name)
  54. return 0;
  55. return !strcmp(data, c->name);
  56. }
  57. static struct device *find_context(struct device *dev, const char *name)
  58. {
  59. return device_find_child(dev, (void *)name, find_iommu_ctx);
  60. }
  61. struct device *msm_iommu_get_ctx(const char *ctx_name)
  62. {
  63. struct msm_iommu_drvdata *drv;
  64. struct device *dev = NULL;
  65. mutex_lock(&iommu_list_lock);
  66. list_for_each_entry(drv, &iommu_list, list) {
  67. dev = find_context(drv->dev, ctx_name);
  68. if (dev)
  69. break;
  70. }
  71. mutex_unlock(&iommu_list_lock);
  72. put_device(dev);
  73. if (!dev || !dev_get_drvdata(dev)) {
  74. pr_debug("Could not find context <%s>\n", ctx_name);
  75. dev = ERR_PTR(-EPROBE_DEFER);
  76. }
  77. return dev;
  78. }
  79. EXPORT_SYMBOL(msm_iommu_get_ctx);