highmem.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (C) 2015 Synopsys, Inc. (www.synopsys.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. */
  9. #include <linux/bootmem.h>
  10. #include <linux/export.h>
  11. #include <linux/highmem.h>
  12. #include <asm/processor.h>
  13. #include <asm/pgtable.h>
  14. #include <asm/pgalloc.h>
  15. #include <asm/tlbflush.h>
  16. /*
  17. * HIGHMEM API:
  18. *
  19. * kmap() API provides sleep semantics hence referred to as "permanent maps"
  20. * It allows mapping LAST_PKMAP pages, using @last_pkmap_nr as the cursor
  21. * for book-keeping
  22. *
  23. * kmap_atomic() can't sleep (calls pagefault_disable()), thus it provides
  24. * shortlived ala "temporary mappings" which historically were implemented as
  25. * fixmaps (compile time addr etc). Their book-keeping is done per cpu.
  26. *
  27. * Both these facts combined (preemption disabled and per-cpu allocation)
  28. * means the total number of concurrent fixmaps will be limited to max
  29. * such allocations in a single control path. Thus KM_TYPE_NR (another
  30. * historic relic) is a small'ish number which caps max percpu fixmaps
  31. *
  32. * ARC HIGHMEM Details
  33. *
  34. * - the kernel vaddr space from 0x7z to 0x8z (currently used by vmalloc/module)
  35. * is now shared between vmalloc and kmap (non overlapping though)
  36. *
  37. * - Both fixmap/pkmap use a dedicated page table each, hooked up to swapper PGD
  38. * This means each only has 1 PGDIR_SIZE worth of kvaddr mappings, which means
  39. * 2M of kvaddr space for typical config (8K page and 11:8:13 traversal split)
  40. *
  41. * - fixmap anyhow needs a limited number of mappings. So 2M kvaddr == 256 PTE
  42. * slots across NR_CPUS would be more than sufficient (generic code defines
  43. * KM_TYPE_NR as 20).
  44. *
  45. * - pkmap being preemptible, in theory could do with more than 256 concurrent
  46. * mappings. However, generic pkmap code: map_new_virtual(), doesn't traverse
  47. * the PGD and only works with a single page table @pkmap_page_table, hence
  48. * sets the limit
  49. */
  50. extern pte_t * pkmap_page_table;
  51. static pte_t * fixmap_page_table;
  52. void *kmap(struct page *page)
  53. {
  54. BUG_ON(in_interrupt());
  55. if (!PageHighMem(page))
  56. return page_address(page);
  57. return kmap_high(page);
  58. }
  59. EXPORT_SYMBOL(kmap);
  60. void *kmap_atomic(struct page *page)
  61. {
  62. int idx, cpu_idx;
  63. unsigned long vaddr;
  64. preempt_disable();
  65. pagefault_disable();
  66. if (!PageHighMem(page))
  67. return page_address(page);
  68. cpu_idx = kmap_atomic_idx_push();
  69. idx = cpu_idx + KM_TYPE_NR * smp_processor_id();
  70. vaddr = FIXMAP_ADDR(idx);
  71. set_pte_at(&init_mm, vaddr, fixmap_page_table + idx,
  72. mk_pte(page, kmap_prot));
  73. return (void *)vaddr;
  74. }
  75. EXPORT_SYMBOL(kmap_atomic);
  76. void __kunmap_atomic(void *kv)
  77. {
  78. unsigned long kvaddr = (unsigned long)kv;
  79. if (kvaddr >= FIXMAP_BASE && kvaddr < (FIXMAP_BASE + FIXMAP_SIZE)) {
  80. /*
  81. * Because preemption is disabled, this vaddr can be associated
  82. * with the current allocated index.
  83. * But in case of multiple live kmap_atomic(), it still relies on
  84. * callers to unmap in right order.
  85. */
  86. int cpu_idx = kmap_atomic_idx();
  87. int idx = cpu_idx + KM_TYPE_NR * smp_processor_id();
  88. WARN_ON(kvaddr != FIXMAP_ADDR(idx));
  89. pte_clear(&init_mm, kvaddr, fixmap_page_table + idx);
  90. local_flush_tlb_kernel_range(kvaddr, kvaddr + PAGE_SIZE);
  91. kmap_atomic_idx_pop();
  92. }
  93. pagefault_enable();
  94. preempt_enable();
  95. }
  96. EXPORT_SYMBOL(__kunmap_atomic);
  97. static noinline pte_t * __init alloc_kmap_pgtable(unsigned long kvaddr)
  98. {
  99. pgd_t *pgd_k;
  100. pud_t *pud_k;
  101. pmd_t *pmd_k;
  102. pte_t *pte_k;
  103. pgd_k = pgd_offset_k(kvaddr);
  104. pud_k = pud_offset(pgd_k, kvaddr);
  105. pmd_k = pmd_offset(pud_k, kvaddr);
  106. pte_k = (pte_t *)alloc_bootmem_low_pages(PAGE_SIZE);
  107. pmd_populate_kernel(&init_mm, pmd_k, pte_k);
  108. return pte_k;
  109. }
  110. void __init kmap_init(void)
  111. {
  112. /* Due to recursive include hell, we can't do this in processor.h */
  113. BUILD_BUG_ON(PAGE_OFFSET < (VMALLOC_END + FIXMAP_SIZE + PKMAP_SIZE));
  114. BUILD_BUG_ON(KM_TYPE_NR > PTRS_PER_PTE);
  115. pkmap_page_table = alloc_kmap_pgtable(PKMAP_BASE);
  116. BUILD_BUG_ON(LAST_PKMAP > PTRS_PER_PTE);
  117. fixmap_page_table = alloc_kmap_pgtable(FIXMAP_BASE);
  118. }