ioremap.c 22 KB

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