11a646342SBen Skeggs /*
21a646342SBen Skeggs  * Copyright 1993-2003 NVIDIA, Corporation
31a646342SBen Skeggs  * Copyright 2007-2009 Stuart Bennett
41a646342SBen Skeggs  *
51a646342SBen Skeggs  * Permission is hereby granted, free of charge, to any person obtaining a
61a646342SBen Skeggs  * copy of this software and associated documentation files (the "Software"),
71a646342SBen Skeggs  * to deal in the Software without restriction, including without limitation
81a646342SBen Skeggs  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
91a646342SBen Skeggs  * and/or sell copies of the Software, and to permit persons to whom the
101a646342SBen Skeggs  * Software is furnished to do so, subject to the following conditions:
111a646342SBen Skeggs  *
121a646342SBen Skeggs  * The above copyright notice and this permission notice shall be included in
131a646342SBen Skeggs  * all copies or substantial portions of the Software.
141a646342SBen Skeggs  *
151a646342SBen Skeggs  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
161a646342SBen Skeggs  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
171a646342SBen Skeggs  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
181a646342SBen Skeggs  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
191a646342SBen Skeggs  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
201a646342SBen Skeggs  * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
211a646342SBen Skeggs  * SOFTWARE.
221a646342SBen Skeggs  */
231a646342SBen Skeggs 
244dc28134SBen Skeggs #include "nouveau_drv.h"
251a646342SBen Skeggs #include "nouveau_reg.h"
261a646342SBen Skeggs #include "hw.h"
271a646342SBen Skeggs 
281a646342SBen Skeggs /****************************************************************************\
291a646342SBen Skeggs *                                                                            *
301a646342SBen Skeggs * The video arbitration routines calculate some "magic" numbers.  Fixes      *
311a646342SBen Skeggs * the snow seen when accessing the framebuffer without it.                   *
321a646342SBen Skeggs * It just works (I hope).                                                    *
331a646342SBen Skeggs *                                                                            *
341a646342SBen Skeggs \****************************************************************************/
351a646342SBen Skeggs 
361a646342SBen Skeggs struct nv_fifo_info {
371a646342SBen Skeggs 	int lwm;
381a646342SBen Skeggs 	int burst;
391a646342SBen Skeggs };
401a646342SBen Skeggs 
411a646342SBen Skeggs struct nv_sim_state {
421a646342SBen Skeggs 	int pclk_khz;
431a646342SBen Skeggs 	int mclk_khz;
441a646342SBen Skeggs 	int nvclk_khz;
451a646342SBen Skeggs 	int bpp;
461a646342SBen Skeggs 	int mem_page_miss;
471a646342SBen Skeggs 	int mem_latency;
481a646342SBen Skeggs 	int memory_type;
491a646342SBen Skeggs 	int memory_width;
501a646342SBen Skeggs 	int two_heads;
511a646342SBen Skeggs };
521a646342SBen Skeggs 
531a646342SBen Skeggs static void
nv04_calc_arb(struct nv_fifo_info * fifo,struct nv_sim_state * arb)541a646342SBen Skeggs nv04_calc_arb(struct nv_fifo_info *fifo, struct nv_sim_state *arb)
551a646342SBen Skeggs {
5667d52f0fSYueHaibing 	int pagemiss, cas, bpp;
57cd04e4ffSWambui Karuga 	int nvclks, mclks, crtpagemiss;
581a646342SBen Skeggs 	int found, mclk_extra, mclk_loop, cbs, m1, p1;
591a646342SBen Skeggs 	int mclk_freq, pclk_freq, nvclk_freq;
601a646342SBen Skeggs 	int us_m, us_n, us_p, crtc_drain_rate;
611a646342SBen Skeggs 	int cpm_us, us_crt, clwm;
621a646342SBen Skeggs 
631a646342SBen Skeggs 	pclk_freq = arb->pclk_khz;
641a646342SBen Skeggs 	mclk_freq = arb->mclk_khz;
651a646342SBen Skeggs 	nvclk_freq = arb->nvclk_khz;
661a646342SBen Skeggs 	pagemiss = arb->mem_page_miss;
671a646342SBen Skeggs 	cas = arb->mem_latency;
681a646342SBen Skeggs 	bpp = arb->bpp;
691a646342SBen Skeggs 	cbs = 128;
701a646342SBen Skeggs 
711a646342SBen Skeggs 	nvclks = 10;
721a646342SBen Skeggs 	mclks = 13 + cas;
731a646342SBen Skeggs 	mclk_extra = 3;
741a646342SBen Skeggs 	found = 0;
751a646342SBen Skeggs 
761a646342SBen Skeggs 	while (!found) {
771a646342SBen Skeggs 		found = 1;
781a646342SBen Skeggs 
791a646342SBen Skeggs 		mclk_loop = mclks + mclk_extra;
801a646342SBen Skeggs 		us_m = mclk_loop * 1000 * 1000 / mclk_freq;
811a646342SBen Skeggs 		us_n = nvclks * 1000 * 1000 / nvclk_freq;
821a646342SBen Skeggs 		us_p = nvclks * 1000 * 1000 / pclk_freq;
831a646342SBen Skeggs 
841a646342SBen Skeggs 		crtc_drain_rate = pclk_freq * bpp / 8;
851a646342SBen Skeggs 		crtpagemiss = 2;
861a646342SBen Skeggs 		crtpagemiss += 1;
871a646342SBen Skeggs 		cpm_us = crtpagemiss * pagemiss * 1000 * 1000 / mclk_freq;
881a646342SBen Skeggs 		us_crt = cpm_us + us_m + us_n + us_p;
891a646342SBen Skeggs 		clwm = us_crt * crtc_drain_rate / (1000 * 1000);
901a646342SBen Skeggs 		clwm++;
911a646342SBen Skeggs 
921a646342SBen Skeggs 		m1 = clwm + cbs - 512;
931a646342SBen Skeggs 		p1 = m1 * pclk_freq / mclk_freq;
941a646342SBen Skeggs 		p1 = p1 * bpp / 8;
951a646342SBen Skeggs 		if ((p1 < m1 && m1 > 0) || clwm > 519) {
961a646342SBen Skeggs 			found = !mclk_extra;
971a646342SBen Skeggs 			mclk_extra--;
981a646342SBen Skeggs 		}
991a646342SBen Skeggs 		if (clwm < 384)
1001a646342SBen Skeggs 			clwm = 384;
1011a646342SBen Skeggs 
1021a646342SBen Skeggs 		fifo->lwm = clwm;
1031a646342SBen Skeggs 		fifo->burst = cbs;
1041a646342SBen Skeggs 	}
1051a646342SBen Skeggs }
1061a646342SBen Skeggs 
1071a646342SBen Skeggs static void
nv10_calc_arb(struct nv_fifo_info * fifo,struct nv_sim_state * arb)1081a646342SBen Skeggs nv10_calc_arb(struct nv_fifo_info *fifo, struct nv_sim_state *arb)
1091a646342SBen Skeggs {
1101a646342SBen Skeggs 	int fill_rate, drain_rate;
1111a646342SBen Skeggs 	int pclks, nvclks, mclks, xclks;
1121a646342SBen Skeggs 	int pclk_freq, nvclk_freq, mclk_freq;
1131a646342SBen Skeggs 	int fill_lat, extra_lat;
1141a646342SBen Skeggs 	int max_burst_o, max_burst_l;
1151a646342SBen Skeggs 	int fifo_len, min_lwm, max_lwm;
1161a646342SBen Skeggs 	const int burst_lat = 80; /* Maximum allowable latency due
1171a646342SBen Skeggs 				   * to the CRTC FIFO burst. (ns) */
1181a646342SBen Skeggs 
1191a646342SBen Skeggs 	pclk_freq = arb->pclk_khz;
1201a646342SBen Skeggs 	nvclk_freq = arb->nvclk_khz;
1211a646342SBen Skeggs 	mclk_freq = arb->mclk_khz;
1221a646342SBen Skeggs 
1231a646342SBen Skeggs 	fill_rate = mclk_freq * arb->memory_width / 8; /* kB/s */
1241a646342SBen Skeggs 	drain_rate = pclk_freq * arb->bpp / 8; /* kB/s */
1251a646342SBen Skeggs 
1261a646342SBen Skeggs 	fifo_len = arb->two_heads ? 1536 : 1024; /* B */
1271a646342SBen Skeggs 
1281a646342SBen Skeggs 	/* Fixed FIFO refill latency. */
1291a646342SBen Skeggs 
1301a646342SBen Skeggs 	pclks = 4;	/* lwm detect. */
1311a646342SBen Skeggs 
1321a646342SBen Skeggs 	nvclks = 3	/* lwm -> sync. */
1331a646342SBen Skeggs 		+ 2	/* fbi bus cycles (1 req + 1 busy) */
1341a646342SBen Skeggs 		+ 1	/* 2 edge sync.  may be very close to edge so
1351a646342SBen Skeggs 			 * just put one. */
1361a646342SBen Skeggs 		+ 1	/* fbi_d_rdv_n */
1371a646342SBen Skeggs 		+ 1	/* Fbi_d_rdata */
1381a646342SBen Skeggs 		+ 1;	/* crtfifo load */
1391a646342SBen Skeggs 
1401a646342SBen Skeggs 	mclks = 1	/* 2 edge sync.  may be very close to edge so
1411a646342SBen Skeggs 			 * just put one. */
1421a646342SBen Skeggs 		+ 1	/* arb_hp_req */
1431a646342SBen Skeggs 		+ 5	/* tiling pipeline */
1441a646342SBen Skeggs 		+ 2	/* latency fifo */
1451a646342SBen Skeggs 		+ 2	/* memory request to fbio block */
1461a646342SBen Skeggs 		+ 7;	/* data returned from fbio block */
1471a646342SBen Skeggs 
1481a646342SBen Skeggs 	/* Need to accumulate 256 bits for read */
1491a646342SBen Skeggs 	mclks += (arb->memory_type == 0 ? 2 : 1)
1501a646342SBen Skeggs 		* arb->memory_width / 32;
1511a646342SBen Skeggs 
1521a646342SBen Skeggs 	fill_lat = mclks * 1000 * 1000 / mclk_freq   /* minimum mclk latency */
1531a646342SBen Skeggs 		+ nvclks * 1000 * 1000 / nvclk_freq  /* nvclk latency */
1541a646342SBen Skeggs 		+ pclks * 1000 * 1000 / pclk_freq;   /* pclk latency */
1551a646342SBen Skeggs 
1561a646342SBen Skeggs 	/* Conditional FIFO refill latency. */
1571a646342SBen Skeggs 
1581a646342SBen Skeggs 	xclks = 2 * arb->mem_page_miss + mclks /* Extra latency due to
1591a646342SBen Skeggs 						* the overlay. */
1601a646342SBen Skeggs 		+ 2 * arb->mem_page_miss       /* Extra pagemiss latency. */
1611a646342SBen Skeggs 		+ (arb->bpp == 32 ? 8 : 4);    /* Margin of error. */
1621a646342SBen Skeggs 
1631a646342SBen Skeggs 	extra_lat = xclks * 1000 * 1000 / mclk_freq;
1641a646342SBen Skeggs 
1651a646342SBen Skeggs 	if (arb->two_heads)
1661a646342SBen Skeggs 		/* Account for another CRTC. */
1671a646342SBen Skeggs 		extra_lat += fill_lat + extra_lat + burst_lat;
1681a646342SBen Skeggs 
1691a646342SBen Skeggs 	/* FIFO burst */
1701a646342SBen Skeggs 
1711a646342SBen Skeggs 	/* Max burst not leading to overflows. */
1721a646342SBen Skeggs 	max_burst_o = (1 + fifo_len - extra_lat * drain_rate / (1000 * 1000))
1731a646342SBen Skeggs 		* (fill_rate / 1000) / ((fill_rate - drain_rate) / 1000);
1741a646342SBen Skeggs 	fifo->burst = min(max_burst_o, 1024);
1751a646342SBen Skeggs 
1761a646342SBen Skeggs 	/* Max burst value with an acceptable latency. */
1771a646342SBen Skeggs 	max_burst_l = burst_lat * fill_rate / (1000 * 1000);
1781a646342SBen Skeggs 	fifo->burst = min(max_burst_l, fifo->burst);
1791a646342SBen Skeggs 
1801a646342SBen Skeggs 	fifo->burst = rounddown_pow_of_two(fifo->burst);
1811a646342SBen Skeggs 
1821a646342SBen Skeggs 	/* FIFO low watermark */
1831a646342SBen Skeggs 
1841a646342SBen Skeggs 	min_lwm = (fill_lat + extra_lat) * drain_rate / (1000 * 1000) + 1;
1851a646342SBen Skeggs 	max_lwm = fifo_len - fifo->burst
1861a646342SBen Skeggs 		+ fill_lat * drain_rate / (1000 * 1000)
1871a646342SBen Skeggs 		+ fifo->burst * drain_rate / fill_rate;
1881a646342SBen Skeggs 
1891a646342SBen Skeggs 	fifo->lwm = min_lwm + 10 * (max_lwm - min_lwm) / 100; /* Empirical. */
1901a646342SBen Skeggs }
1911a646342SBen Skeggs 
1921a646342SBen Skeggs static void
nv04_update_arb(struct drm_device * dev,int VClk,int bpp,int * burst,int * lwm)1931a646342SBen Skeggs nv04_update_arb(struct drm_device *dev, int VClk, int bpp,
1941a646342SBen Skeggs 		int *burst, int *lwm)
1951a646342SBen Skeggs {
1961a646342SBen Skeggs 	struct nouveau_drm *drm = nouveau_drm(dev);
1971167c6bcSBen Skeggs 	struct nvif_object *device = &nouveau_drm(dev)->client.device.object;
1981a646342SBen Skeggs 	struct nv_fifo_info fifo_data;
1991a646342SBen Skeggs 	struct nv_sim_state sim_data;
2001a646342SBen Skeggs 	int MClk = nouveau_hw_get_clock(dev, PLL_MEMORY);
2011a646342SBen Skeggs 	int NVClk = nouveau_hw_get_clock(dev, PLL_CORE);
202db2bec18SBen Skeggs 	uint32_t cfg1 = nvif_rd32(device, NV04_PFB_CFG1);
203*4c0d42f7SThomas Zimmermann 	struct pci_dev *pdev = to_pci_dev(dev->dev);
2041a646342SBen Skeggs 
2051a646342SBen Skeggs 	sim_data.pclk_khz = VClk;
2061a646342SBen Skeggs 	sim_data.mclk_khz = MClk;
2071a646342SBen Skeggs 	sim_data.nvclk_khz = NVClk;
2081a646342SBen Skeggs 	sim_data.bpp = bpp;
2091a646342SBen Skeggs 	sim_data.two_heads = nv_two_heads(dev);
210*4c0d42f7SThomas Zimmermann 	if ((pdev->device & 0xffff) == 0x01a0 /*CHIPSET_NFORCE*/ ||
211*4c0d42f7SThomas Zimmermann 	    (pdev->device & 0xffff) == 0x01f0 /*CHIPSET_NFORCE2*/) {
2121a646342SBen Skeggs 		uint32_t type;
213*4c0d42f7SThomas Zimmermann 		int domain = pci_domain_nr(pdev->bus);
2141a646342SBen Skeggs 
2152fa6d6cdSSinan Kaya 		pci_read_config_dword(pci_get_domain_bus_and_slot(domain, 0, 1),
2162fa6d6cdSSinan Kaya 				      0x7c, &type);
2171a646342SBen Skeggs 
2181a646342SBen Skeggs 		sim_data.memory_type = (type >> 12) & 1;
2191a646342SBen Skeggs 		sim_data.memory_width = 64;
2201a646342SBen Skeggs 		sim_data.mem_latency = 3;
2211a646342SBen Skeggs 		sim_data.mem_page_miss = 10;
2221a646342SBen Skeggs 	} else {
223db2bec18SBen Skeggs 		sim_data.memory_type = nvif_rd32(device, NV04_PFB_CFG0) & 0x1;
224db2bec18SBen Skeggs 		sim_data.memory_width = (nvif_rd32(device, NV_PEXTDEV_BOOT_0) & 0x10) ? 128 : 64;
2251a646342SBen Skeggs 		sim_data.mem_latency = cfg1 & 0xf;
2261a646342SBen Skeggs 		sim_data.mem_page_miss = ((cfg1 >> 4) & 0xf) + ((cfg1 >> 31) & 0x1);
2271a646342SBen Skeggs 	}
2281a646342SBen Skeggs 
2291167c6bcSBen Skeggs 	if (drm->client.device.info.family == NV_DEVICE_INFO_V0_TNT)
2301a646342SBen Skeggs 		nv04_calc_arb(&fifo_data, &sim_data);
2311a646342SBen Skeggs 	else
2321a646342SBen Skeggs 		nv10_calc_arb(&fifo_data, &sim_data);
2331a646342SBen Skeggs 
2341a646342SBen Skeggs 	*burst = ilog2(fifo_data.burst >> 4);
2351a646342SBen Skeggs 	*lwm = fifo_data.lwm >> 3;
2361a646342SBen Skeggs }
2371a646342SBen Skeggs 
2381a646342SBen Skeggs static void
nv20_update_arb(int * burst,int * lwm)2391a646342SBen Skeggs nv20_update_arb(int *burst, int *lwm)
2401a646342SBen Skeggs {
2411a646342SBen Skeggs 	unsigned int fifo_size, burst_size, graphics_lwm;
2421a646342SBen Skeggs 
2431a646342SBen Skeggs 	fifo_size = 2048;
2441a646342SBen Skeggs 	burst_size = 512;
2451a646342SBen Skeggs 	graphics_lwm = fifo_size - burst_size;
2461a646342SBen Skeggs 
2471a646342SBen Skeggs 	*burst = ilog2(burst_size >> 5);
2481a646342SBen Skeggs 	*lwm = graphics_lwm >> 3;
2491a646342SBen Skeggs }
2501a646342SBen Skeggs 
2511a646342SBen Skeggs void
nouveau_calc_arb(struct drm_device * dev,int vclk,int bpp,int * burst,int * lwm)2521a646342SBen Skeggs nouveau_calc_arb(struct drm_device *dev, int vclk, int bpp, int *burst, int *lwm)
2531a646342SBen Skeggs {
2541a646342SBen Skeggs 	struct nouveau_drm *drm = nouveau_drm(dev);
255*4c0d42f7SThomas Zimmermann 	struct pci_dev *pdev = to_pci_dev(dev->dev);
2561a646342SBen Skeggs 
2571167c6bcSBen Skeggs 	if (drm->client.device.info.family < NV_DEVICE_INFO_V0_KELVIN)
2581a646342SBen Skeggs 		nv04_update_arb(dev, vclk, bpp, burst, lwm);
259*4c0d42f7SThomas Zimmermann 	else if ((pdev->device & 0xfff0) == 0x0240 /*CHIPSET_C51*/ ||
260*4c0d42f7SThomas Zimmermann 		 (pdev->device & 0xfff0) == 0x03d0 /*CHIPSET_C512*/) {
2611a646342SBen Skeggs 		*burst = 128;
2621a646342SBen Skeggs 		*lwm = 0x0480;
2631a646342SBen Skeggs 	} else
2641a646342SBen Skeggs 		nv20_update_arb(burst, lwm);
2651a646342SBen Skeggs }
266