radeon: remove unused legacy state
[platform/upstream/libdrm.git] / linux-core / i915_gem_tiling.c
1 /*
2  * Copyright © 2008 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Eric Anholt <eric@anholt.net>
25  *
26  */
27
28 #include "drmP.h"
29 #include "drm.h"
30 #include "i915_drm.h"
31 #include "i915_drv.h"
32
33 /** @file i915_gem_tiling.c
34  *
35  * Support for managing tiling state of buffer objects.
36  *
37  * The idea behind tiling is to increase cache hit rates by rearranging
38  * pixel data so that a group of pixel accesses are in the same cacheline.
39  * Performance improvement from doing this on the back/depth buffer are on
40  * the order of 30%.
41  *
42  * Intel architectures make this somewhat more complicated, though, by
43  * adjustments made to addressing of data when the memory is in interleaved
44  * mode (matched pairs of DIMMS) to improve memory bandwidth.
45  * For interleaved memory, the CPU sends every sequential 64 bytes
46  * to an alternate memory channel so it can get the bandwidth from both.
47  *
48  * The GPU also rearranges its accesses for increased bandwidth to interleaved
49  * memory, and it matches what the CPU does for non-tiled.  However, when tiled
50  * it does it a little differently, since one walks addresses not just in the
51  * X direction but also Y.  So, along with alternating channels when bit
52  * 6 of the address flips, it also alternates when other bits flip --  Bits 9
53  * (every 512 bytes, an X tile scanline) and 10 (every two X tile scanlines)
54  * are common to both the 915 and 965-class hardware.
55  *
56  * The CPU also sometimes XORs in higher bits as well, to improve
57  * bandwidth doing strided access like we do so frequently in graphics.  This
58  * is called "Channel XOR Randomization" in the MCH documentation.  The result
59  * is that the CPU is XORing in either bit 11 or bit 17 to bit 6 of its address
60  * decode.
61  *
62  * All of this bit 6 XORing has an effect on our memory management,
63  * as we need to make sure that the 3d driver can correctly address object
64  * contents.
65  *
66  * If we don't have interleaved memory, all tiling is safe and no swizzling is
67  * required.
68  *
69  * When bit 17 is XORed in, we simply refuse to tile at all.  Bit
70  * 17 is not just a page offset, so as we page an objet out and back in,
71  * individual pages in it will have different bit 17 addresses, resulting in
72  * each 64 bytes being swapped with its neighbor!
73  *
74  * Otherwise, if interleaved, we have to tell the 3d driver what the address
75  * swizzling it needs to do is, since it's writing with the CPU to the pages
76  * (bit 6 and potentially bit 11 XORed in), and the GPU is reading from the
77  * pages (bit 6, 9, and 10 XORed in), resulting in a cumulative bit swizzling
78  * required by the CPU of XORing in bit 6, 9, 10, and potentially 11, in order
79  * to match what the GPU expects.
80  */
81
82 /**
83  * Detects bit 6 swizzling of address lookup between IGD access and CPU
84  * access through main memory.
85  */
86 void
87 i915_gem_detect_bit_6_swizzle(struct drm_device *dev)
88 {
89         struct drm_i915_private *dev_priv = dev->dev_private;
90         struct pci_dev *bridge;
91         uint32_t swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
92         uint32_t swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
93         int mchbar_offset;
94         char __iomem *mchbar;
95         int ret;
96
97         bridge = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
98         if (bridge == NULL) {
99                 DRM_ERROR("Couldn't get bridge device\n");
100                 return;
101         }
102
103         ret = pci_enable_device(bridge);
104         if (ret != 0) {
105                 DRM_ERROR("pci_enable_device failed: %d\n", ret);
106                 return;
107         }
108
109         if (IS_I965G(dev))
110                 mchbar_offset = 0x48;
111         else
112                 mchbar_offset = 0x44;
113
114         /* Use resource 2 for our BAR that's stashed in a nonstandard location,
115          * since the bridge would only ever use standard BARs 0-1 (though it
116          * doesn't anyway)
117          */
118         ret = pci_read_base(bridge, pci_bar_mem64, &bridge->resource[2],
119                             mchbar_offset);
120         if (ret != 0) {
121                 DRM_ERROR("pci_read_base failed: %d\n", ret);
122                 return;
123         }
124
125         mchbar = ioremap(pci_resource_start(bridge, 2),
126                          pci_resource_len(bridge, 2));
127         if (mchbar == NULL) {
128                 DRM_ERROR("Couldn't map MCHBAR to determine tile swizzling\n");
129                 return;
130         }
131
132         if (IS_I965G(dev) && !IS_I965GM(dev)) {
133                 uint32_t chdecmisc;
134
135                 /* On the 965, channel interleave appears to be determined by
136                  * the flex bit.  If flex is set, then the ranks (sides of a
137                  * DIMM) of memory will be "stacked" (physical addresses walk
138                  * through one rank then move on to the next, flipping channels
139                  * or not depending on rank configuration).  The GPU in this
140                  * case does exactly the same addressing as the CPU.
141                  *
142                  * Unlike the 945, channel randomization based does not
143                  * appear to be available.
144                  *
145                  * XXX: While the G965 doesn't appear to do any interleaving
146                  * when the DIMMs are not exactly matched, the G4x chipsets
147                  * might be for "L-shaped" configurations, and will need to be
148                  * detected.
149                  *
150                  * L-shaped configuration:
151                  *
152                  * +-----+
153                  * |     |
154                  * |DIMM2|         <-- non-interleaved
155                  * +-----+
156                  * +-----+ +-----+
157                  * |     | |     |
158                  * |DIMM0| |DIMM1| <-- interleaved area
159                  * +-----+ +-----+
160                  */
161                 chdecmisc = readb(mchbar + CHDECMISC);
162
163                 if (chdecmisc == 0xff) {
164                         DRM_ERROR("Couldn't read from MCHBAR.  "
165                                   "Disabling tiling.\n");
166                 } else if (chdecmisc & CHDECMISC_FLEXMEMORY) {
167                         swizzle_x = I915_BIT_6_SWIZZLE_NONE;
168                         swizzle_y = I915_BIT_6_SWIZZLE_NONE;
169                 } else {
170                         swizzle_x = I915_BIT_6_SWIZZLE_9_10;
171                         swizzle_y = I915_BIT_6_SWIZZLE_9;
172                 }
173         } else if (IS_I9XX(dev)) {
174                 uint32_t dcc;
175
176                 /* On 915-945 and GM965, channel interleave by the CPU is
177                  * determined by DCC.  The CPU will alternate based on bit 6
178                  * in interleaved mode, and the GPU will then also alternate
179                  * on bit 6, 9, and 10 for X, but the CPU may also optionally
180                  * alternate based on bit 17 (XOR not disabled and XOR
181                  * bit == 17).
182                  */
183                 dcc = readl(mchbar + DCC);
184                 switch (dcc & DCC_ADDRESSING_MODE_MASK) {
185                 case DCC_ADDRESSING_MODE_SINGLE_CHANNEL:
186                 case DCC_ADDRESSING_MODE_DUAL_CHANNEL_ASYMMETRIC:
187                         swizzle_x = I915_BIT_6_SWIZZLE_NONE;
188                         swizzle_y = I915_BIT_6_SWIZZLE_NONE;
189                         break;
190                 case DCC_ADDRESSING_MODE_DUAL_CHANNEL_INTERLEAVED:
191                         if (IS_I915G(dev) || IS_I915GM(dev) ||
192                             dcc & DCC_CHANNEL_XOR_DISABLE) {
193                                 swizzle_x = I915_BIT_6_SWIZZLE_9_10;
194                                 swizzle_y = I915_BIT_6_SWIZZLE_9;
195                         } else if (IS_I965GM(dev)) {
196                                 /* GM965 only does bit 11-based channel
197                                  * randomization
198                                  */
199                                 swizzle_x = I915_BIT_6_SWIZZLE_9_10_11;
200                                 swizzle_y = I915_BIT_6_SWIZZLE_9_11;
201                         } else {
202                                 /* Bit 17 or perhaps other swizzling */
203                                 swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
204                                 swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
205                         }
206                         break;
207                 }
208                 if (dcc == 0xffffffff) {
209                         DRM_ERROR("Couldn't read from MCHBAR.  "
210                                   "Disabling tiling.\n");
211                         swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
212                         swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
213                 }
214         } else {
215                 /* As far as we know, the 865 doesn't have these bit 6
216                  * swizzling issues.
217                  */
218                 swizzle_x = I915_BIT_6_SWIZZLE_NONE;
219                 swizzle_y = I915_BIT_6_SWIZZLE_NONE;
220         }
221
222         iounmap(mchbar);
223
224         dev_priv->mm.bit_6_swizzle_x = swizzle_x;
225         dev_priv->mm.bit_6_swizzle_y = swizzle_y;
226 }
227
228 /**
229  * Sets the tiling mode of an object, returning the required swizzling of
230  * bit 6 of addresses in the object.
231  */
232 int
233 i915_gem_set_tiling(struct drm_device *dev, void *data,
234                    struct drm_file *file_priv)
235 {
236         struct drm_i915_gem_set_tiling *args = data;
237         struct drm_i915_private *dev_priv = dev->dev_private;
238         struct drm_gem_object *obj;
239         struct drm_i915_gem_object *obj_priv;
240
241         obj = drm_gem_object_lookup(dev, file_priv, args->handle);
242         if (obj == NULL)
243                 return -EINVAL;
244         obj_priv = obj->driver_private;
245
246         mutex_lock(&dev->struct_mutex);
247
248         if (args->tiling_mode == I915_TILING_NONE) {
249                 obj_priv->tiling_mode = I915_TILING_NONE;
250                 args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
251         } else {
252                 if (args->tiling_mode == I915_TILING_X)
253                         args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x;
254                 else
255                         args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y;
256                 /* If we can't handle the swizzling, make it untiled. */
257                 if (args->swizzle_mode == I915_BIT_6_SWIZZLE_UNKNOWN) {
258                         args->tiling_mode = I915_TILING_NONE;
259                         args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
260                 }
261         }
262         obj_priv->tiling_mode = args->tiling_mode;
263
264         mutex_unlock(&dev->struct_mutex);
265
266         drm_gem_object_unreference(obj);
267
268         return 0;
269 }
270
271 /**
272  * Returns the current tiling mode and required bit 6 swizzling for the object.
273  */
274 int
275 i915_gem_get_tiling(struct drm_device *dev, void *data,
276                    struct drm_file *file_priv)
277 {
278         struct drm_i915_gem_get_tiling *args = data;
279         struct drm_i915_private *dev_priv = dev->dev_private;
280         struct drm_gem_object *obj;
281         struct drm_i915_gem_object *obj_priv;
282
283         obj = drm_gem_object_lookup(dev, file_priv, args->handle);
284         if (obj == NULL)
285                 return -EINVAL;
286         obj_priv = obj->driver_private;
287
288         mutex_lock(&dev->struct_mutex);
289
290         args->tiling_mode = obj_priv->tiling_mode;
291         switch (obj_priv->tiling_mode) {
292         case I915_TILING_X:
293                 args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x;
294                 break;
295         case I915_TILING_Y:
296                 args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y;
297                 break;
298         case I915_TILING_NONE:
299                 args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
300                 break;
301         default:
302                 DRM_ERROR("unknown tiling mode\n");
303         }
304
305         mutex_unlock(&dev->struct_mutex);
306
307         drm_gem_object_unreference(obj);
308
309         return 0;
310 }