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