ioremap.c 25 KB

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