pat.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. PAT (Page Attribute Table)
  2. x86 Page Attribute Table (PAT) allows for setting the memory attribute at the
  3. page level granularity. PAT is complementary to the MTRR settings which allows
  4. for setting of memory types over physical address ranges. However, PAT is
  5. more flexible than MTRR due to its capability to set attributes at page level
  6. and also due to the fact that there are no hardware limitations on number of
  7. such attribute settings allowed. Added flexibility comes with guidelines for
  8. not having memory type aliasing for the same physical memory with multiple
  9. virtual addresses.
  10. PAT allows for different types of memory attributes. The most commonly used
  11. ones that will be supported at this time are Write-back, Uncached,
  12. Write-combined, Write-through and Uncached Minus.
  13. PAT APIs
  14. --------
  15. There are many different APIs in the kernel that allows setting of memory
  16. attributes at the page level. In order to avoid aliasing, these interfaces
  17. should be used thoughtfully. Below is a table of interfaces available,
  18. their intended usage and their memory attribute relationships. Internally,
  19. these APIs use a reserve_memtype()/free_memtype() interface on the physical
  20. address range to avoid any aliasing.
  21. -------------------------------------------------------------------
  22. API | RAM | ACPI,... | Reserved/Holes |
  23. -----------------------|----------|------------|------------------|
  24. | | | |
  25. ioremap | -- | UC- | UC- |
  26. | | | |
  27. ioremap_cache | -- | WB | WB |
  28. | | | |
  29. ioremap_uc | -- | UC | UC |
  30. | | | |
  31. ioremap_nocache | -- | UC- | UC- |
  32. | | | |
  33. ioremap_wc | -- | -- | WC |
  34. | | | |
  35. ioremap_wt | -- | -- | WT |
  36. | | | |
  37. set_memory_uc | UC- | -- | -- |
  38. set_memory_wb | | | |
  39. | | | |
  40. set_memory_wc | WC | -- | -- |
  41. set_memory_wb | | | |
  42. | | | |
  43. set_memory_wt | WT | -- | -- |
  44. set_memory_wb | | | |
  45. | | | |
  46. pci sysfs resource | -- | -- | UC- |
  47. | | | |
  48. pci sysfs resource_wc | -- | -- | WC |
  49. is IORESOURCE_PREFETCH| | | |
  50. | | | |
  51. pci proc | -- | -- | UC- |
  52. !PCIIOC_WRITE_COMBINE | | | |
  53. | | | |
  54. pci proc | -- | -- | WC |
  55. PCIIOC_WRITE_COMBINE | | | |
  56. | | | |
  57. /dev/mem | -- | WB/WC/UC- | WB/WC/UC- |
  58. read-write | | | |
  59. | | | |
  60. /dev/mem | -- | UC- | UC- |
  61. mmap SYNC flag | | | |
  62. | | | |
  63. /dev/mem | -- | WB/WC/UC- | WB/WC/UC- |
  64. mmap !SYNC flag | |(from exist-| (from exist- |
  65. and | | ing alias)| ing alias) |
  66. any alias to this area| | | |
  67. | | | |
  68. /dev/mem | -- | WB | WB |
  69. mmap !SYNC flag | | | |
  70. no alias to this area | | | |
  71. and | | | |
  72. MTRR says WB | | | |
  73. | | | |
  74. /dev/mem | -- | -- | UC- |
  75. mmap !SYNC flag | | | |
  76. no alias to this area | | | |
  77. and | | | |
  78. MTRR says !WB | | | |
  79. | | | |
  80. -------------------------------------------------------------------
  81. Advanced APIs for drivers
  82. -------------------------
  83. A. Exporting pages to users with remap_pfn_range, io_remap_pfn_range,
  84. vm_insert_pfn
  85. Drivers wanting to export some pages to userspace do it by using mmap
  86. interface and a combination of
  87. 1) pgprot_noncached()
  88. 2) io_remap_pfn_range() or remap_pfn_range() or vm_insert_pfn()
  89. With PAT support, a new API pgprot_writecombine is being added. So, drivers can
  90. continue to use the above sequence, with either pgprot_noncached() or
  91. pgprot_writecombine() in step 1, followed by step 2.
  92. In addition, step 2 internally tracks the region as UC or WC in memtype
  93. list in order to ensure no conflicting mapping.
  94. Note that this set of APIs only works with IO (non RAM) regions. If driver
  95. wants to export a RAM region, it has to do set_memory_uc() or set_memory_wc()
  96. as step 0 above and also track the usage of those pages and use set_memory_wb()
  97. before the page is freed to free pool.
  98. MTRR effects on PAT / non-PAT systems
  99. -------------------------------------
  100. The following table provides the effects of using write-combining MTRRs when
  101. using ioremap*() calls on x86 for both non-PAT and PAT systems. Ideally
  102. mtrr_add() usage will be phased out in favor of arch_phys_wc_add() which will
  103. be a no-op on PAT enabled systems. The region over which a arch_phys_wc_add()
  104. is made, should already have been ioremapped with WC attributes or PAT entries,
  105. this can be done by using ioremap_wc() / set_memory_wc(). Devices which
  106. combine areas of IO memory desired to remain uncacheable with areas where
  107. write-combining is desirable should consider use of ioremap_uc() followed by
  108. set_memory_wc() to white-list effective write-combined areas. Such use is
  109. nevertheless discouraged as the effective memory type is considered
  110. implementation defined, yet this strategy can be used as last resort on devices
  111. with size-constrained regions where otherwise MTRR write-combining would
  112. otherwise not be effective.
  113. ----------------------------------------------------------------------
  114. MTRR Non-PAT PAT Linux ioremap value Effective memory type
  115. ----------------------------------------------------------------------
  116. Non-PAT | PAT
  117. PAT
  118. |PCD
  119. ||PWT
  120. |||
  121. WC 000 WB _PAGE_CACHE_MODE_WB WC | WC
  122. WC 001 WC _PAGE_CACHE_MODE_WC WC* | WC
  123. WC 010 UC- _PAGE_CACHE_MODE_UC_MINUS WC* | UC
  124. WC 011 UC _PAGE_CACHE_MODE_UC UC | UC
  125. ----------------------------------------------------------------------
  126. (*) denotes implementation defined and is discouraged
  127. Notes:
  128. -- in the above table mean "Not suggested usage for the API". Some of the --'s
  129. are strictly enforced by the kernel. Some others are not really enforced
  130. today, but may be enforced in future.
  131. For ioremap and pci access through /sys or /proc - The actual type returned
  132. can be more restrictive, in case of any existing aliasing for that address.
  133. For example: If there is an existing uncached mapping, a new ioremap_wc can
  134. return uncached mapping in place of write-combine requested.
  135. set_memory_[uc|wc|wt] and set_memory_wb should be used in pairs, where driver
  136. will first make a region uc, wc or wt and switch it back to wb after use.
  137. Over time writes to /proc/mtrr will be deprecated in favor of using PAT based
  138. interfaces. Users writing to /proc/mtrr are suggested to use above interfaces.
  139. Drivers should use ioremap_[uc|wc] to access PCI BARs with [uc|wc] access
  140. types.
  141. Drivers should use set_memory_[uc|wc|wt] to set access type for RAM ranges.
  142. PAT debugging
  143. -------------
  144. With CONFIG_DEBUG_FS enabled, PAT memtype list can be examined by
  145. # mount -t debugfs debugfs /sys/kernel/debug
  146. # cat /sys/kernel/debug/x86/pat_memtype_list
  147. PAT memtype list:
  148. uncached-minus @ 0x7fadf000-0x7fae0000
  149. uncached-minus @ 0x7fb19000-0x7fb1a000
  150. uncached-minus @ 0x7fb1a000-0x7fb1b000
  151. uncached-minus @ 0x7fb1b000-0x7fb1c000
  152. uncached-minus @ 0x7fb1c000-0x7fb1d000
  153. uncached-minus @ 0x7fb1d000-0x7fb1e000
  154. uncached-minus @ 0x7fb1e000-0x7fb25000
  155. uncached-minus @ 0x7fb25000-0x7fb26000
  156. uncached-minus @ 0x7fb26000-0x7fb27000
  157. uncached-minus @ 0x7fb27000-0x7fb28000
  158. uncached-minus @ 0x7fb28000-0x7fb2e000
  159. uncached-minus @ 0x7fb2e000-0x7fb2f000
  160. uncached-minus @ 0x7fb2f000-0x7fb30000
  161. uncached-minus @ 0x7fb31000-0x7fb32000
  162. uncached-minus @ 0x80000000-0x90000000
  163. This list shows physical address ranges and various PAT settings used to
  164. access those physical address ranges.
  165. Another, more verbose way of getting PAT related debug messages is with
  166. "debugpat" boot parameter. With this parameter, various debug messages are
  167. printed to dmesg log.
  168. PAT Initialization
  169. ------------------
  170. The following table describes how PAT is initialized under various
  171. configurations. The PAT MSR must be updated by Linux in order to support WC
  172. and WT attributes. Otherwise, the PAT MSR has the value programmed in it
  173. by the firmware. Note, Xen enables WC attribute in the PAT MSR for guests.
  174. MTRR PAT Call Sequence PAT State PAT MSR
  175. =========================================================
  176. E E MTRR -> PAT init Enabled OS
  177. E D MTRR -> PAT init Disabled -
  178. D E MTRR -> PAT disable Disabled BIOS
  179. D D MTRR -> PAT disable Disabled -
  180. - np/E PAT -> PAT disable Disabled BIOS
  181. - np/D PAT -> PAT disable Disabled -
  182. E !P/E MTRR -> PAT init Disabled BIOS
  183. D !P/E MTRR -> PAT disable Disabled BIOS
  184. !M !P/E MTRR stub -> PAT disable Disabled BIOS
  185. Legend
  186. ------------------------------------------------
  187. E Feature enabled in CPU
  188. D Feature disabled/unsupported in CPU
  189. np "nopat" boot option specified
  190. !P CONFIG_X86_PAT option unset
  191. !M CONFIG_MTRR option unset
  192. Enabled PAT state set to enabled
  193. Disabled PAT state set to disabled
  194. OS PAT initializes PAT MSR with OS setting
  195. BIOS PAT keeps PAT MSR with BIOS setting