pat.txt 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 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_nocache | -- | UC- | UC- |
  30. | | | |
  31. ioremap_wc | -- | -- | WC |
  32. | | | |
  33. set_memory_uc | UC- | -- | -- |
  34. set_memory_wb | | | |
  35. | | | |
  36. set_memory_wc | WC | -- | -- |
  37. set_memory_wb | | | |
  38. | | | |
  39. pci sysfs resource | -- | -- | UC- |
  40. | | | |
  41. pci sysfs resource_wc | -- | -- | WC |
  42. is IORESOURCE_PREFETCH| | | |
  43. | | | |
  44. pci proc | -- | -- | UC- |
  45. !PCIIOC_WRITE_COMBINE | | | |
  46. | | | |
  47. pci proc | -- | -- | WC |
  48. PCIIOC_WRITE_COMBINE | | | |
  49. | | | |
  50. /dev/mem | -- | WB/WC/UC- | WB/WC/UC- |
  51. read-write | | | |
  52. | | | |
  53. /dev/mem | -- | UC- | UC- |
  54. mmap SYNC flag | | | |
  55. | | | |
  56. /dev/mem | -- | WB/WC/UC- | WB/WC/UC- |
  57. mmap !SYNC flag | |(from exist-| (from exist- |
  58. and | | ing alias)| ing alias) |
  59. any alias to this area| | | |
  60. | | | |
  61. /dev/mem | -- | WB | WB |
  62. mmap !SYNC flag | | | |
  63. no alias to this area | | | |
  64. and | | | |
  65. MTRR says WB | | | |
  66. | | | |
  67. /dev/mem | -- | -- | UC- |
  68. mmap !SYNC flag | | | |
  69. no alias to this area | | | |
  70. and | | | |
  71. MTRR says !WB | | | |
  72. | | | |
  73. -------------------------------------------------------------------
  74. Advanced APIs for drivers
  75. -------------------------
  76. A. Exporting pages to users with remap_pfn_range, io_remap_pfn_range,
  77. vm_insert_pfn
  78. Drivers wanting to export some pages to userspace do it by using mmap
  79. interface and a combination of
  80. 1) pgprot_noncached()
  81. 2) io_remap_pfn_range() or remap_pfn_range() or vm_insert_pfn()
  82. With PAT support, a new API pgprot_writecombine is being added. So, drivers can
  83. continue to use the above sequence, with either pgprot_noncached() or
  84. pgprot_writecombine() in step 1, followed by step 2.
  85. In addition, step 2 internally tracks the region as UC or WC in memtype
  86. list in order to ensure no conflicting mapping.
  87. Note that this set of APIs only works with IO (non RAM) regions. If driver
  88. wants to export a RAM region, it has to do set_memory_uc() or set_memory_wc()
  89. as step 0 above and also track the usage of those pages and use set_memory_wb()
  90. before the page is freed to free pool.
  91. Notes:
  92. -- in the above table mean "Not suggested usage for the API". Some of the --'s
  93. are strictly enforced by the kernel. Some others are not really enforced
  94. today, but may be enforced in future.
  95. For ioremap and pci access through /sys or /proc - The actual type returned
  96. can be more restrictive, in case of any existing aliasing for that address.
  97. For example: If there is an existing uncached mapping, a new ioremap_wc can
  98. return uncached mapping in place of write-combine requested.
  99. set_memory_[uc|wc] and set_memory_wb should be used in pairs, where driver will
  100. first make a region uc or wc and switch it back to wb after use.
  101. Over time writes to /proc/mtrr will be deprecated in favor of using PAT based
  102. interfaces. Users writing to /proc/mtrr are suggested to use above interfaces.
  103. Drivers should use ioremap_[uc|wc] to access PCI BARs with [uc|wc] access
  104. types.
  105. Drivers should use set_memory_[uc|wc] to set access type for RAM ranges.
  106. PAT debugging
  107. -------------
  108. With CONFIG_DEBUG_FS enabled, PAT memtype list can be examined by
  109. # mount -t debugfs debugfs /sys/kernel/debug
  110. # cat /sys/kernel/debug/x86/pat_memtype_list
  111. PAT memtype list:
  112. uncached-minus @ 0x7fadf000-0x7fae0000
  113. uncached-minus @ 0x7fb19000-0x7fb1a000
  114. uncached-minus @ 0x7fb1a000-0x7fb1b000
  115. uncached-minus @ 0x7fb1b000-0x7fb1c000
  116. uncached-minus @ 0x7fb1c000-0x7fb1d000
  117. uncached-minus @ 0x7fb1d000-0x7fb1e000
  118. uncached-minus @ 0x7fb1e000-0x7fb25000
  119. uncached-minus @ 0x7fb25000-0x7fb26000
  120. uncached-minus @ 0x7fb26000-0x7fb27000
  121. uncached-minus @ 0x7fb27000-0x7fb28000
  122. uncached-minus @ 0x7fb28000-0x7fb2e000
  123. uncached-minus @ 0x7fb2e000-0x7fb2f000
  124. uncached-minus @ 0x7fb2f000-0x7fb30000
  125. uncached-minus @ 0x7fb31000-0x7fb32000
  126. uncached-minus @ 0x80000000-0x90000000
  127. This list shows physical address ranges and various PAT settings used to
  128. access those physical address ranges.
  129. Another, more verbose way of getting PAT related debug messages is with
  130. "debugpat" boot parameter. With this parameter, various debug messages are
  131. printed to dmesg log.