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 
241a646342SBen Skeggs #include <drm/drmP.h>
251a646342SBen Skeggs 
264dc28134SBen Skeggs #include "nouveau_drv.h"
271a646342SBen Skeggs #include "nouveau_reg.h"
281a646342SBen Skeggs #include "hw.h"
291a646342SBen Skeggs 
301a646342SBen Skeggs /****************************************************************************\
311a646342SBen Skeggs *                                                                            *
321a646342SBen Skeggs * The video arbitration routines calculate some "magic" numbers.  Fixes      *
331a646342SBen Skeggs * the snow seen when accessing the framebuffer without it.                   *
341a646342SBen Skeggs * It just works (I hope).                                                    *
351a646342SBen Skeggs *                                                                            *
361a646342SBen Skeggs \****************************************************************************/
371a646342SBen Skeggs 
381a646342SBen Skeggs struct nv_fifo_info {
391a646342SBen Skeggs 	int lwm;
401a646342SBen Skeggs 	int burst;
411a646342SBen Skeggs };
421a646342SBen Skeggs 
431a646342SBen Skeggs struct nv_sim_state {
441a646342SBen Skeggs 	int pclk_khz;
451a646342SBen Skeggs 	int mclk_khz;
461a646342SBen Skeggs 	int nvclk_khz;
471a646342SBen Skeggs 	int bpp;
481a646342SBen Skeggs 	int mem_page_miss;
491a646342SBen Skeggs 	int mem_latency;
501a646342SBen Skeggs 	int memory_type;
511a646342SBen Skeggs 	int memory_width;
521a646342SBen Skeggs 	int two_heads;
531a646342SBen Skeggs };
541a646342SBen Skeggs 
551a646342SBen Skeggs static void
561a646342SBen Skeggs nv04_calc_arb(struct nv_fifo_info *fifo, struct nv_sim_state *arb)
571a646342SBen Skeggs {
581a646342SBen Skeggs 	int pagemiss, cas, width, bpp;
591a646342SBen Skeggs 	int nvclks, mclks, pclks, crtpagemiss;
601a646342SBen Skeggs 	int found, mclk_extra, mclk_loop, cbs, m1, p1;
611a646342SBen Skeggs 	int mclk_freq, pclk_freq, nvclk_freq;
621a646342SBen Skeggs 	int us_m, us_n, us_p, crtc_drain_rate;
631a646342SBen Skeggs 	int cpm_us, us_crt, clwm;
641a646342SBen Skeggs 
651a646342SBen Skeggs 	pclk_freq = arb->pclk_khz;
661a646342SBen Skeggs 	mclk_freq = arb->mclk_khz;
671a646342SBen Skeggs 	nvclk_freq = arb->nvclk_khz;
681a646342SBen Skeggs 	pagemiss = arb->mem_page_miss;
691a646342SBen Skeggs 	cas = arb->mem_latency;
701a646342SBen Skeggs 	width = arb->memory_width >> 6;
711a646342SBen Skeggs 	bpp = arb->bpp;
721a646342SBen Skeggs 	cbs = 128;
731a646342SBen Skeggs 
741a646342SBen Skeggs 	pclks = 2;
751a646342SBen Skeggs 	nvclks = 10;
761a646342SBen Skeggs 	mclks = 13 + cas;
771a646342SBen Skeggs 	mclk_extra = 3;
781a646342SBen Skeggs 	found = 0;
791a646342SBen Skeggs 
801a646342SBen Skeggs 	while (!found) {
811a646342SBen Skeggs 		found = 1;
821a646342SBen Skeggs 
831a646342SBen Skeggs 		mclk_loop = mclks + mclk_extra;
841a646342SBen Skeggs 		us_m = mclk_loop * 1000 * 1000 / mclk_freq;
851a646342SBen Skeggs 		us_n = nvclks * 1000 * 1000 / nvclk_freq;
861a646342SBen Skeggs 		us_p = nvclks * 1000 * 1000 / pclk_freq;
871a646342SBen Skeggs 
881a646342SBen Skeggs 		crtc_drain_rate = pclk_freq * bpp / 8;
891a646342SBen Skeggs 		crtpagemiss = 2;
901a646342SBen Skeggs 		crtpagemiss += 1;
911a646342SBen Skeggs 		cpm_us = crtpagemiss * pagemiss * 1000 * 1000 / mclk_freq;
921a646342SBen Skeggs 		us_crt = cpm_us + us_m + us_n + us_p;
931a646342SBen Skeggs 		clwm = us_crt * crtc_drain_rate / (1000 * 1000);
941a646342SBen Skeggs 		clwm++;
951a646342SBen Skeggs 
961a646342SBen Skeggs 		m1 = clwm + cbs - 512;
971a646342SBen Skeggs 		p1 = m1 * pclk_freq / mclk_freq;
981a646342SBen Skeggs 		p1 = p1 * bpp / 8;
991a646342SBen Skeggs 		if ((p1 < m1 && m1 > 0) || clwm > 519) {
1001a646342SBen Skeggs 			found = !mclk_extra;
1011a646342SBen Skeggs 			mclk_extra--;
1021a646342SBen Skeggs 		}
1031a646342SBen Skeggs 		if (clwm < 384)
1041a646342SBen Skeggs 			clwm = 384;
1051a646342SBen Skeggs 
1061a646342SBen Skeggs 		fifo->lwm = clwm;
1071a646342SBen Skeggs 		fifo->burst = cbs;
1081a646342SBen Skeggs 	}
1091a646342SBen Skeggs }
1101a646342SBen Skeggs 
1111a646342SBen Skeggs static void
1121a646342SBen Skeggs nv10_calc_arb(struct nv_fifo_info *fifo, struct nv_sim_state *arb)
1131a646342SBen Skeggs {
1141a646342SBen Skeggs 	int fill_rate, drain_rate;
1151a646342SBen Skeggs 	int pclks, nvclks, mclks, xclks;
1161a646342SBen Skeggs 	int pclk_freq, nvclk_freq, mclk_freq;
1171a646342SBen Skeggs 	int fill_lat, extra_lat;
1181a646342SBen Skeggs 	int max_burst_o, max_burst_l;
1191a646342SBen Skeggs 	int fifo_len, min_lwm, max_lwm;
1201a646342SBen Skeggs 	const int burst_lat = 80; /* Maximum allowable latency due
1211a646342SBen Skeggs 				   * to the CRTC FIFO burst. (ns) */
1221a646342SBen Skeggs 
1231a646342SBen Skeggs 	pclk_freq = arb->pclk_khz;
1241a646342SBen Skeggs 	nvclk_freq = arb->nvclk_khz;
1251a646342SBen Skeggs 	mclk_freq = arb->mclk_khz;
1261a646342SBen Skeggs 
1271a646342SBen Skeggs 	fill_rate = mclk_freq * arb->memory_width / 8; /* kB/s */
1281a646342SBen Skeggs 	drain_rate = pclk_freq * arb->bpp / 8; /* kB/s */
1291a646342SBen Skeggs 
1301a646342SBen Skeggs 	fifo_len = arb->two_heads ? 1536 : 1024; /* B */
1311a646342SBen Skeggs 
1321a646342SBen Skeggs 	/* Fixed FIFO refill latency. */
1331a646342SBen Skeggs 
1341a646342SBen Skeggs 	pclks = 4;	/* lwm detect. */
1351a646342SBen Skeggs 
1361a646342SBen Skeggs 	nvclks = 3	/* lwm -> sync. */
1371a646342SBen Skeggs 		+ 2	/* fbi bus cycles (1 req + 1 busy) */
1381a646342SBen Skeggs 		+ 1	/* 2 edge sync.  may be very close to edge so
1391a646342SBen Skeggs 			 * just put one. */
1401a646342SBen Skeggs 		+ 1	/* fbi_d_rdv_n */
1411a646342SBen Skeggs 		+ 1	/* Fbi_d_rdata */
1421a646342SBen Skeggs 		+ 1;	/* crtfifo load */
1431a646342SBen Skeggs 
1441a646342SBen Skeggs 	mclks = 1	/* 2 edge sync.  may be very close to edge so
1451a646342SBen Skeggs 			 * just put one. */
1461a646342SBen Skeggs 		+ 1	/* arb_hp_req */
1471a646342SBen Skeggs 		+ 5	/* tiling pipeline */
1481a646342SBen Skeggs 		+ 2	/* latency fifo */
1491a646342SBen Skeggs 		+ 2	/* memory request to fbio block */
1501a646342SBen Skeggs 		+ 7;	/* data returned from fbio block */
1511a646342SBen Skeggs 
1521a646342SBen Skeggs 	/* Need to accumulate 256 bits for read */
1531a646342SBen Skeggs 	mclks += (arb->memory_type == 0 ? 2 : 1)
1541a646342SBen Skeggs 		* arb->memory_width / 32;
1551a646342SBen Skeggs 
1561a646342SBen Skeggs 	fill_lat = mclks * 1000 * 1000 / mclk_freq   /* minimum mclk latency */
1571a646342SBen Skeggs 		+ nvclks * 1000 * 1000 / nvclk_freq  /* nvclk latency */
1581a646342SBen Skeggs 		+ pclks * 1000 * 1000 / pclk_freq;   /* pclk latency */
1591a646342SBen Skeggs 
1601a646342SBen Skeggs 	/* Conditional FIFO refill latency. */
1611a646342SBen Skeggs 
1621a646342SBen Skeggs 	xclks = 2 * arb->mem_page_miss + mclks /* Extra latency due to
1631a646342SBen Skeggs 						* the overlay. */
1641a646342SBen Skeggs 		+ 2 * arb->mem_page_miss       /* Extra pagemiss latency. */
1651a646342SBen Skeggs 		+ (arb->bpp == 32 ? 8 : 4);    /* Margin of error. */
1661a646342SBen Skeggs 
1671a646342SBen Skeggs 	extra_lat = xclks * 1000 * 1000 / mclk_freq;
1681a646342SBen Skeggs 
1691a646342SBen Skeggs 	if (arb->two_heads)
1701a646342SBen Skeggs 		/* Account for another CRTC. */
1711a646342SBen Skeggs 		extra_lat += fill_lat + extra_lat + burst_lat;
1721a646342SBen Skeggs 
1731a646342SBen Skeggs 	/* FIFO burst */
1741a646342SBen Skeggs 
1751a646342SBen Skeggs 	/* Max burst not leading to overflows. */
1761a646342SBen Skeggs 	max_burst_o = (1 + fifo_len - extra_lat * drain_rate / (1000 * 1000))
1771a646342SBen Skeggs 		* (fill_rate / 1000) / ((fill_rate - drain_rate) / 1000);
1781a646342SBen Skeggs 	fifo->burst = min(max_burst_o, 1024);
1791a646342SBen Skeggs 
1801a646342SBen Skeggs 	/* Max burst value with an acceptable latency. */
1811a646342SBen Skeggs 	max_burst_l = burst_lat * fill_rate / (1000 * 1000);
1821a646342SBen Skeggs 	fifo->burst = min(max_burst_l, fifo->burst);
1831a646342SBen Skeggs 
1841a646342SBen Skeggs 	fifo->burst = rounddown_pow_of_two(fifo->burst);
1851a646342SBen Skeggs 
1861a646342SBen Skeggs 	/* FIFO low watermark */
1871a646342SBen Skeggs 
1881a646342SBen Skeggs 	min_lwm = (fill_lat + extra_lat) * drain_rate / (1000 * 1000) + 1;
1891a646342SBen Skeggs 	max_lwm = fifo_len - fifo->burst
1901a646342SBen Skeggs 		+ fill_lat * drain_rate / (1000 * 1000)
1911a646342SBen Skeggs 		+ fifo->burst * drain_rate / fill_rate;
1921a646342SBen Skeggs 
1931a646342SBen Skeggs 	fifo->lwm = min_lwm + 10 * (max_lwm - min_lwm) / 100; /* Empirical. */
1941a646342SBen Skeggs }
1951a646342SBen Skeggs 
1961a646342SBen Skeggs static void
1971a646342SBen Skeggs nv04_update_arb(struct drm_device *dev, int VClk, int bpp,
1981a646342SBen Skeggs 		int *burst, int *lwm)
1991a646342SBen Skeggs {
2001a646342SBen Skeggs 	struct nouveau_drm *drm = nouveau_drm(dev);
2011167c6bcSBen Skeggs 	struct nvif_object *device = &nouveau_drm(dev)->client.device.object;
2021a646342SBen Skeggs 	struct nv_fifo_info fifo_data;
2031a646342SBen Skeggs 	struct nv_sim_state sim_data;
2041a646342SBen Skeggs 	int MClk = nouveau_hw_get_clock(dev, PLL_MEMORY);
2051a646342SBen Skeggs 	int NVClk = nouveau_hw_get_clock(dev, PLL_CORE);
206db2bec18SBen Skeggs 	uint32_t cfg1 = nvif_rd32(device, NV04_PFB_CFG1);
2071a646342SBen Skeggs 
2081a646342SBen Skeggs 	sim_data.pclk_khz = VClk;
2091a646342SBen Skeggs 	sim_data.mclk_khz = MClk;
2101a646342SBen Skeggs 	sim_data.nvclk_khz = NVClk;
2111a646342SBen Skeggs 	sim_data.bpp = bpp;
2121a646342SBen Skeggs 	sim_data.two_heads = nv_two_heads(dev);
213ffbab09bSVille Syrjälä 	if ((dev->pdev->device & 0xffff) == 0x01a0 /*CHIPSET_NFORCE*/ ||
214ffbab09bSVille Syrjälä 	    (dev->pdev->device & 0xffff) == 0x01f0 /*CHIPSET_NFORCE2*/) {
2151a646342SBen Skeggs 		uint32_t type;
2162fa6d6cdSSinan Kaya 		int domain = pci_domain_nr(dev->pdev->bus);
2171a646342SBen Skeggs 
2182fa6d6cdSSinan Kaya 		pci_read_config_dword(pci_get_domain_bus_and_slot(domain, 0, 1),
2192fa6d6cdSSinan Kaya 				      0x7c, &type);
2201a646342SBen Skeggs 
2211a646342SBen Skeggs 		sim_data.memory_type = (type >> 12) & 1;
2221a646342SBen Skeggs 		sim_data.memory_width = 64;
2231a646342SBen Skeggs 		sim_data.mem_latency = 3;
2241a646342SBen Skeggs 		sim_data.mem_page_miss = 10;
2251a646342SBen Skeggs 	} else {
226db2bec18SBen Skeggs 		sim_data.memory_type = nvif_rd32(device, NV04_PFB_CFG0) & 0x1;
227db2bec18SBen Skeggs 		sim_data.memory_width = (nvif_rd32(device, NV_PEXTDEV_BOOT_0) & 0x10) ? 128 : 64;
2281a646342SBen Skeggs 		sim_data.mem_latency = cfg1 & 0xf;
2291a646342SBen Skeggs 		sim_data.mem_page_miss = ((cfg1 >> 4) & 0xf) + ((cfg1 >> 31) & 0x1);
2301a646342SBen Skeggs 	}
2311a646342SBen Skeggs 
2321167c6bcSBen Skeggs 	if (drm->client.device.info.family == NV_DEVICE_INFO_V0_TNT)
2331a646342SBen Skeggs 		nv04_calc_arb(&fifo_data, &sim_data);
2341a646342SBen Skeggs 	else
2351a646342SBen Skeggs 		nv10_calc_arb(&fifo_data, &sim_data);
2361a646342SBen Skeggs 
2371a646342SBen Skeggs 	*burst = ilog2(fifo_data.burst >> 4);
2381a646342SBen Skeggs 	*lwm = fifo_data.lwm >> 3;
2391a646342SBen Skeggs }
2401a646342SBen Skeggs 
2411a646342SBen Skeggs static void
2421a646342SBen Skeggs nv20_update_arb(int *burst, int *lwm)
2431a646342SBen Skeggs {
2441a646342SBen Skeggs 	unsigned int fifo_size, burst_size, graphics_lwm;
2451a646342SBen Skeggs 
2461a646342SBen Skeggs 	fifo_size = 2048;
2471a646342SBen Skeggs 	burst_size = 512;
2481a646342SBen Skeggs 	graphics_lwm = fifo_size - burst_size;
2491a646342SBen Skeggs 
2501a646342SBen Skeggs 	*burst = ilog2(burst_size >> 5);
2511a646342SBen Skeggs 	*lwm = graphics_lwm >> 3;
2521a646342SBen Skeggs }
2531a646342SBen Skeggs 
2541a646342SBen Skeggs void
2551a646342SBen Skeggs nouveau_calc_arb(struct drm_device *dev, int vclk, int bpp, int *burst, int *lwm)
2561a646342SBen Skeggs {
2571a646342SBen Skeggs 	struct nouveau_drm *drm = nouveau_drm(dev);
2581a646342SBen Skeggs 
2591167c6bcSBen Skeggs 	if (drm->client.device.info.family < NV_DEVICE_INFO_V0_KELVIN)
2601a646342SBen Skeggs 		nv04_update_arb(dev, vclk, bpp, burst, lwm);
261ffbab09bSVille Syrjälä 	else if ((dev->pdev->device & 0xfff0) == 0x0240 /*CHIPSET_C51*/ ||
262ffbab09bSVille Syrjälä 		 (dev->pdev->device & 0xfff0) == 0x03d0 /*CHIPSET_C512*/) {
2631a646342SBen Skeggs 		*burst = 128;
2641a646342SBen Skeggs 		*lwm = 0x0480;
2651a646342SBen Skeggs 	} else
2661a646342SBen Skeggs 		nv20_update_arb(burst, lwm);
2671a646342SBen Skeggs }
268