ioremap.c 22 KB

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