io-pgtable.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Generic page table allocator for IOMMUs.
  4. *
  5. * Copyright (C) 2014 ARM Limited
  6. *
  7. * Author: Will Deacon <will.deacon@arm.com>
  8. */
  9. #include <linux/bug.h>
  10. #include <linux/io-pgtable.h>
  11. #include <linux/kernel.h>
  12. #include <linux/types.h>
  13. static const struct io_pgtable_init_fns *
  14. io_pgtable_init_table[IO_PGTABLE_NUM_FMTS] = {
  15. #ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE
  16. [ARM_32_LPAE_S1] = &io_pgtable_arm_32_lpae_s1_init_fns,
  17. [ARM_32_LPAE_S2] = &io_pgtable_arm_32_lpae_s2_init_fns,
  18. [ARM_64_LPAE_S1] = &io_pgtable_arm_64_lpae_s1_init_fns,
  19. [ARM_64_LPAE_S2] = &io_pgtable_arm_64_lpae_s2_init_fns,
  20. [ARM_MALI_LPAE] = &io_pgtable_arm_mali_lpae_init_fns,
  21. #endif
  22. #ifdef CONFIG_IOMMU_IO_PGTABLE_ARMV7S
  23. [ARM_V7S] = &io_pgtable_arm_v7s_init_fns,
  24. #endif
  25. };
  26. struct io_pgtable_ops *alloc_io_pgtable_ops(enum io_pgtable_fmt fmt,
  27. struct io_pgtable_cfg *cfg,
  28. void *cookie)
  29. {
  30. struct io_pgtable *iop;
  31. const struct io_pgtable_init_fns *fns;
  32. if (fmt >= IO_PGTABLE_NUM_FMTS)
  33. return NULL;
  34. fns = io_pgtable_init_table[fmt];
  35. if (!fns)
  36. return NULL;
  37. iop = fns->alloc(cfg, cookie);
  38. if (!iop)
  39. return NULL;
  40. iop->fmt = fmt;
  41. iop->cookie = cookie;
  42. iop->cfg = *cfg;
  43. return &iop->ops;
  44. }
  45. EXPORT_SYMBOL_GPL(alloc_io_pgtable_ops);
  46. /*
  47. * It is the IOMMU driver's responsibility to ensure that the page table
  48. * is no longer accessible to the walker by this point.
  49. */
  50. void free_io_pgtable_ops(struct io_pgtable_ops *ops)
  51. {
  52. struct io_pgtable *iop;
  53. if (!ops)
  54. return;
  55. iop = io_pgtable_ops_to_pgtable(ops);
  56. io_pgtable_tlb_flush_all(iop);
  57. io_pgtable_init_table[iop->fmt]->free(iop);
  58. }
  59. EXPORT_SYMBOL_GPL(free_io_pgtable_ops);