GNU Linux-libre 4.14.332-gnu1
[releases.git] / arch / x86 / mm / ioremap.c
1 /*
2  * Re-map IO memory to kernel address space so that we can access it.
3  * This is needed for high PCI addresses that aren't mapped in the
4  * 640k-1MB IO memory area on PC's
5  *
6  * (C) Copyright 1995 1996 Linus Torvalds
7  */
8
9 #include <linux/bootmem.h>
10 #include <linux/init.h>
11 #include <linux/io.h>
12 #include <linux/ioport.h>
13 #include <linux/slab.h>
14 #include <linux/vmalloc.h>
15 #include <linux/mmiotrace.h>
16 #include <linux/mem_encrypt.h>
17 #include <linux/efi.h>
18
19 #include <asm/set_memory.h>
20 #include <asm/e820/api.h>
21 #include <asm/fixmap.h>
22 #include <asm/pgtable.h>
23 #include <asm/tlbflush.h>
24 #include <asm/pgalloc.h>
25 #include <asm/pat.h>
26 #include <asm/setup.h>
27
28 #include "physaddr.h"
29
30 /*
31  * Fix up the linear direct mapping of the kernel to avoid cache attribute
32  * conflicts.
33  */
34 int ioremap_change_attr(unsigned long vaddr, unsigned long size,
35                         enum page_cache_mode pcm)
36 {
37         unsigned long nrpages = size >> PAGE_SHIFT;
38         int err;
39
40         switch (pcm) {
41         case _PAGE_CACHE_MODE_UC:
42         default:
43                 err = _set_memory_uc(vaddr, nrpages);
44                 break;
45         case _PAGE_CACHE_MODE_WC:
46                 err = _set_memory_wc(vaddr, nrpages);
47                 break;
48         case _PAGE_CACHE_MODE_WT:
49                 err = _set_memory_wt(vaddr, nrpages);
50                 break;
51         case _PAGE_CACHE_MODE_WB:
52                 err = _set_memory_wb(vaddr, nrpages);
53                 break;
54         }
55
56         return err;
57 }
58
59 static int __ioremap_check_ram(unsigned long start_pfn, unsigned long nr_pages,
60                                void *arg)
61 {
62         unsigned long i;
63
64         for (i = 0; i < nr_pages; ++i)
65                 if (pfn_valid(start_pfn + i) &&
66                     !PageReserved(pfn_to_page(start_pfn + i)))
67                         return 1;
68
69         return 0;
70 }
71
72 /*
73  * Remap an arbitrary physical address space into the kernel virtual
74  * address space. It transparently creates kernel huge I/O mapping when
75  * the physical address is aligned by a huge page size (1GB or 2MB) and
76  * the requested size is at least the huge page size.
77  *
78  * NOTE: MTRRs can override PAT memory types with a 4KB granularity.
79  * Therefore, the mapping code falls back to use a smaller page toward 4KB
80  * when a mapping range is covered by non-WB type of MTRRs.
81  *
82  * NOTE! We need to allow non-page-aligned mappings too: we will obviously
83  * have to convert them into an offset in a page-aligned mapping, but the
84  * caller shouldn't need to know that small detail.
85  */
86 static void __iomem *__ioremap_caller(resource_size_t phys_addr,
87                 unsigned long size, enum page_cache_mode pcm, void *caller)
88 {
89         unsigned long offset, vaddr;
90         resource_size_t pfn, last_pfn, last_addr;
91         const resource_size_t unaligned_phys_addr = phys_addr;
92         const unsigned long unaligned_size = size;
93         struct vm_struct *area;
94         enum page_cache_mode new_pcm;
95         pgprot_t prot;
96         int retval;
97         void __iomem *ret_addr;
98
99         /* Don't allow wraparound or zero size */
100         last_addr = phys_addr + size - 1;
101         if (!size || last_addr < phys_addr)
102                 return NULL;
103
104         if (!phys_addr_valid(phys_addr)) {
105                 printk(KERN_WARNING "ioremap: invalid physical address %llx\n",
106                        (unsigned long long)phys_addr);
107                 WARN_ON_ONCE(1);
108                 return NULL;
109         }
110
111         /*
112          * Don't allow anybody to remap normal RAM that we're using..
113          */
114         pfn      = phys_addr >> PAGE_SHIFT;
115         last_pfn = last_addr >> PAGE_SHIFT;
116         if (walk_system_ram_range(pfn, last_pfn - pfn + 1, NULL,
117                                           __ioremap_check_ram) == 1) {
118                 WARN_ONCE(1, "ioremap on RAM at %pa - %pa\n",
119                           &phys_addr, &last_addr);
120                 return NULL;
121         }
122
123         /*
124          * Mappings have to be page-aligned
125          */
126         offset = phys_addr & ~PAGE_MASK;
127         phys_addr &= PAGE_MASK;
128         size = PAGE_ALIGN(last_addr+1) - phys_addr;
129
130         /*
131          * Mask out any bits not part of the actual physical
132          * address, like memory encryption bits.
133          */
134         phys_addr &= PHYSICAL_PAGE_MASK;
135
136         retval = reserve_memtype(phys_addr, (u64)phys_addr + size,
137                                                 pcm, &new_pcm);
138         if (retval) {
139                 printk(KERN_ERR "ioremap reserve_memtype failed %d\n", retval);
140                 return NULL;
141         }
142
143         if (pcm != new_pcm) {
144                 if (!is_new_memtype_allowed(phys_addr, size, pcm, new_pcm)) {
145                         printk(KERN_ERR
146                 "ioremap error for 0x%llx-0x%llx, requested 0x%x, got 0x%x\n",
147                                 (unsigned long long)phys_addr,
148                                 (unsigned long long)(phys_addr + size),
149                                 pcm, new_pcm);
150                         goto err_free_memtype;
151                 }
152                 pcm = new_pcm;
153         }
154
155         prot = PAGE_KERNEL_IO;
156         switch (pcm) {
157         case _PAGE_CACHE_MODE_UC:
158         default:
159                 prot = __pgprot(pgprot_val(prot) |
160                                 cachemode2protval(_PAGE_CACHE_MODE_UC));
161                 break;
162         case _PAGE_CACHE_MODE_UC_MINUS:
163                 prot = __pgprot(pgprot_val(prot) |
164                                 cachemode2protval(_PAGE_CACHE_MODE_UC_MINUS));
165                 break;
166         case _PAGE_CACHE_MODE_WC:
167                 prot = __pgprot(pgprot_val(prot) |
168                                 cachemode2protval(_PAGE_CACHE_MODE_WC));
169                 break;
170         case _PAGE_CACHE_MODE_WT:
171                 prot = __pgprot(pgprot_val(prot) |
172                                 cachemode2protval(_PAGE_CACHE_MODE_WT));
173                 break;
174         case _PAGE_CACHE_MODE_WB:
175                 break;
176         }
177
178         /*
179          * Ok, go for it..
180          */
181         area = get_vm_area_caller(size, VM_IOREMAP, caller);
182         if (!area)
183                 goto err_free_memtype;
184         area->phys_addr = phys_addr;
185         vaddr = (unsigned long) area->addr;
186
187         if (kernel_map_sync_memtype(phys_addr, size, pcm))
188                 goto err_free_area;
189
190         if (ioremap_page_range(vaddr, vaddr + size, phys_addr, prot))
191                 goto err_free_area;
192
193         ret_addr = (void __iomem *) (vaddr + offset);
194         mmiotrace_ioremap(unaligned_phys_addr, unaligned_size, ret_addr);
195
196         /*
197          * Check if the request spans more than any BAR in the iomem resource
198          * tree.
199          */
200         if (iomem_map_sanity_check(unaligned_phys_addr, unaligned_size))
201                 pr_warn("caller %pS mapping multiple BARs\n", caller);
202
203         return ret_addr;
204 err_free_area:
205         free_vm_area(area);
206 err_free_memtype:
207         free_memtype(phys_addr, phys_addr + size);
208         return NULL;
209 }
210
211 /**
212  * ioremap_nocache     -   map bus memory into CPU space
213  * @phys_addr:    bus address of the memory
214  * @size:      size of the resource to map
215  *
216  * ioremap_nocache performs a platform specific sequence of operations to
217  * make bus memory CPU accessible via the readb/readw/readl/writeb/
218  * writew/writel functions and the other mmio helpers. The returned
219  * address is not guaranteed to be usable directly as a virtual
220  * address.
221  *
222  * This version of ioremap ensures that the memory is marked uncachable
223  * on the CPU as well as honouring existing caching rules from things like
224  * the PCI bus. Note that there are other caches and buffers on many
225  * busses. In particular driver authors should read up on PCI writes
226  *
227  * It's useful if some control registers are in such an area and
228  * write combining or read caching is not desirable:
229  *
230  * Must be freed with iounmap.
231  */
232 void __iomem *ioremap_nocache(resource_size_t phys_addr, unsigned long size)
233 {
234         /*
235          * Ideally, this should be:
236          *      pat_enabled() ? _PAGE_CACHE_MODE_UC : _PAGE_CACHE_MODE_UC_MINUS;
237          *
238          * Till we fix all X drivers to use ioremap_wc(), we will use
239          * UC MINUS. Drivers that are certain they need or can already
240          * be converted over to strong UC can use ioremap_uc().
241          */
242         enum page_cache_mode pcm = _PAGE_CACHE_MODE_UC_MINUS;
243
244         return __ioremap_caller(phys_addr, size, pcm,
245                                 __builtin_return_address(0));
246 }
247 EXPORT_SYMBOL(ioremap_nocache);
248
249 /**
250  * ioremap_uc     -   map bus memory into CPU space as strongly uncachable
251  * @phys_addr:    bus address of the memory
252  * @size:      size of the resource to map
253  *
254  * ioremap_uc performs a platform specific sequence of operations to
255  * make bus memory CPU accessible via the readb/readw/readl/writeb/
256  * writew/writel functions and the other mmio helpers. The returned
257  * address is not guaranteed to be usable directly as a virtual
258  * address.
259  *
260  * This version of ioremap ensures that the memory is marked with a strong
261  * preference as completely uncachable on the CPU when possible. For non-PAT
262  * systems this ends up setting page-attribute flags PCD=1, PWT=1. For PAT
263  * systems this will set the PAT entry for the pages as strong UC.  This call
264  * will honor existing caching rules from things like the PCI bus. Note that
265  * there are other caches and buffers on many busses. In particular driver
266  * authors should read up on PCI writes.
267  *
268  * It's useful if some control registers are in such an area and
269  * write combining or read caching is not desirable:
270  *
271  * Must be freed with iounmap.
272  */
273 void __iomem *ioremap_uc(resource_size_t phys_addr, unsigned long size)
274 {
275         enum page_cache_mode pcm = _PAGE_CACHE_MODE_UC;
276
277         return __ioremap_caller(phys_addr, size, pcm,
278                                 __builtin_return_address(0));
279 }
280 EXPORT_SYMBOL_GPL(ioremap_uc);
281
282 /**
283  * ioremap_wc   -       map memory into CPU space write combined
284  * @phys_addr:  bus address of the memory
285  * @size:       size of the resource to map
286  *
287  * This version of ioremap ensures that the memory is marked write combining.
288  * Write combining allows faster writes to some hardware devices.
289  *
290  * Must be freed with iounmap.
291  */
292 void __iomem *ioremap_wc(resource_size_t phys_addr, unsigned long size)
293 {
294         return __ioremap_caller(phys_addr, size, _PAGE_CACHE_MODE_WC,
295                                         __builtin_return_address(0));
296 }
297 EXPORT_SYMBOL(ioremap_wc);
298
299 /**
300  * ioremap_wt   -       map memory into CPU space write through
301  * @phys_addr:  bus address of the memory
302  * @size:       size of the resource to map
303  *
304  * This version of ioremap ensures that the memory is marked write through.
305  * Write through stores data into memory while keeping the cache up-to-date.
306  *
307  * Must be freed with iounmap.
308  */
309 void __iomem *ioremap_wt(resource_size_t phys_addr, unsigned long size)
310 {
311         return __ioremap_caller(phys_addr, size, _PAGE_CACHE_MODE_WT,
312                                         __builtin_return_address(0));
313 }
314 EXPORT_SYMBOL(ioremap_wt);
315
316 void __iomem *ioremap_cache(resource_size_t phys_addr, unsigned long size)
317 {
318         return __ioremap_caller(phys_addr, size, _PAGE_CACHE_MODE_WB,
319                                 __builtin_return_address(0));
320 }
321 EXPORT_SYMBOL(ioremap_cache);
322
323 void __iomem *ioremap_prot(resource_size_t phys_addr, unsigned long size,
324                                 unsigned long prot_val)
325 {
326         return __ioremap_caller(phys_addr, size,
327                                 pgprot2cachemode(__pgprot(prot_val)),
328                                 __builtin_return_address(0));
329 }
330 EXPORT_SYMBOL(ioremap_prot);
331
332 /**
333  * iounmap - Free a IO remapping
334  * @addr: virtual address from ioremap_*
335  *
336  * Caller must ensure there is only one unmapping for the same pointer.
337  */
338 void iounmap(volatile void __iomem *addr)
339 {
340         struct vm_struct *p, *o;
341
342         if ((void __force *)addr <= high_memory)
343                 return;
344
345         /*
346          * The PCI/ISA range special-casing was removed from __ioremap()
347          * so this check, in theory, can be removed. However, there are
348          * cases where iounmap() is called for addresses not obtained via
349          * ioremap() (vga16fb for example). Add a warning so that these
350          * cases can be caught and fixed.
351          */
352         if ((void __force *)addr >= phys_to_virt(ISA_START_ADDRESS) &&
353             (void __force *)addr < phys_to_virt(ISA_END_ADDRESS)) {
354                 WARN(1, "iounmap() called for ISA range not obtained using ioremap()\n");
355                 return;
356         }
357
358         mmiotrace_iounmap(addr);
359
360         addr = (volatile void __iomem *)
361                 (PAGE_MASK & (unsigned long __force)addr);
362
363         /* Use the vm area unlocked, assuming the caller
364            ensures there isn't another iounmap for the same address
365            in parallel. Reuse of the virtual address is prevented by
366            leaving it in the global lists until we're done with it.
367            cpa takes care of the direct mappings. */
368         p = find_vm_area((void __force *)addr);
369
370         if (!p) {
371                 printk(KERN_ERR "iounmap: bad address %p\n", addr);
372                 dump_stack();
373                 return;
374         }
375
376         free_memtype(p->phys_addr, p->phys_addr + get_vm_area_size(p));
377
378         /* Finally remove it */
379         o = remove_vm_area((void __force *)addr);
380         BUG_ON(p != o || o == NULL);
381         kfree(p);
382 }
383 EXPORT_SYMBOL(iounmap);
384
385 int __init arch_ioremap_pud_supported(void)
386 {
387 #ifdef CONFIG_X86_64
388         return boot_cpu_has(X86_FEATURE_GBPAGES);
389 #else
390         return 0;
391 #endif
392 }
393
394 int __init arch_ioremap_pmd_supported(void)
395 {
396         return boot_cpu_has(X86_FEATURE_PSE);
397 }
398
399 /*
400  * Convert a physical pointer to a virtual kernel pointer for /dev/mem
401  * access
402  */
403 void *xlate_dev_mem_ptr(phys_addr_t phys)
404 {
405         unsigned long start  = phys &  PAGE_MASK;
406         unsigned long offset = phys & ~PAGE_MASK;
407         void *vaddr;
408
409         /* memremap() maps if RAM, otherwise falls back to ioremap() */
410         vaddr = memremap(start, PAGE_SIZE, MEMREMAP_WB);
411
412         /* Only add the offset on success and return NULL if memremap() failed */
413         if (vaddr)
414                 vaddr += offset;
415
416         return vaddr;
417 }
418
419 void unxlate_dev_mem_ptr(phys_addr_t phys, void *addr)
420 {
421         memunmap((void *)((unsigned long)addr & PAGE_MASK));
422 }
423
424 /*
425  * Examine the physical address to determine if it is an area of memory
426  * that should be mapped decrypted.  If the memory is not part of the
427  * kernel usable area it was accessed and created decrypted, so these
428  * areas should be mapped decrypted. And since the encryption key can
429  * change across reboots, persistent memory should also be mapped
430  * decrypted.
431  */
432 static bool memremap_should_map_decrypted(resource_size_t phys_addr,
433                                           unsigned long size)
434 {
435         int is_pmem;
436
437         /*
438          * Check if the address is part of a persistent memory region.
439          * This check covers areas added by E820, EFI and ACPI.
440          */
441         is_pmem = region_intersects(phys_addr, size, IORESOURCE_MEM,
442                                     IORES_DESC_PERSISTENT_MEMORY);
443         if (is_pmem != REGION_DISJOINT)
444                 return true;
445
446         /*
447          * Check if the non-volatile attribute is set for an EFI
448          * reserved area.
449          */
450         if (efi_enabled(EFI_BOOT)) {
451                 switch (efi_mem_type(phys_addr)) {
452                 case EFI_RESERVED_TYPE:
453                         if (efi_mem_attributes(phys_addr) & EFI_MEMORY_NV)
454                                 return true;
455                         break;
456                 default:
457                         break;
458                 }
459         }
460
461         /* Check if the address is outside kernel usable area */
462         switch (e820__get_entry_type(phys_addr, phys_addr + size - 1)) {
463         case E820_TYPE_RESERVED:
464         case E820_TYPE_ACPI:
465         case E820_TYPE_NVS:
466         case E820_TYPE_UNUSABLE:
467         case E820_TYPE_PRAM:
468                 return true;
469         default:
470                 break;
471         }
472
473         return false;
474 }
475
476 /*
477  * Examine the physical address to determine if it is EFI data. Check
478  * it against the boot params structure and EFI tables and memory types.
479  */
480 static bool memremap_is_efi_data(resource_size_t phys_addr,
481                                  unsigned long size)
482 {
483         u64 paddr;
484
485         /* Check if the address is part of EFI boot/runtime data */
486         if (!efi_enabled(EFI_BOOT))
487                 return false;
488
489         paddr = boot_params.efi_info.efi_memmap_hi;
490         paddr <<= 32;
491         paddr |= boot_params.efi_info.efi_memmap;
492         if (phys_addr == paddr)
493                 return true;
494
495         paddr = boot_params.efi_info.efi_systab_hi;
496         paddr <<= 32;
497         paddr |= boot_params.efi_info.efi_systab;
498         if (phys_addr == paddr)
499                 return true;
500
501         if (efi_is_table_address(phys_addr))
502                 return true;
503
504         switch (efi_mem_type(phys_addr)) {
505         case EFI_BOOT_SERVICES_DATA:
506         case EFI_RUNTIME_SERVICES_DATA:
507                 return true;
508         default:
509                 break;
510         }
511
512         return false;
513 }
514
515 /*
516  * Examine the physical address to determine if it is boot data by checking
517  * it against the boot params setup_data chain.
518  */
519 static bool memremap_is_setup_data(resource_size_t phys_addr,
520                                    unsigned long size)
521 {
522         struct setup_data *data;
523         u64 paddr, paddr_next;
524
525         paddr = boot_params.hdr.setup_data;
526         while (paddr) {
527                 unsigned int len;
528
529                 if (phys_addr == paddr)
530                         return true;
531
532                 data = memremap(paddr, sizeof(*data),
533                                 MEMREMAP_WB | MEMREMAP_DEC);
534
535                 paddr_next = data->next;
536                 len = data->len;
537
538                 memunmap(data);
539
540                 if ((phys_addr > paddr) && (phys_addr < (paddr + len)))
541                         return true;
542
543                 paddr = paddr_next;
544         }
545
546         return false;
547 }
548
549 /*
550  * Examine the physical address to determine if it is boot data by checking
551  * it against the boot params setup_data chain (early boot version).
552  */
553 static bool __init early_memremap_is_setup_data(resource_size_t phys_addr,
554                                                 unsigned long size)
555 {
556         struct setup_data *data;
557         u64 paddr, paddr_next;
558
559         paddr = boot_params.hdr.setup_data;
560         while (paddr) {
561                 unsigned int len;
562
563                 if (phys_addr == paddr)
564                         return true;
565
566                 data = early_memremap_decrypted(paddr, sizeof(*data));
567
568                 paddr_next = data->next;
569                 len = data->len;
570
571                 early_memunmap(data, sizeof(*data));
572
573                 if ((phys_addr > paddr) && (phys_addr < (paddr + len)))
574                         return true;
575
576                 paddr = paddr_next;
577         }
578
579         return false;
580 }
581
582 /*
583  * Architecture function to determine if RAM remap is allowed. By default, a
584  * RAM remap will map the data as encrypted. Determine if a RAM remap should
585  * not be done so that the data will be mapped decrypted.
586  */
587 bool arch_memremap_can_ram_remap(resource_size_t phys_addr, unsigned long size,
588                                  unsigned long flags)
589 {
590         if (!sme_active())
591                 return true;
592
593         if (flags & MEMREMAP_ENC)
594                 return true;
595
596         if (flags & MEMREMAP_DEC)
597                 return false;
598
599         if (memremap_is_setup_data(phys_addr, size) ||
600             memremap_is_efi_data(phys_addr, size) ||
601             memremap_should_map_decrypted(phys_addr, size))
602                 return false;
603
604         return true;
605 }
606
607 /*
608  * Architecture override of __weak function to adjust the protection attributes
609  * used when remapping memory. By default, early_memremap() will map the data
610  * as encrypted. Determine if an encrypted mapping should not be done and set
611  * the appropriate protection attributes.
612  */
613 pgprot_t __init early_memremap_pgprot_adjust(resource_size_t phys_addr,
614                                              unsigned long size,
615                                              pgprot_t prot)
616 {
617         if (!sme_active())
618                 return prot;
619
620         if (early_memremap_is_setup_data(phys_addr, size) ||
621             memremap_is_efi_data(phys_addr, size) ||
622             memremap_should_map_decrypted(phys_addr, size))
623                 prot = pgprot_decrypted(prot);
624         else
625                 prot = pgprot_encrypted(prot);
626
627         return prot;
628 }
629
630 bool phys_mem_access_encrypted(unsigned long phys_addr, unsigned long size)
631 {
632         return arch_memremap_can_ram_remap(phys_addr, size, 0);
633 }
634
635 #ifdef CONFIG_AMD_MEM_ENCRYPT
636 /* Remap memory with encryption */
637 void __init *early_memremap_encrypted(resource_size_t phys_addr,
638                                       unsigned long size)
639 {
640         return early_memremap_prot(phys_addr, size, __PAGE_KERNEL_ENC);
641 }
642
643 /*
644  * Remap memory with encryption and write-protected - cannot be called
645  * before pat_init() is called
646  */
647 void __init *early_memremap_encrypted_wp(resource_size_t phys_addr,
648                                          unsigned long size)
649 {
650         /* Be sure the write-protect PAT entry is set for write-protect */
651         if (__pte2cachemode_tbl[_PAGE_CACHE_MODE_WP] != _PAGE_CACHE_MODE_WP)
652                 return NULL;
653
654         return early_memremap_prot(phys_addr, size, __PAGE_KERNEL_ENC_WP);
655 }
656
657 /* Remap memory without encryption */
658 void __init *early_memremap_decrypted(resource_size_t phys_addr,
659                                       unsigned long size)
660 {
661         return early_memremap_prot(phys_addr, size, __PAGE_KERNEL_NOENC);
662 }
663
664 /*
665  * Remap memory without encryption and write-protected - cannot be called
666  * before pat_init() is called
667  */
668 void __init *early_memremap_decrypted_wp(resource_size_t phys_addr,
669                                          unsigned long size)
670 {
671         /* Be sure the write-protect PAT entry is set for write-protect */
672         if (__pte2cachemode_tbl[_PAGE_CACHE_MODE_WP] != _PAGE_CACHE_MODE_WP)
673                 return NULL;
674
675         return early_memremap_prot(phys_addr, size, __PAGE_KERNEL_NOENC_WP);
676 }
677 #endif  /* CONFIG_AMD_MEM_ENCRYPT */
678
679 static pte_t bm_pte[PAGE_SIZE/sizeof(pte_t)] __page_aligned_bss;
680
681 static inline pmd_t * __init early_ioremap_pmd(unsigned long addr)
682 {
683         /* Don't assume we're using swapper_pg_dir at this point */
684         pgd_t *base = __va(read_cr3_pa());
685         pgd_t *pgd = &base[pgd_index(addr)];
686         p4d_t *p4d = p4d_offset(pgd, addr);
687         pud_t *pud = pud_offset(p4d, addr);
688         pmd_t *pmd = pmd_offset(pud, addr);
689
690         return pmd;
691 }
692
693 static inline pte_t * __init early_ioremap_pte(unsigned long addr)
694 {
695         return &bm_pte[pte_index(addr)];
696 }
697
698 bool __init is_early_ioremap_ptep(pte_t *ptep)
699 {
700         return ptep >= &bm_pte[0] && ptep < &bm_pte[PAGE_SIZE/sizeof(pte_t)];
701 }
702
703 void __init early_ioremap_init(void)
704 {
705         pmd_t *pmd;
706
707 #ifdef CONFIG_X86_64
708         BUILD_BUG_ON((fix_to_virt(0) + PAGE_SIZE) & ((1 << PMD_SHIFT) - 1));
709 #else
710         WARN_ON((fix_to_virt(0) + PAGE_SIZE) & ((1 << PMD_SHIFT) - 1));
711 #endif
712
713         early_ioremap_setup();
714
715         pmd = early_ioremap_pmd(fix_to_virt(FIX_BTMAP_BEGIN));
716         memset(bm_pte, 0, sizeof(bm_pte));
717         pmd_populate_kernel(&init_mm, pmd, bm_pte);
718
719         /*
720          * The boot-ioremap range spans multiple pmds, for which
721          * we are not prepared:
722          */
723 #define __FIXADDR_TOP (-PAGE_SIZE)
724         BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
725                      != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
726 #undef __FIXADDR_TOP
727         if (pmd != early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END))) {
728                 WARN_ON(1);
729                 printk(KERN_WARNING "pmd %p != %p\n",
730                        pmd, early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END)));
731                 printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
732                         fix_to_virt(FIX_BTMAP_BEGIN));
733                 printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_END):   %08lx\n",
734                         fix_to_virt(FIX_BTMAP_END));
735
736                 printk(KERN_WARNING "FIX_BTMAP_END:       %d\n", FIX_BTMAP_END);
737                 printk(KERN_WARNING "FIX_BTMAP_BEGIN:     %d\n",
738                        FIX_BTMAP_BEGIN);
739         }
740 }
741
742 void __init __early_set_fixmap(enum fixed_addresses idx,
743                                phys_addr_t phys, pgprot_t flags)
744 {
745         unsigned long addr = __fix_to_virt(idx);
746         pte_t *pte;
747
748         if (idx >= __end_of_fixed_addresses) {
749                 BUG();
750                 return;
751         }
752         pte = early_ioremap_pte(addr);
753
754         if (pgprot_val(flags))
755                 set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
756         else
757                 pte_clear(&init_mm, addr, pte);
758         __flush_tlb_one_kernel(addr);
759 }