11. PAT (Page Attribute Table)

x86 Page Attribute Table (PAT) allows for setting the memory attribute at thepage level granularity. PAT is complementary to the MTRR settings which allowsfor setting of memory types over physical address ranges. However, PAT ismore flexible than MTRR due to its capability to set attributes at page leveland also due to the fact that there are no hardware limitations on number ofsuch attribute settings allowed. Added flexibility comes with guidelines fornot having memory type aliasing for the same physical memory with multiplevirtual addresses.

PAT allows for different types of memory attributes. The most commonly usedones that will be supported at this time are:

WBWrite-back
UCUncached
WCWrite-combined
WTWrite-through
UC-Uncached Minus

11.1. PAT APIs

There are many different APIs in the kernel that allows setting of memoryattributes at the page level. In order to avoid aliasing, these interfacesshould be used thoughtfully. Below is a table of interfaces available,their intended usage and their memory attribute relationships. Internally,these APIs use a reserve_memtype()/free_memtype() interface on the physicaladdress range to avoid any aliasing.

APIRAMACPI,…Reserved/Holes
ioremapUC-UC-
ioremap_cacheWBWB
ioremap_ucUCUC
ioremap_wcWC
ioremap_wtWT
set_memory_uc,set_memory_wbUC-
set_memory_wc,set_memory_wbWC
set_memory_wt,set_memory_wbWT
pci sysfs resourceUC-
pci sysfs resource_wcis IORESOURCE_PREFETCHWC
pci proc!PCIIOC_WRITE_COMBINEUC-
pci procPCIIOC_WRITE_COMBINEWC
/dev/memread-writeWB/WC/UC-WB/WC/UC-
/dev/memmmap SYNC flagUC-UC-
/dev/memmmap !SYNC flagandany alias to this area
WB/WC/UC-

(from existingalias)

WB/WC/UC-

(from existingalias)

/dev/memmmap !SYNC flagno alias to this areaandMTRR says WBWBWB
/dev/memmmap !SYNC flagno alias to this areaandMTRR says !WBUC-

11.2. Advanced APIs for drivers

A. Exporting pages to users with remap_pfn_range, io_remap_pfn_range,vmf_insert_pfn.

Drivers wanting to export some pages to userspace do it by using mmapinterface and a combination of:

  1. pgprot_noncached()
  2. io_remap_pfn_range() orremap_pfn_range() orvmf_insert_pfn()

With PAT support, a new API pgprot_writecombine is being added. So, drivers cancontinue to use the above sequence, with either pgprot_noncached() orpgprot_writecombine() in step 1, followed by step 2.

In addition, step 2 internally tracks the region as UC or WC in memtypelist in order to ensure no conflicting mapping.

Note that this set of APIs only works with IO (non RAM) regions. If driverwants to export a RAM region, it has to do set_memory_uc() or set_memory_wc()as step 0 above and also track the usage of those pages and use set_memory_wb()before the page is freed to free pool.

11.3. MTRR effects on PAT / non-PAT systems

The following table provides the effects of using write-combining MTRRs whenusing ioremap*() calls on x86 for both non-PAT and PAT systems. Ideallymtrr_add() usage will be phased out in favor ofarch_phys_wc_add() which willbe a no-op on PAT enabled systems. The region over which aarch_phys_wc_add()is made, should already have been ioremapped with WC attributes or PAT entries,this can be done by using ioremap_wc() / set_memory_wc(). Devices whichcombine areas of IO memory desired to remain uncacheable with areas wherewrite-combining is desirable should consider use of ioremap_uc() followed byset_memory_wc() to white-list effective write-combined areas. Such use isnevertheless discouraged as the effective memory type is consideredimplementation defined, yet this strategy can be used as last resort on deviceswith size-constrained regions where otherwise MTRR write-combining wouldotherwise not be effective.

====  =======  ===  =========================  =====================MTRR  Non-PAT  PAT  Linux ioremap value        Effective memory type====  =======  ===  =========================  =====================      PAT                                        Non-PAT |  PAT      |PCD                                               |      ||PWT                                              |      |||                                                |WC    000      WB   _PAGE_CACHE_MODE_WB             WC   |   WCWC    001      WC   _PAGE_CACHE_MODE_WC             WC*  |   WCWC    010      UC-  _PAGE_CACHE_MODE_UC_MINUS       WC*  |   UCWC    011      UC   _PAGE_CACHE_MODE_UC             UC   |   UC====  =======  ===  =========================  =====================(*) denotes implementation defined and is discouraged

Note

– in the above table mean “Not suggested usage for the API”. Someof the –’s are strictly enforced by the kernel. Some others are not reallyenforced today, but may be enforced in future.

For ioremap and pci access through /sys or /proc - The actual type returnedcan be more restrictive, in case of any existing aliasing for that address.For example: If there is an existing uncached mapping, a new ioremap_wc canreturn uncached mapping in place of write-combine requested.

set_memory_[uc|wc|wt] and set_memory_wb should be used in pairs, where driverwill first make a region uc, wc or wt and switch it back to wb after use.

Over time writes to /proc/mtrr will be deprecated in favor of using PAT basedinterfaces. Users writing to /proc/mtrr are suggested to use above interfaces.

Drivers should use ioremap_[uc|wc] to access PCI BARs with [uc|wc] accesstypes.

Drivers should use set_memory_[uc|wc|wt] to set access type for RAM ranges.

11.4. PAT debugging

With CONFIG_DEBUG_FS enabled, PAT memtype list can be examined by:

# mount -t debugfs debugfs /sys/kernel/debug# cat /sys/kernel/debug/x86/pat_memtype_listPAT memtype list:uncached-minus @ 0x7fadf000-0x7fae0000uncached-minus @ 0x7fb19000-0x7fb1a000uncached-minus @ 0x7fb1a000-0x7fb1b000uncached-minus @ 0x7fb1b000-0x7fb1c000uncached-minus @ 0x7fb1c000-0x7fb1d000uncached-minus @ 0x7fb1d000-0x7fb1e000uncached-minus @ 0x7fb1e000-0x7fb25000uncached-minus @ 0x7fb25000-0x7fb26000uncached-minus @ 0x7fb26000-0x7fb27000uncached-minus @ 0x7fb27000-0x7fb28000uncached-minus @ 0x7fb28000-0x7fb2e000uncached-minus @ 0x7fb2e000-0x7fb2f000uncached-minus @ 0x7fb2f000-0x7fb30000uncached-minus @ 0x7fb31000-0x7fb32000uncached-minus @ 0x80000000-0x90000000

This list shows physical address ranges and various PAT settings used toaccess those physical address ranges.

Another, more verbose way of getting PAT related debug messages is with“debugpat” boot parameter. With this parameter, various debug messages areprinted to dmesg log.

11.5. PAT Initialization

The following table describes how PAT is initialized under variousconfigurations. The PAT MSR must be updated by Linux in order to support WCand WT attributes. Otherwise, the PAT MSR has the value programmed in itby the firmware. Note, Xen enables WC attribute in the PAT MSR for guests.

MTRRPATCall SequencePAT StatePAT MSR
EEMTRR -> PAT initEnabledOS
EDMTRR -> PAT initDisabled
DEMTRR -> PAT disableDisabledBIOS
DDMTRR -> PAT disableDisabled
np/EPAT -> PAT disableDisabledBIOS
np/DPAT -> PAT disableDisabled
E!P/EMTRR -> PAT initDisabledBIOS
D!P/EMTRR -> PAT disableDisabledBIOS
!M!P/EMTRR stub -> PAT disableDisabledBIOS
Legend
EFeature enabled in CPU
DFeature disabled/unsupported in CPU
np“nopat” boot option specified
!PCONFIG_X86_PAT option unset
!MCONFIG_MTRR option unset
EnabledPAT state set to enabled
DisabledPAT state set to disabled
OSPAT initializes PAT MSR with OS setting
BIOSPAT keeps PAT MSR with BIOS setting