ioremap.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/mm.h>
  3. #include <linux/io.h>
  4. static ioremap_prot_hook_t ioremap_prot_hook;
  5. int arm64_ioremap_prot_hook_register(ioremap_prot_hook_t hook)
  6. {
  7. if (WARN_ON(ioremap_prot_hook))
  8. return -EBUSY;
  9. ioremap_prot_hook = hook;
  10. return 0;
  11. }
  12. void __iomem *ioremap_prot(phys_addr_t phys_addr, size_t size,
  13. unsigned long prot)
  14. {
  15. unsigned long last_addr = phys_addr + size - 1;
  16. pgprot_t pgprot = __pgprot(prot);
  17. /* Don't allow outside PHYS_MASK */
  18. if (last_addr & ~PHYS_MASK)
  19. return NULL;
  20. /* Don't allow RAM to be mapped. */
  21. if (WARN_ON(pfn_is_map_memory(__phys_to_pfn(phys_addr))))
  22. return NULL;
  23. /*
  24. * If a hook is registered (e.g. for confidential computing
  25. * purposes), call that now and barf if it fails.
  26. */
  27. if (unlikely(ioremap_prot_hook) &&
  28. WARN_ON(ioremap_prot_hook(phys_addr, size, &pgprot))) {
  29. return NULL;
  30. }
  31. return generic_ioremap_prot(phys_addr, size, pgprot);
  32. }
  33. EXPORT_SYMBOL(ioremap_prot);
  34. /*
  35. * Must be called after early_fixmap_init
  36. */
  37. void __init early_ioremap_init(void)
  38. {
  39. early_ioremap_setup();
  40. }
  41. bool arch_memremap_can_ram_remap(resource_size_t offset, size_t size,
  42. unsigned long flags)
  43. {
  44. unsigned long pfn = PHYS_PFN(offset);
  45. return pfn_is_map_memory(pfn);
  46. }