]>
Commit | Line | Data |
---|---|---|
6ee73861 BS |
1 | /* |
2 | * Copyright 1993-2003 NVIDIA, Corporation | |
3 | * Copyright 2007-2009 Stuart Bennett | |
4 | * | |
5 | * Permission is hereby granted, free of charge, to any person obtaining a | |
6 | * copy of this software and associated documentation files (the "Software"), | |
7 | * to deal in the Software without restriction, including without limitation | |
8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
9 | * and/or sell copies of the Software, and to permit persons to whom the | |
10 | * Software is furnished to do so, subject to the following conditions: | |
11 | * | |
12 | * The above copyright notice and this permission notice shall be included in | |
13 | * all copies or substantial portions of the 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 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
19 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF | |
20 | * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
21 | * SOFTWARE. | |
22 | */ | |
23 | ||
24 | #include "drmP.h" | |
77145f1c BS |
25 | #include "nouveau_drm.h" |
26 | #include "nouveau_reg.h" | |
6ee73861 BS |
27 | #include "nouveau_hw.h" |
28 | ||
29 | /****************************************************************************\ | |
30 | * * | |
31 | * The video arbitration routines calculate some "magic" numbers. Fixes * | |
32 | * the snow seen when accessing the framebuffer without it. * | |
33 | * It just works (I hope). * | |
34 | * * | |
35 | \****************************************************************************/ | |
36 | ||
37 | struct nv_fifo_info { | |
38 | int lwm; | |
39 | int burst; | |
40 | }; | |
41 | ||
42 | struct nv_sim_state { | |
43 | int pclk_khz; | |
44 | int mclk_khz; | |
45 | int nvclk_khz; | |
46 | int bpp; | |
47 | int mem_page_miss; | |
48 | int mem_latency; | |
49 | int memory_type; | |
50 | int memory_width; | |
51 | int two_heads; | |
52 | }; | |
53 | ||
54 | static void | |
55 | nv04_calc_arb(struct nv_fifo_info *fifo, struct nv_sim_state *arb) | |
56 | { | |
57 | int pagemiss, cas, width, bpp; | |
58 | int nvclks, mclks, pclks, crtpagemiss; | |
59 | int found, mclk_extra, mclk_loop, cbs, m1, p1; | |
60 | int mclk_freq, pclk_freq, nvclk_freq; | |
61 | int us_m, us_n, us_p, crtc_drain_rate; | |
62 | int cpm_us, us_crt, clwm; | |
63 | ||
64 | pclk_freq = arb->pclk_khz; | |
65 | mclk_freq = arb->mclk_khz; | |
66 | nvclk_freq = arb->nvclk_khz; | |
67 | pagemiss = arb->mem_page_miss; | |
68 | cas = arb->mem_latency; | |
69 | width = arb->memory_width >> 6; | |
70 | bpp = arb->bpp; | |
71 | cbs = 128; | |
72 | ||
73 | pclks = 2; | |
74 | nvclks = 10; | |
75 | mclks = 13 + cas; | |
76 | mclk_extra = 3; | |
77 | found = 0; | |
78 | ||
79 | while (!found) { | |
80 | found = 1; | |
81 | ||
82 | mclk_loop = mclks + mclk_extra; | |
83 | us_m = mclk_loop * 1000 * 1000 / mclk_freq; | |
84 | us_n = nvclks * 1000 * 1000 / nvclk_freq; | |
85 | us_p = nvclks * 1000 * 1000 / pclk_freq; | |
86 | ||
87 | crtc_drain_rate = pclk_freq * bpp / 8; | |
88 | crtpagemiss = 2; | |
89 | crtpagemiss += 1; | |
90 | cpm_us = crtpagemiss * pagemiss * 1000 * 1000 / mclk_freq; | |
91 | us_crt = cpm_us + us_m + us_n + us_p; | |
92 | clwm = us_crt * crtc_drain_rate / (1000 * 1000); | |
93 | clwm++; | |
94 | ||
95 | m1 = clwm + cbs - 512; | |
96 | p1 = m1 * pclk_freq / mclk_freq; | |
97 | p1 = p1 * bpp / 8; | |
98 | if ((p1 < m1 && m1 > 0) || clwm > 519) { | |
99 | found = !mclk_extra; | |
100 | mclk_extra--; | |
101 | } | |
102 | if (clwm < 384) | |
103 | clwm = 384; | |
104 | ||
105 | fifo->lwm = clwm; | |
106 | fifo->burst = cbs; | |
107 | } | |
108 | } | |
109 | ||
110 | static void | |
111 | nv10_calc_arb(struct nv_fifo_info *fifo, struct nv_sim_state *arb) | |
112 | { | |
113 | int fill_rate, drain_rate; | |
114 | int pclks, nvclks, mclks, xclks; | |
115 | int pclk_freq, nvclk_freq, mclk_freq; | |
116 | int fill_lat, extra_lat; | |
117 | int max_burst_o, max_burst_l; | |
118 | int fifo_len, min_lwm, max_lwm; | |
119 | const int burst_lat = 80; /* Maximum allowable latency due | |
120 | * to the CRTC FIFO burst. (ns) */ | |
121 | ||
122 | pclk_freq = arb->pclk_khz; | |
123 | nvclk_freq = arb->nvclk_khz; | |
124 | mclk_freq = arb->mclk_khz; | |
125 | ||
126 | fill_rate = mclk_freq * arb->memory_width / 8; /* kB/s */ | |
127 | drain_rate = pclk_freq * arb->bpp / 8; /* kB/s */ | |
128 | ||
129 | fifo_len = arb->two_heads ? 1536 : 1024; /* B */ | |
130 | ||
131 | /* Fixed FIFO refill latency. */ | |
132 | ||
133 | pclks = 4; /* lwm detect. */ | |
134 | ||
135 | nvclks = 3 /* lwm -> sync. */ | |
136 | + 2 /* fbi bus cycles (1 req + 1 busy) */ | |
137 | + 1 /* 2 edge sync. may be very close to edge so | |
138 | * just put one. */ | |
139 | + 1 /* fbi_d_rdv_n */ | |
140 | + 1 /* Fbi_d_rdata */ | |
141 | + 1; /* crtfifo load */ | |
142 | ||
143 | mclks = 1 /* 2 edge sync. may be very close to edge so | |
144 | * just put one. */ | |
145 | + 1 /* arb_hp_req */ | |
146 | + 5 /* tiling pipeline */ | |
147 | + 2 /* latency fifo */ | |
148 | + 2 /* memory request to fbio block */ | |
149 | + 7; /* data returned from fbio block */ | |
150 | ||
151 | /* Need to accumulate 256 bits for read */ | |
152 | mclks += (arb->memory_type == 0 ? 2 : 1) | |
153 | * arb->memory_width / 32; | |
154 | ||
155 | fill_lat = mclks * 1000 * 1000 / mclk_freq /* minimum mclk latency */ | |
156 | + nvclks * 1000 * 1000 / nvclk_freq /* nvclk latency */ | |
157 | + pclks * 1000 * 1000 / pclk_freq; /* pclk latency */ | |
158 | ||
159 | /* Conditional FIFO refill latency. */ | |
160 | ||
161 | xclks = 2 * arb->mem_page_miss + mclks /* Extra latency due to | |
162 | * the overlay. */ | |
163 | + 2 * arb->mem_page_miss /* Extra pagemiss latency. */ | |
164 | + (arb->bpp == 32 ? 8 : 4); /* Margin of error. */ | |
165 | ||
166 | extra_lat = xclks * 1000 * 1000 / mclk_freq; | |
167 | ||
168 | if (arb->two_heads) | |
169 | /* Account for another CRTC. */ | |
170 | extra_lat += fill_lat + extra_lat + burst_lat; | |
171 | ||
172 | /* FIFO burst */ | |
173 | ||
174 | /* Max burst not leading to overflows. */ | |
175 | max_burst_o = (1 + fifo_len - extra_lat * drain_rate / (1000 * 1000)) | |
176 | * (fill_rate / 1000) / ((fill_rate - drain_rate) / 1000); | |
177 | fifo->burst = min(max_burst_o, 1024); | |
178 | ||
179 | /* Max burst value with an acceptable latency. */ | |
180 | max_burst_l = burst_lat * fill_rate / (1000 * 1000); | |
181 | fifo->burst = min(max_burst_l, fifo->burst); | |
182 | ||
183 | fifo->burst = rounddown_pow_of_two(fifo->burst); | |
184 | ||
185 | /* FIFO low watermark */ | |
186 | ||
187 | min_lwm = (fill_lat + extra_lat) * drain_rate / (1000 * 1000) + 1; | |
188 | max_lwm = fifo_len - fifo->burst | |
189 | + fill_lat * drain_rate / (1000 * 1000) | |
190 | + fifo->burst * drain_rate / fill_rate; | |
191 | ||
192 | fifo->lwm = min_lwm + 10 * (max_lwm - min_lwm) / 100; /* Empirical. */ | |
193 | } | |
194 | ||
195 | static void | |
196 | nv04_update_arb(struct drm_device *dev, int VClk, int bpp, | |
197 | int *burst, int *lwm) | |
198 | { | |
77145f1c BS |
199 | struct nouveau_drm *drm = nouveau_drm(dev); |
200 | struct nouveau_device *device = nouveau_dev(dev); | |
6ee73861 BS |
201 | struct nv_fifo_info fifo_data; |
202 | struct nv_sim_state sim_data; | |
855a95e4 BS |
203 | int MClk = nouveau_hw_get_clock(dev, PLL_MEMORY); |
204 | int NVClk = nouveau_hw_get_clock(dev, PLL_CORE); | |
77145f1c | 205 | uint32_t cfg1 = nv_rd32(device, NV04_PFB_CFG1); |
6ee73861 BS |
206 | |
207 | sim_data.pclk_khz = VClk; | |
208 | sim_data.mclk_khz = MClk; | |
209 | sim_data.nvclk_khz = NVClk; | |
210 | sim_data.bpp = bpp; | |
211 | sim_data.two_heads = nv_two_heads(dev); | |
212 | if ((dev->pci_device & 0xffff) == 0x01a0 /*CHIPSET_NFORCE*/ || | |
213 | (dev->pci_device & 0xffff) == 0x01f0 /*CHIPSET_NFORCE2*/) { | |
214 | uint32_t type; | |
215 | ||
216 | pci_read_config_dword(pci_get_bus_and_slot(0, 1), 0x7c, &type); | |
217 | ||
218 | sim_data.memory_type = (type >> 12) & 1; | |
219 | sim_data.memory_width = 64; | |
220 | sim_data.mem_latency = 3; | |
221 | sim_data.mem_page_miss = 10; | |
222 | } else { | |
77145f1c BS |
223 | sim_data.memory_type = nv_rd32(device, NV04_PFB_CFG0) & 0x1; |
224 | sim_data.memory_width = (nv_rd32(device, NV_PEXTDEV_BOOT_0) & 0x10) ? 128 : 64; | |
6ee73861 BS |
225 | sim_data.mem_latency = cfg1 & 0xf; |
226 | sim_data.mem_page_miss = ((cfg1 >> 4) & 0xf) + ((cfg1 >> 31) & 0x1); | |
227 | } | |
228 | ||
77145f1c | 229 | if (nv_device(drm->device)->card_type == NV_04) |
6ee73861 BS |
230 | nv04_calc_arb(&fifo_data, &sim_data); |
231 | else | |
232 | nv10_calc_arb(&fifo_data, &sim_data); | |
233 | ||
234 | *burst = ilog2(fifo_data.burst >> 4); | |
235 | *lwm = fifo_data.lwm >> 3; | |
236 | } | |
237 | ||
238 | static void | |
4295f188 | 239 | nv20_update_arb(int *burst, int *lwm) |
6ee73861 BS |
240 | { |
241 | unsigned int fifo_size, burst_size, graphics_lwm; | |
242 | ||
243 | fifo_size = 2048; | |
244 | burst_size = 512; | |
245 | graphics_lwm = fifo_size - burst_size; | |
246 | ||
247 | *burst = ilog2(burst_size >> 5); | |
248 | *lwm = graphics_lwm >> 3; | |
249 | } | |
250 | ||
251 | void | |
252 | nouveau_calc_arb(struct drm_device *dev, int vclk, int bpp, int *burst, int *lwm) | |
253 | { | |
77145f1c | 254 | struct nouveau_drm *drm = nouveau_drm(dev); |
6ee73861 | 255 | |
77145f1c | 256 | if (nv_device(drm->device)->card_type < NV_20) |
6ee73861 BS |
257 | nv04_update_arb(dev, vclk, bpp, burst, lwm); |
258 | else if ((dev->pci_device & 0xfff0) == 0x0240 /*CHIPSET_C51*/ || | |
259 | (dev->pci_device & 0xfff0) == 0x03d0 /*CHIPSET_C512*/) { | |
260 | *burst = 128; | |
261 | *lwm = 0x0480; | |
262 | } else | |
4295f188 | 263 | nv20_update_arb(burst, lwm); |
6ee73861 | 264 | } |