i915_dma.c revision 239965
1219019Sgabor/* i915_dma.c -- DMA support for the I915 -*- linux-c -*-
2219019Sgabor */
3219019Sgabor/*-
4219019Sgabor * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5219019Sgabor * All Rights Reserved.
6219019Sgabor *
7219019Sgabor * Permission is hereby granted, free of charge, to any person obtaining a
8219019Sgabor * copy of this software and associated documentation files (the
9219019Sgabor * "Software"), to deal in the Software without restriction, including
10219019Sgabor * without limitation the rights to use, copy, modify, merge, publish,
11219019Sgabor * distribute, sub license, and/or sell copies of the Software, and to
12219019Sgabor * permit persons to whom the Software is furnished to do so, subject to
13219019Sgabor * the following conditions:
14219019Sgabor *
15219019Sgabor * The above copyright notice and this permission notice (including the
16219019Sgabor * next paragraph) shall be included in all copies or substantial portions
17219019Sgabor * of the Software.
18219019Sgabor *
19219019Sgabor * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20219019Sgabor * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21219019Sgabor * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22219019Sgabor * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23219019Sgabor * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24219019Sgabor * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25219019Sgabor * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26219019Sgabor *
27219019Sgabor */
28219019Sgabor
29219019Sgabor#include <sys/cdefs.h>
30219019Sgabor__FBSDID("$FreeBSD: stable/9/sys/dev/drm2/i915/i915_dma.c 239965 2012-09-01 05:35:48Z kib $");
31219019Sgabor
32219019Sgabor#include <dev/drm2/drmP.h>
33219019Sgabor#include <dev/drm2/drm.h>
34219019Sgabor#include <dev/drm2/i915/i915_drm.h>
35219019Sgabor#include <dev/drm2/i915/i915_drv.h>
36219019Sgabor#include <dev/drm2/i915/intel_drv.h>
37219019Sgabor#include <dev/drm2/i915/intel_ringbuffer.h>
38219019Sgabor
39219019Sgaborstatic struct drm_i915_private *i915_mch_dev;
40219019Sgabor/*
41219019Sgabor * Lock protecting IPS related data structures
42219019Sgabor *   - i915_mch_dev
43219019Sgabor *   - dev_priv->max_delay
44219019Sgabor *   - dev_priv->min_delay
45219019Sgabor *   - dev_priv->fmax
46219019Sgabor *   - dev_priv->gpu_busy
47219019Sgabor */
48219019Sgaborstatic struct mtx mchdev_lock;
49219019SgaborMTX_SYSINIT(mchdev, &mchdev_lock, "mchdev", MTX_DEF);
50219019Sgabor
51219019Sgaborstatic void i915_pineview_get_mem_freq(struct drm_device *dev);
52219019Sgaborstatic void i915_ironlake_get_mem_freq(struct drm_device *dev);
53219019Sgaborstatic int i915_driver_unload_int(struct drm_device *dev, bool locked);
54219019Sgabor
55219019Sgaborstatic void i915_write_hws_pga(struct drm_device *dev)
56219019Sgabor{
57219019Sgabor	drm_i915_private_t *dev_priv = dev->dev_private;
58219019Sgabor	u32 addr;
59219019Sgabor
60219019Sgabor	addr = dev_priv->status_page_dmah->busaddr;
61219019Sgabor	if (INTEL_INFO(dev)->gen >= 4)
62219019Sgabor		addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
63219019Sgabor	I915_WRITE(HWS_PGA, addr);
64219019Sgabor}
65219019Sgabor
66219019Sgabor/**
67219019Sgabor * Sets up the hardware status page for devices that need a physical address
68219019Sgabor * in the register.
69219019Sgabor */
70219019Sgaborstatic int i915_init_phys_hws(struct drm_device *dev)
71219019Sgabor{
72219019Sgabor	drm_i915_private_t *dev_priv = dev->dev_private;
73219019Sgabor	struct intel_ring_buffer *ring = LP_RING(dev_priv);
74219019Sgabor
75219019Sgabor	/*
76219019Sgabor	 * Program Hardware Status Page
77219019Sgabor	 * XXXKIB Keep 4GB limit for allocation for now.  This method
78219019Sgabor	 * of allocation is used on <= 965 hardware, that has several
79219019Sgabor	 * erratas regarding the use of physical memory > 4 GB.
80219019Sgabor	 */
81219019Sgabor	DRM_UNLOCK(dev);
82219019Sgabor	dev_priv->status_page_dmah =
83219019Sgabor		drm_pci_alloc(dev, PAGE_SIZE, PAGE_SIZE, 0xffffffff);
84219019Sgabor	DRM_LOCK(dev);
85219019Sgabor	if (!dev_priv->status_page_dmah) {
86219019Sgabor		DRM_ERROR("Can not allocate hardware status page\n");
87219019Sgabor		return -ENOMEM;
88219019Sgabor	}
89219019Sgabor	ring->status_page.page_addr = dev_priv->hw_status_page =
90219019Sgabor	    dev_priv->status_page_dmah->vaddr;
91219019Sgabor	dev_priv->dma_status_page = dev_priv->status_page_dmah->busaddr;
92219019Sgabor
93219019Sgabor	memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
94219019Sgabor
95219019Sgabor	i915_write_hws_pga(dev);
96219019Sgabor	DRM_DEBUG("Enabled hardware status page, phys %jx\n",
97219019Sgabor	    (uintmax_t)dev_priv->dma_status_page);
98219019Sgabor	return 0;
99219019Sgabor}
100219019Sgabor
101219019Sgabor/**
102219019Sgabor * Frees the hardware status page, whether it's a physical address or a virtual
103219019Sgabor * address set up by the X Server.
104219019Sgabor */
105219019Sgaborstatic void i915_free_hws(struct drm_device *dev)
106219019Sgabor{
107219019Sgabor	drm_i915_private_t *dev_priv = dev->dev_private;
108219019Sgabor	struct intel_ring_buffer *ring = LP_RING(dev_priv);
109219019Sgabor
110219019Sgabor	if (dev_priv->status_page_dmah) {
111219019Sgabor		drm_pci_free(dev, dev_priv->status_page_dmah);
112219019Sgabor		dev_priv->status_page_dmah = NULL;
113219019Sgabor	}
114219019Sgabor
115219019Sgabor	if (dev_priv->status_gfx_addr) {
116219019Sgabor		dev_priv->status_gfx_addr = 0;
117219019Sgabor		ring->status_page.gfx_addr = 0;
118219019Sgabor		drm_core_ioremapfree(&dev_priv->hws_map, dev);
119219019Sgabor	}
120219019Sgabor
121219019Sgabor	/* Need to rewrite hardware status page */
122219019Sgabor	I915_WRITE(HWS_PGA, 0x1ffff000);
123219019Sgabor}
124219019Sgabor
125219019Sgaborvoid i915_kernel_lost_context(struct drm_device * dev)
126219019Sgabor{
127219019Sgabor	drm_i915_private_t *dev_priv = dev->dev_private;
128219019Sgabor	struct intel_ring_buffer *ring = LP_RING(dev_priv);
129219019Sgabor
130219019Sgabor	/*
131219019Sgabor	 * We should never lose context on the ring with modesetting
132219019Sgabor	 * as we don't expose it to userspace
133219019Sgabor	 */
134219019Sgabor	if (drm_core_check_feature(dev, DRIVER_MODESET))
135219019Sgabor		return;
136219019Sgabor
137219019Sgabor	ring->head = I915_READ_HEAD(ring) & HEAD_ADDR;
138219019Sgabor	ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
139219019Sgabor	ring->space = ring->head - (ring->tail + 8);
140219019Sgabor	if (ring->space < 0)
141219019Sgabor		ring->space += ring->size;
142219019Sgabor
143219019Sgabor#if 1
144219019Sgabor	KIB_NOTYET();
145219019Sgabor#else
146219019Sgabor	if (!dev->primary->master)
147219019Sgabor		return;
148219019Sgabor#endif
149219019Sgabor
150219019Sgabor	if (ring->head == ring->tail && dev_priv->sarea_priv)
151219019Sgabor		dev_priv->sarea_priv->perf_boxes |= I915_BOX_RING_EMPTY;
152219019Sgabor}
153219019Sgabor
154219019Sgaborstatic int i915_dma_cleanup(struct drm_device * dev)
155219019Sgabor{
156219019Sgabor	drm_i915_private_t *dev_priv = dev->dev_private;
157219019Sgabor	int i;
158219019Sgabor
159219019Sgabor
160219019Sgabor	/* Make sure interrupts are disabled here because the uninstall ioctl
161219019Sgabor	 * may not have been called from userspace and after dev_private
162219019Sgabor	 * is freed, it's too late.
163219019Sgabor	 */
164219019Sgabor	if (dev->irq_enabled)
165219019Sgabor		drm_irq_uninstall(dev);
166219019Sgabor
167219019Sgabor	for (i = 0; i < I915_NUM_RINGS; i++)
168219019Sgabor		intel_cleanup_ring_buffer(&dev_priv->rings[i]);
169219019Sgabor
170219019Sgabor	/* Clear the HWS virtual address at teardown */
171219019Sgabor	if (I915_NEED_GFX_HWS(dev))
172219019Sgabor		i915_free_hws(dev);
173219019Sgabor
174219019Sgabor	return 0;
175219019Sgabor}
176219019Sgabor
177219019Sgaborstatic int i915_initialize(struct drm_device * dev, drm_i915_init_t * init)
178219019Sgabor{
179219019Sgabor	drm_i915_private_t *dev_priv = dev->dev_private;
180219019Sgabor	int ret;
181219019Sgabor
182219019Sgabor	dev_priv->sarea = drm_getsarea(dev);
183219019Sgabor	if (!dev_priv->sarea) {
184219019Sgabor		DRM_ERROR("can not find sarea!\n");
185219019Sgabor		i915_dma_cleanup(dev);
186219019Sgabor		return -EINVAL;
187219019Sgabor	}
188219019Sgabor
189219019Sgabor	dev_priv->sarea_priv = (drm_i915_sarea_t *)
190219019Sgabor	    ((u8 *) dev_priv->sarea->virtual + init->sarea_priv_offset);
191219019Sgabor
192219019Sgabor	if (init->ring_size != 0) {
193219019Sgabor		if (LP_RING(dev_priv)->obj != NULL) {
194219019Sgabor			i915_dma_cleanup(dev);
195219019Sgabor			DRM_ERROR("Client tried to initialize ringbuffer in "
196219019Sgabor				  "GEM mode\n");
197219019Sgabor			return -EINVAL;
198219019Sgabor		}
199219019Sgabor
200219019Sgabor		ret = intel_render_ring_init_dri(dev,
201219019Sgabor						 init->ring_start,
202219019Sgabor						 init->ring_size);
203219019Sgabor		if (ret) {
204219019Sgabor			i915_dma_cleanup(dev);
205219019Sgabor			return ret;
206219019Sgabor		}
207219019Sgabor	}
208219019Sgabor
209219019Sgabor	dev_priv->cpp = init->cpp;
210219019Sgabor	dev_priv->back_offset = init->back_offset;
211219019Sgabor	dev_priv->front_offset = init->front_offset;
212219019Sgabor	dev_priv->current_page = 0;
213219019Sgabor	dev_priv->sarea_priv->pf_current_page = 0;
214219019Sgabor
215219019Sgabor	/* Allow hardware batchbuffers unless told otherwise.
216219019Sgabor	 */
217219019Sgabor	dev_priv->allow_batchbuffer = 1;
218219019Sgabor
219219019Sgabor	return 0;
220219019Sgabor}
221219019Sgabor
222219019Sgaborstatic int i915_dma_resume(struct drm_device * dev)
223219019Sgabor{
224219019Sgabor	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
225219019Sgabor	struct intel_ring_buffer *ring = LP_RING(dev_priv);
226219019Sgabor
227219019Sgabor	DRM_DEBUG("\n");
228219019Sgabor
229219019Sgabor	if (ring->map.handle == NULL) {
230219019Sgabor		DRM_ERROR("can not ioremap virtual address for"
231219019Sgabor			  " ring buffer\n");
232219019Sgabor		return -ENOMEM;
233219019Sgabor	}
234219019Sgabor
235219019Sgabor	/* Program Hardware Status Page */
236219019Sgabor	if (!ring->status_page.page_addr) {
237219019Sgabor		DRM_ERROR("Can not find hardware status page\n");
238219019Sgabor		return -EINVAL;
239219019Sgabor	}
240219019Sgabor	DRM_DEBUG("hw status page @ %p\n", ring->status_page.page_addr);
241219019Sgabor	if (ring->status_page.gfx_addr != 0)
242219019Sgabor		intel_ring_setup_status_page(ring);
243219019Sgabor	else
244219019Sgabor		i915_write_hws_pga(dev);
245219019Sgabor
246219019Sgabor	DRM_DEBUG("Enabled hardware status page\n");
247219019Sgabor
248219019Sgabor	return 0;
249219019Sgabor}
250219019Sgabor
251219019Sgaborstatic int i915_dma_init(struct drm_device *dev, void *data,
252219019Sgabor			 struct drm_file *file_priv)
253219019Sgabor{
254219019Sgabor	drm_i915_init_t *init = data;
255219019Sgabor	int retcode = 0;
256219019Sgabor
257219019Sgabor	switch (init->func) {
258219019Sgabor	case I915_INIT_DMA:
259219019Sgabor		retcode = i915_initialize(dev, init);
260219019Sgabor		break;
261219019Sgabor	case I915_CLEANUP_DMA:
262219019Sgabor		retcode = i915_dma_cleanup(dev);
263219019Sgabor		break;
264219019Sgabor	case I915_RESUME_DMA:
265219019Sgabor		retcode = i915_dma_resume(dev);
266219019Sgabor		break;
267219019Sgabor	default:
268219019Sgabor		retcode = -EINVAL;
269219019Sgabor		break;
270219019Sgabor	}
271219019Sgabor
272219019Sgabor	return retcode;
273219019Sgabor}
274219019Sgabor
275219019Sgabor/* Implement basically the same security restrictions as hardware does
276219019Sgabor * for MI_BATCH_NON_SECURE.  These can be made stricter at any time.
277219019Sgabor *
278219019Sgabor * Most of the calculations below involve calculating the size of a
279219019Sgabor * particular instruction.  It's important to get the size right as
280219019Sgabor * that tells us where the next instruction to check is.  Any illegal
281219019Sgabor * instruction detected will be given a size of zero, which is a
282219019Sgabor * signal to abort the rest of the buffer.
283219019Sgabor */
284219019Sgaborstatic int do_validate_cmd(int cmd)
285219019Sgabor{
286219019Sgabor	switch (((cmd >> 29) & 0x7)) {
287219019Sgabor	case 0x0:
288219019Sgabor		switch ((cmd >> 23) & 0x3f) {
289219019Sgabor		case 0x0:
290219019Sgabor			return 1;	/* MI_NOOP */
291219019Sgabor		case 0x4:
292219019Sgabor			return 1;	/* MI_FLUSH */
293219019Sgabor		default:
294219019Sgabor			return 0;	/* disallow everything else */
295219019Sgabor		}
296219019Sgabor		break;
297219019Sgabor	case 0x1:
298219019Sgabor		return 0;	/* reserved */
299219019Sgabor	case 0x2:
300219019Sgabor		return (cmd & 0xff) + 2;	/* 2d commands */
301219019Sgabor	case 0x3:
302219019Sgabor		if (((cmd >> 24) & 0x1f) <= 0x18)
303219019Sgabor			return 1;
304219019Sgabor
305219019Sgabor		switch ((cmd >> 24) & 0x1f) {
306219019Sgabor		case 0x1c:
307219019Sgabor			return 1;
308219019Sgabor		case 0x1d:
309219019Sgabor			switch ((cmd >> 16) & 0xff) {
310219019Sgabor			case 0x3:
311219019Sgabor				return (cmd & 0x1f) + 2;
312219019Sgabor			case 0x4:
313219019Sgabor				return (cmd & 0xf) + 2;
314219019Sgabor			default:
315219019Sgabor				return (cmd & 0xffff) + 2;
316219019Sgabor			}
317219019Sgabor		case 0x1e:
318219019Sgabor			if (cmd & (1 << 23))
319219019Sgabor				return (cmd & 0xffff) + 1;
320219019Sgabor			else
321219019Sgabor				return 1;
322219019Sgabor		case 0x1f:
323219019Sgabor			if ((cmd & (1 << 23)) == 0)	/* inline vertices */
324219019Sgabor				return (cmd & 0x1ffff) + 2;
325219019Sgabor			else if (cmd & (1 << 17))	/* indirect random */
326219019Sgabor				if ((cmd & 0xffff) == 0)
327219019Sgabor					return 0;	/* unknown length, too hard */
328219019Sgabor				else
329219019Sgabor					return (((cmd & 0xffff) + 1) / 2) + 1;
330219019Sgabor			else
331219019Sgabor				return 2;	/* indirect sequential */
332219019Sgabor		default:
333219019Sgabor			return 0;
334219019Sgabor		}
335219019Sgabor	default:
336219019Sgabor		return 0;
337219019Sgabor	}
338219019Sgabor
339219019Sgabor	return 0;
340219019Sgabor}
341219019Sgabor
342219019Sgaborstatic int validate_cmd(int cmd)
343219019Sgabor{
344219019Sgabor	int ret = do_validate_cmd(cmd);
345219019Sgabor
346219019Sgabor/*	printk("validate_cmd( %x ): %d\n", cmd, ret); */
347219019Sgabor
348219019Sgabor	return ret;
349219019Sgabor}
350219019Sgabor
351219019Sgaborstatic int i915_emit_cmds(struct drm_device *dev, int __user *buffer,
352219019Sgabor			  int dwords)
353219019Sgabor{
354219019Sgabor	drm_i915_private_t *dev_priv = dev->dev_private;
355219019Sgabor	int i;
356219019Sgabor
357219019Sgabor	if ((dwords+1) * sizeof(int) >= LP_RING(dev_priv)->size - 8)
358219019Sgabor		return -EINVAL;
359219019Sgabor
360219019Sgabor	BEGIN_LP_RING((dwords+1)&~1);
361219019Sgabor
362219019Sgabor	for (i = 0; i < dwords;) {
363219019Sgabor		int cmd, sz;
364219019Sgabor
365219019Sgabor		if (DRM_COPY_FROM_USER_UNCHECKED(&cmd, &buffer[i], sizeof(cmd)))
366219019Sgabor			return -EINVAL;
367219019Sgabor
368219019Sgabor		if ((sz = validate_cmd(cmd)) == 0 || i + sz > dwords)
369219019Sgabor			return -EINVAL;
370219019Sgabor
371219019Sgabor		OUT_RING(cmd);
372219019Sgabor
373219019Sgabor		while (++i, --sz) {
374219019Sgabor			if (DRM_COPY_FROM_USER_UNCHECKED(&cmd, &buffer[i],
375219019Sgabor							 sizeof(cmd))) {
376219019Sgabor				return -EINVAL;
377219019Sgabor			}
378219019Sgabor			OUT_RING(cmd);
379219019Sgabor		}
380219019Sgabor	}
381219019Sgabor
382219019Sgabor	if (dwords & 1)
383219019Sgabor		OUT_RING(0);
384219019Sgabor
385219019Sgabor	ADVANCE_LP_RING();
386219019Sgabor
387219019Sgabor	return 0;
388219019Sgabor}
389219019Sgabor
390219019Sgaborint i915_emit_box(struct drm_device * dev,
391219019Sgabor		  struct drm_clip_rect *boxes,
392219019Sgabor		  int i, int DR1, int DR4)
393219019Sgabor{
394219019Sgabor	struct drm_clip_rect box;
395219019Sgabor
396219019Sgabor	if (DRM_COPY_FROM_USER_UNCHECKED(&box, &boxes[i], sizeof(box))) {
397219019Sgabor		return -EFAULT;
398219019Sgabor	}
399219019Sgabor
400219019Sgabor	return (i915_emit_box_p(dev, &box, DR1, DR4));
401219019Sgabor}
402219019Sgabor
403219019Sgaborint
404219019Sgabori915_emit_box_p(struct drm_device *dev, struct drm_clip_rect *box,
405219019Sgabor    int DR1, int DR4)
406219019Sgabor{
407219019Sgabor	drm_i915_private_t *dev_priv = dev->dev_private;
408219019Sgabor	int ret;
409219019Sgabor
410219019Sgabor	if (box->y2 <= box->y1 || box->x2 <= box->x1 || box->y2 <= 0 ||
411219019Sgabor	    box->x2 <= 0) {
412219019Sgabor		DRM_ERROR("Bad box %d,%d..%d,%d\n",
413219019Sgabor			  box->x1, box->y1, box->x2, box->y2);
414219019Sgabor		return -EINVAL;
415219019Sgabor	}
416219019Sgabor
417219019Sgabor	if (INTEL_INFO(dev)->gen >= 4) {
418219019Sgabor		ret = BEGIN_LP_RING(4);
419219019Sgabor		if (ret != 0)
420219019Sgabor			return (ret);
421219019Sgabor
422219019Sgabor		OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
423219019Sgabor		OUT_RING((box->x1 & 0xffff) | (box->y1 << 16));
424219019Sgabor		OUT_RING(((box->x2 - 1) & 0xffff) | ((box->y2 - 1) << 16));
425219019Sgabor		OUT_RING(DR4);
426219019Sgabor	} else {
427219019Sgabor		ret = BEGIN_LP_RING(6);
428219019Sgabor		if (ret != 0)
429219019Sgabor			return (ret);
430219019Sgabor
431219019Sgabor		OUT_RING(GFX_OP_DRAWRECT_INFO);
432219019Sgabor		OUT_RING(DR1);
433219019Sgabor		OUT_RING((box->x1 & 0xffff) | (box->y1 << 16));
434219019Sgabor		OUT_RING(((box->x2 - 1) & 0xffff) | ((box->y2 - 1) << 16));
435219019Sgabor		OUT_RING(DR4);
436219019Sgabor		OUT_RING(0);
437219019Sgabor	}
438219019Sgabor	ADVANCE_LP_RING();
439219019Sgabor
440219019Sgabor	return 0;
441219019Sgabor}
442219019Sgabor
443219019Sgabor/* XXX: Emitting the counter should really be moved to part of the IRQ
444219019Sgabor * emit. For now, do it in both places:
445219019Sgabor */
446219019Sgabor
447219019Sgaborstatic void i915_emit_breadcrumb(struct drm_device *dev)
448219019Sgabor{
449219019Sgabor	drm_i915_private_t *dev_priv = dev->dev_private;
450219019Sgabor
451219019Sgabor	if (++dev_priv->counter > 0x7FFFFFFFUL)
452219019Sgabor		dev_priv->counter = 0;
453219019Sgabor	if (dev_priv->sarea_priv)
454219019Sgabor		dev_priv->sarea_priv->last_enqueue = dev_priv->counter;
455219019Sgabor
456219019Sgabor	if (BEGIN_LP_RING(4) == 0) {
457219019Sgabor		OUT_RING(MI_STORE_DWORD_INDEX);
458219019Sgabor		OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
459219019Sgabor		OUT_RING(dev_priv->counter);
460219019Sgabor		OUT_RING(0);
461219019Sgabor		ADVANCE_LP_RING();
462219019Sgabor	}
463219019Sgabor}
464219019Sgabor
465219019Sgaborstatic int i915_dispatch_cmdbuffer(struct drm_device * dev,
466219019Sgabor    drm_i915_cmdbuffer_t * cmd, struct drm_clip_rect *cliprects, void *cmdbuf)
467219019Sgabor{
468219019Sgabor	int nbox = cmd->num_cliprects;
469219019Sgabor	int i = 0, count, ret;
470219019Sgabor
471219019Sgabor	if (cmd->sz & 0x3) {
472219019Sgabor		DRM_ERROR("alignment\n");
473219019Sgabor		return -EINVAL;
474219019Sgabor	}
475219019Sgabor
476219019Sgabor	i915_kernel_lost_context(dev);
477219019Sgabor
478219019Sgabor	count = nbox ? nbox : 1;
479219019Sgabor
480219019Sgabor	for (i = 0; i < count; i++) {
481219019Sgabor		if (i < nbox) {
482219019Sgabor			ret = i915_emit_box_p(dev, &cmd->cliprects[i],
483219019Sgabor			    cmd->DR1, cmd->DR4);
484219019Sgabor			if (ret)
485219019Sgabor				return ret;
486219019Sgabor		}
487219019Sgabor
488219019Sgabor		ret = i915_emit_cmds(dev, cmdbuf, cmd->sz / 4);
489219019Sgabor		if (ret)
490219019Sgabor			return ret;
491219019Sgabor	}
492219019Sgabor
493219019Sgabor	i915_emit_breadcrumb(dev);
494219019Sgabor	return 0;
495219019Sgabor}
496219019Sgabor
497219019Sgaborstatic int
498219019Sgabori915_dispatch_batchbuffer(struct drm_device * dev,
499219019Sgabor    drm_i915_batchbuffer_t * batch, struct drm_clip_rect *cliprects)
500219019Sgabor{
501219019Sgabor	drm_i915_private_t *dev_priv = dev->dev_private;
502219019Sgabor	int nbox = batch->num_cliprects;
503219019Sgabor	int i, count, ret;
504219019Sgabor
505219019Sgabor	if ((batch->start | batch->used) & 0x7) {
506219019Sgabor		DRM_ERROR("alignment\n");
507219019Sgabor		return -EINVAL;
508219019Sgabor	}
509219019Sgabor
510219019Sgabor	i915_kernel_lost_context(dev);
511219019Sgabor
512219019Sgabor	count = nbox ? nbox : 1;
513219019Sgabor
514219019Sgabor	for (i = 0; i < count; i++) {
515219019Sgabor		if (i < nbox) {
516219019Sgabor			int ret = i915_emit_box_p(dev, &cliprects[i],
517219019Sgabor			    batch->DR1, batch->DR4);
518219019Sgabor			if (ret)
519219019Sgabor				return ret;
520219019Sgabor		}
521219019Sgabor
522219019Sgabor		if (!IS_I830(dev) && !IS_845G(dev)) {
523219019Sgabor			ret = BEGIN_LP_RING(2);
524219019Sgabor			if (ret != 0)
525219019Sgabor				return (ret);
526219019Sgabor
527219019Sgabor			if (INTEL_INFO(dev)->gen >= 4) {
528219019Sgabor				OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) |
529219019Sgabor				    MI_BATCH_NON_SECURE_I965);
530219019Sgabor				OUT_RING(batch->start);
531219019Sgabor			} else {
532219019Sgabor				OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
533219019Sgabor				OUT_RING(batch->start | MI_BATCH_NON_SECURE);
534219019Sgabor			}
535219019Sgabor		} else {
536219019Sgabor			ret = BEGIN_LP_RING(4);
537219019Sgabor			if (ret != 0)
538219019Sgabor				return (ret);
539219019Sgabor
540219019Sgabor			OUT_RING(MI_BATCH_BUFFER);
541219019Sgabor			OUT_RING(batch->start | MI_BATCH_NON_SECURE);
542219019Sgabor			OUT_RING(batch->start + batch->used - 4);
543219019Sgabor			OUT_RING(0);
544219019Sgabor		}
545219019Sgabor		ADVANCE_LP_RING();
546219019Sgabor	}
547219019Sgabor
548219019Sgabor	i915_emit_breadcrumb(dev);
549219019Sgabor
550219019Sgabor	return 0;
551219019Sgabor}
552219019Sgabor
553219019Sgaborstatic int i915_dispatch_flip(struct drm_device * dev)
554219019Sgabor{
555219019Sgabor	drm_i915_private_t *dev_priv = dev->dev_private;
556219019Sgabor	int ret;
557219019Sgabor
558219019Sgabor	if (!dev_priv->sarea_priv)
559219019Sgabor		return -EINVAL;
560219019Sgabor
561219019Sgabor	DRM_DEBUG("%s: page=%d pfCurrentPage=%d\n",
562219019Sgabor		  __func__,
563219019Sgabor		  dev_priv->current_page,
564219019Sgabor		  dev_priv->sarea_priv->pf_current_page);
565219019Sgabor
566219019Sgabor	i915_kernel_lost_context(dev);
567219019Sgabor
568219019Sgabor	ret = BEGIN_LP_RING(10);
569219019Sgabor	if (ret)
570219019Sgabor		return ret;
571219019Sgabor	OUT_RING(MI_FLUSH | MI_READ_FLUSH);
572219019Sgabor	OUT_RING(0);
573219019Sgabor
574219019Sgabor	OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
575219019Sgabor	OUT_RING(0);
576219019Sgabor	if (dev_priv->current_page == 0) {
577219019Sgabor		OUT_RING(dev_priv->back_offset);
578219019Sgabor		dev_priv->current_page = 1;
579219019Sgabor	} else {
580219019Sgabor		OUT_RING(dev_priv->front_offset);
581219019Sgabor		dev_priv->current_page = 0;
582219019Sgabor	}
583219019Sgabor	OUT_RING(0);
584
585	OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
586	OUT_RING(0);
587
588	ADVANCE_LP_RING();
589
590	if (++dev_priv->counter > 0x7FFFFFFFUL)
591		dev_priv->counter = 0;
592	if (dev_priv->sarea_priv)
593		dev_priv->sarea_priv->last_enqueue = dev_priv->counter;
594
595	if (BEGIN_LP_RING(4) == 0) {
596		OUT_RING(MI_STORE_DWORD_INDEX);
597		OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
598		OUT_RING(dev_priv->counter);
599		OUT_RING(0);
600		ADVANCE_LP_RING();
601	}
602
603	dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
604	return 0;
605}
606
607static int
608i915_quiescent(struct drm_device *dev)
609{
610	struct intel_ring_buffer *ring = LP_RING(dev->dev_private);
611
612	i915_kernel_lost_context(dev);
613	return (intel_wait_ring_idle(ring));
614}
615
616static int
617i915_flush_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
618{
619	int ret;
620
621	RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
622
623	DRM_LOCK(dev);
624	ret = i915_quiescent(dev);
625	DRM_UNLOCK(dev);
626
627	return (ret);
628}
629
630int i915_batchbuffer(struct drm_device *dev, void *data,
631			    struct drm_file *file_priv)
632{
633	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
634	drm_i915_sarea_t *sarea_priv;
635	drm_i915_batchbuffer_t *batch = data;
636	struct drm_clip_rect *cliprects;
637	size_t cliplen;
638	int ret;
639
640	if (!dev_priv->allow_batchbuffer) {
641		DRM_ERROR("Batchbuffer ioctl disabled\n");
642		return -EINVAL;
643	}
644	DRM_UNLOCK(dev);
645
646	DRM_DEBUG("i915 batchbuffer, start %x used %d cliprects %d\n",
647		  batch->start, batch->used, batch->num_cliprects);
648
649	cliplen = batch->num_cliprects * sizeof(struct drm_clip_rect);
650	if (batch->num_cliprects < 0)
651		return -EFAULT;
652	if (batch->num_cliprects != 0) {
653		cliprects = malloc(batch->num_cliprects *
654		    sizeof(struct drm_clip_rect), DRM_MEM_DMA,
655		    M_WAITOK | M_ZERO);
656
657		ret = -copyin(batch->cliprects, cliprects,
658		    batch->num_cliprects * sizeof(struct drm_clip_rect));
659		if (ret != 0) {
660			DRM_LOCK(dev);
661			goto fail_free;
662		}
663	} else
664		cliprects = NULL;
665
666	DRM_LOCK(dev);
667	RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
668	ret = i915_dispatch_batchbuffer(dev, batch, cliprects);
669
670	sarea_priv = (drm_i915_sarea_t *)dev_priv->sarea_priv;
671	if (sarea_priv)
672		sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
673
674fail_free:
675	free(cliprects, DRM_MEM_DMA);
676	return ret;
677}
678
679int i915_cmdbuffer(struct drm_device *dev, void *data,
680			  struct drm_file *file_priv)
681{
682	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
683	drm_i915_sarea_t *sarea_priv;
684	drm_i915_cmdbuffer_t *cmdbuf = data;
685	struct drm_clip_rect *cliprects = NULL;
686	void *batch_data;
687	int ret;
688
689	DRM_DEBUG("i915 cmdbuffer, buf %p sz %d cliprects %d\n",
690		  cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects);
691
692	if (cmdbuf->num_cliprects < 0)
693		return -EINVAL;
694
695	DRM_UNLOCK(dev);
696
697	batch_data = malloc(cmdbuf->sz, DRM_MEM_DMA, M_WAITOK);
698
699	ret = -copyin(cmdbuf->buf, batch_data, cmdbuf->sz);
700	if (ret != 0) {
701		DRM_LOCK(dev);
702		goto fail_batch_free;
703	}
704
705	if (cmdbuf->num_cliprects) {
706		cliprects = malloc(cmdbuf->num_cliprects *
707		    sizeof(struct drm_clip_rect), DRM_MEM_DMA,
708		    M_WAITOK | M_ZERO);
709		ret = -copyin(cmdbuf->cliprects, cliprects,
710		    cmdbuf->num_cliprects * sizeof(struct drm_clip_rect));
711		if (ret != 0) {
712			DRM_LOCK(dev);
713			goto fail_clip_free;
714		}
715	}
716
717	DRM_LOCK(dev);
718	RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
719	ret = i915_dispatch_cmdbuffer(dev, cmdbuf, cliprects, batch_data);
720	if (ret) {
721		DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
722		goto fail_clip_free;
723	}
724
725	sarea_priv = (drm_i915_sarea_t *)dev_priv->sarea_priv;
726	if (sarea_priv)
727		sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
728
729fail_clip_free:
730	free(cliprects, DRM_MEM_DMA);
731fail_batch_free:
732	free(batch_data, DRM_MEM_DMA);
733	return ret;
734}
735
736static int i915_flip_bufs(struct drm_device *dev, void *data,
737			  struct drm_file *file_priv)
738{
739	int ret;
740
741	DRM_DEBUG("%s\n", __func__);
742
743	RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
744
745	ret = i915_dispatch_flip(dev);
746
747	return ret;
748}
749
750int i915_getparam(struct drm_device *dev, void *data,
751			 struct drm_file *file_priv)
752{
753	drm_i915_private_t *dev_priv = dev->dev_private;
754	drm_i915_getparam_t *param = data;
755	int value;
756
757	if (!dev_priv) {
758		DRM_ERROR("called with no initialization\n");
759		return -EINVAL;
760	}
761
762	switch (param->param) {
763	case I915_PARAM_IRQ_ACTIVE:
764		value = dev->irq_enabled ? 1 : 0;
765		break;
766	case I915_PARAM_ALLOW_BATCHBUFFER:
767		value = dev_priv->allow_batchbuffer ? 1 : 0;
768		break;
769	case I915_PARAM_LAST_DISPATCH:
770		value = READ_BREADCRUMB(dev_priv);
771		break;
772	case I915_PARAM_CHIPSET_ID:
773		value = dev->pci_device;
774		break;
775	case I915_PARAM_HAS_GEM:
776		value = 1;
777		break;
778	case I915_PARAM_NUM_FENCES_AVAIL:
779		value = dev_priv->num_fence_regs - dev_priv->fence_reg_start;
780		break;
781	case I915_PARAM_HAS_OVERLAY:
782		value = dev_priv->overlay ? 1 : 0;
783		break;
784	case I915_PARAM_HAS_PAGEFLIPPING:
785		value = 1;
786		break;
787	case I915_PARAM_HAS_EXECBUF2:
788		value = 1;
789		break;
790	case I915_PARAM_HAS_BSD:
791		value = HAS_BSD(dev);
792		break;
793	case I915_PARAM_HAS_BLT:
794		value = HAS_BLT(dev);
795		break;
796	case I915_PARAM_HAS_RELAXED_FENCING:
797		value = 1;
798		break;
799	case I915_PARAM_HAS_COHERENT_RINGS:
800		value = 1;
801		break;
802	case I915_PARAM_HAS_EXEC_CONSTANTS:
803		value = INTEL_INFO(dev)->gen >= 4;
804		break;
805	case I915_PARAM_HAS_RELAXED_DELTA:
806		value = 1;
807		break;
808	case I915_PARAM_HAS_GEN7_SOL_RESET:
809		value = 1;
810		break;
811	case I915_PARAM_HAS_LLC:
812		value = HAS_LLC(dev);
813		break;
814	default:
815		DRM_DEBUG_DRIVER("Unknown parameter %d\n",
816				 param->param);
817		return -EINVAL;
818	}
819
820	if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) {
821		DRM_ERROR("DRM_COPY_TO_USER failed\n");
822		return -EFAULT;
823	}
824
825	return 0;
826}
827
828static int i915_setparam(struct drm_device *dev, void *data,
829			 struct drm_file *file_priv)
830{
831	drm_i915_private_t *dev_priv = dev->dev_private;
832	drm_i915_setparam_t *param = data;
833
834	if (!dev_priv) {
835		DRM_ERROR("called with no initialization\n");
836		return -EINVAL;
837	}
838
839	switch (param->param) {
840	case I915_SETPARAM_USE_MI_BATCHBUFFER_START:
841		break;
842	case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY:
843		dev_priv->tex_lru_log_granularity = param->value;
844		break;
845	case I915_SETPARAM_ALLOW_BATCHBUFFER:
846		dev_priv->allow_batchbuffer = param->value;
847		break;
848	case I915_SETPARAM_NUM_USED_FENCES:
849		if (param->value > dev_priv->num_fence_regs ||
850		    param->value < 0)
851			return -EINVAL;
852		/* Userspace can use first N regs */
853		dev_priv->fence_reg_start = param->value;
854		break;
855	default:
856		DRM_DEBUG("unknown parameter %d\n", param->param);
857		return -EINVAL;
858	}
859
860	return 0;
861}
862
863static int i915_set_status_page(struct drm_device *dev, void *data,
864				struct drm_file *file_priv)
865{
866	drm_i915_private_t *dev_priv = dev->dev_private;
867	drm_i915_hws_addr_t *hws = data;
868	struct intel_ring_buffer *ring = LP_RING(dev_priv);
869
870	if (!I915_NEED_GFX_HWS(dev))
871		return -EINVAL;
872
873	if (!dev_priv) {
874		DRM_ERROR("called with no initialization\n");
875		return -EINVAL;
876	}
877
878	DRM_DEBUG("set status page addr 0x%08x\n", (u32)hws->addr);
879	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
880		DRM_ERROR("tried to set status page when mode setting active\n");
881		return 0;
882	}
883
884	ring->status_page.gfx_addr = dev_priv->status_gfx_addr =
885	    hws->addr & (0x1ffff<<12);
886
887	dev_priv->hws_map.offset = dev->agp->base + hws->addr;
888	dev_priv->hws_map.size = 4*1024;
889	dev_priv->hws_map.type = 0;
890	dev_priv->hws_map.flags = 0;
891	dev_priv->hws_map.mtrr = 0;
892
893	drm_core_ioremap_wc(&dev_priv->hws_map, dev);
894	if (dev_priv->hws_map.virtual == NULL) {
895		i915_dma_cleanup(dev);
896		ring->status_page.gfx_addr = dev_priv->status_gfx_addr = 0;
897		DRM_ERROR("can not ioremap virtual address for"
898				" G33 hw status page\n");
899		return -ENOMEM;
900	}
901	ring->status_page.page_addr = dev_priv->hw_status_page =
902	    dev_priv->hws_map.virtual;
903
904	memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
905	I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
906	DRM_DEBUG("load hws HWS_PGA with gfx mem 0x%x\n",
907			dev_priv->status_gfx_addr);
908	DRM_DEBUG("load hws at %p\n", dev_priv->hw_status_page);
909	return 0;
910}
911
912static bool
913intel_enable_ppgtt(struct drm_device *dev)
914{
915	if (i915_enable_ppgtt >= 0)
916		return i915_enable_ppgtt;
917
918	/* Disable ppgtt on SNB if VT-d is on. */
919	if (INTEL_INFO(dev)->gen == 6 && intel_iommu_enabled)
920		return false;
921
922	return true;
923}
924
925static int
926i915_load_gem_init(struct drm_device *dev)
927{
928	struct drm_i915_private *dev_priv = dev->dev_private;
929	unsigned long prealloc_size, gtt_size, mappable_size;
930	int ret;
931
932	prealloc_size = dev_priv->mm.gtt.stolen_size;
933	gtt_size = dev_priv->mm.gtt.gtt_total_entries << PAGE_SHIFT;
934	mappable_size = dev_priv->mm.gtt.gtt_mappable_entries << PAGE_SHIFT;
935
936	/* Basic memrange allocator for stolen space */
937	drm_mm_init(&dev_priv->mm.stolen, 0, prealloc_size);
938
939	DRM_LOCK(dev);
940	if (intel_enable_ppgtt(dev) && HAS_ALIASING_PPGTT(dev)) {
941		/* PPGTT pdes are stolen from global gtt ptes, so shrink the
942		 * aperture accordingly when using aliasing ppgtt. */
943		gtt_size -= I915_PPGTT_PD_ENTRIES*PAGE_SIZE;
944		/* For paranoia keep the guard page in between. */
945		gtt_size -= PAGE_SIZE;
946
947		i915_gem_do_init(dev, 0, mappable_size, gtt_size);
948
949		ret = i915_gem_init_aliasing_ppgtt(dev);
950		if (ret) {
951			DRM_UNLOCK(dev);
952			return ret;
953		}
954	} else {
955		/* Let GEM Manage all of the aperture.
956		 *
957		 * However, leave one page at the end still bound to the scratch
958		 * page.  There are a number of places where the hardware
959		 * apparently prefetches past the end of the object, and we've
960		 * seen multiple hangs with the GPU head pointer stuck in a
961		 * batchbuffer bound at the last page of the aperture.  One page
962		 * should be enough to keep any prefetching inside of the
963		 * aperture.
964		 */
965		i915_gem_do_init(dev, 0, mappable_size, gtt_size - PAGE_SIZE);
966	}
967
968	ret = i915_gem_init_hw(dev);
969	DRM_UNLOCK(dev);
970	if (ret != 0) {
971		i915_gem_cleanup_aliasing_ppgtt(dev);
972		return (ret);
973	}
974
975#if 0
976	/* Try to set up FBC with a reasonable compressed buffer size */
977	if (I915_HAS_FBC(dev) && i915_powersave) {
978		int cfb_size;
979
980		/* Leave 1M for line length buffer & misc. */
981
982		/* Try to get a 32M buffer... */
983		if (prealloc_size > (36*1024*1024))
984			cfb_size = 32*1024*1024;
985		else /* fall back to 7/8 of the stolen space */
986			cfb_size = prealloc_size * 7 / 8;
987		i915_setup_compression(dev, cfb_size);
988	}
989#endif
990
991	/* Allow hardware batchbuffers unless told otherwise. */
992	dev_priv->allow_batchbuffer = 1;
993	return 0;
994}
995
996static int
997i915_load_modeset_init(struct drm_device *dev)
998{
999	struct drm_i915_private *dev_priv = dev->dev_private;
1000	int ret;
1001
1002	ret = intel_parse_bios(dev);
1003	if (ret)
1004		DRM_INFO("failed to find VBIOS tables\n");
1005
1006#if 0
1007	intel_register_dsm_handler();
1008#endif
1009
1010	/* IIR "flip pending" bit means done if this bit is set */
1011	if (IS_GEN3(dev) && (I915_READ(ECOSKPD) & ECO_FLIP_DONE))
1012		dev_priv->flip_pending_is_done = true;
1013
1014	intel_modeset_init(dev);
1015
1016	ret = i915_load_gem_init(dev);
1017	if (ret != 0)
1018		goto cleanup_gem;
1019
1020	intel_modeset_gem_init(dev);
1021
1022	ret = drm_irq_install(dev);
1023	if (ret)
1024		goto cleanup_gem;
1025
1026	dev->vblank_disable_allowed = 1;
1027
1028	ret = intel_fbdev_init(dev);
1029	if (ret)
1030		goto cleanup_gem;
1031
1032	drm_kms_helper_poll_init(dev);
1033
1034	/* We're off and running w/KMS */
1035	dev_priv->mm.suspended = 0;
1036
1037	return (0);
1038
1039cleanup_gem:
1040	DRM_LOCK(dev);
1041	i915_gem_cleanup_ringbuffer(dev);
1042	DRM_UNLOCK(dev);
1043	i915_gem_cleanup_aliasing_ppgtt(dev);
1044	return (ret);
1045}
1046
1047static int
1048i915_get_bridge_dev(struct drm_device *dev)
1049{
1050	struct drm_i915_private *dev_priv;
1051
1052	dev_priv = dev->dev_private;
1053
1054	dev_priv->bridge_dev = intel_gtt_get_bridge_device();
1055	if (dev_priv->bridge_dev == NULL) {
1056		DRM_ERROR("bridge device not found\n");
1057		return (-1);
1058	}
1059	return (0);
1060}
1061
1062#define MCHBAR_I915 0x44
1063#define MCHBAR_I965 0x48
1064#define MCHBAR_SIZE (4*4096)
1065
1066#define DEVEN_REG 0x54
1067#define   DEVEN_MCHBAR_EN (1 << 28)
1068
1069/* Allocate space for the MCH regs if needed, return nonzero on error */
1070static int
1071intel_alloc_mchbar_resource(struct drm_device *dev)
1072{
1073	drm_i915_private_t *dev_priv;
1074	device_t vga;
1075	int reg;
1076	u32 temp_lo, temp_hi;
1077	u64 mchbar_addr, temp;
1078
1079	dev_priv = dev->dev_private;
1080	reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
1081
1082	if (INTEL_INFO(dev)->gen >= 4)
1083		temp_hi = pci_read_config(dev_priv->bridge_dev, reg + 4, 4);
1084	else
1085		temp_hi = 0;
1086	temp_lo = pci_read_config(dev_priv->bridge_dev, reg, 4);
1087	mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
1088
1089	/* If ACPI doesn't have it, assume we need to allocate it ourselves */
1090#ifdef XXX_CONFIG_PNP
1091	if (mchbar_addr &&
1092	    pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE))
1093		return 0;
1094#endif
1095
1096	/* Get some space for it */
1097	vga = device_get_parent(dev->device);
1098	dev_priv->mch_res_rid = 0x100;
1099	dev_priv->mch_res = BUS_ALLOC_RESOURCE(device_get_parent(vga),
1100	    dev->device, SYS_RES_MEMORY, &dev_priv->mch_res_rid, 0, ~0UL,
1101	    MCHBAR_SIZE, RF_ACTIVE | RF_SHAREABLE);
1102	if (dev_priv->mch_res == NULL) {
1103		DRM_ERROR("failed mchbar resource alloc\n");
1104		return (-ENOMEM);
1105	}
1106
1107	if (INTEL_INFO(dev)->gen >= 4) {
1108		temp = rman_get_start(dev_priv->mch_res);
1109		temp >>= 32;
1110		pci_write_config(dev_priv->bridge_dev, reg + 4, temp, 4);
1111	}
1112	pci_write_config(dev_priv->bridge_dev, reg,
1113	    rman_get_start(dev_priv->mch_res) & UINT32_MAX, 4);
1114	return (0);
1115}
1116
1117static void
1118intel_setup_mchbar(struct drm_device *dev)
1119{
1120	drm_i915_private_t *dev_priv;
1121	int mchbar_reg;
1122	u32 temp;
1123	bool enabled;
1124
1125	dev_priv = dev->dev_private;
1126	mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
1127
1128	dev_priv->mchbar_need_disable = false;
1129
1130	if (IS_I915G(dev) || IS_I915GM(dev)) {
1131		temp = pci_read_config(dev_priv->bridge_dev, DEVEN_REG, 4);
1132		enabled = (temp & DEVEN_MCHBAR_EN) != 0;
1133	} else {
1134		temp = pci_read_config(dev_priv->bridge_dev, mchbar_reg, 4);
1135		enabled = temp & 1;
1136	}
1137
1138	/* If it's already enabled, don't have to do anything */
1139	if (enabled) {
1140		DRM_DEBUG("mchbar already enabled\n");
1141		return;
1142	}
1143
1144	if (intel_alloc_mchbar_resource(dev))
1145		return;
1146
1147	dev_priv->mchbar_need_disable = true;
1148
1149	/* Space is allocated or reserved, so enable it. */
1150	if (IS_I915G(dev) || IS_I915GM(dev)) {
1151		pci_write_config(dev_priv->bridge_dev, DEVEN_REG,
1152		    temp | DEVEN_MCHBAR_EN, 4);
1153	} else {
1154		temp = pci_read_config(dev_priv->bridge_dev, mchbar_reg, 4);
1155		pci_write_config(dev_priv->bridge_dev, mchbar_reg, temp | 1, 4);
1156	}
1157}
1158
1159static void
1160intel_teardown_mchbar(struct drm_device *dev)
1161{
1162	drm_i915_private_t *dev_priv;
1163	device_t vga;
1164	int mchbar_reg;
1165	u32 temp;
1166
1167	dev_priv = dev->dev_private;
1168	mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
1169
1170	if (dev_priv->mchbar_need_disable) {
1171		if (IS_I915G(dev) || IS_I915GM(dev)) {
1172			temp = pci_read_config(dev_priv->bridge_dev,
1173			    DEVEN_REG, 4);
1174			temp &= ~DEVEN_MCHBAR_EN;
1175			pci_write_config(dev_priv->bridge_dev, DEVEN_REG,
1176			    temp, 4);
1177		} else {
1178			temp = pci_read_config(dev_priv->bridge_dev,
1179			    mchbar_reg, 4);
1180			temp &= ~1;
1181			pci_write_config(dev_priv->bridge_dev, mchbar_reg,
1182			    temp, 4);
1183		}
1184	}
1185
1186	if (dev_priv->mch_res != NULL) {
1187		vga = device_get_parent(dev->device);
1188		BUS_DEACTIVATE_RESOURCE(device_get_parent(vga), dev->device,
1189		    SYS_RES_MEMORY, dev_priv->mch_res_rid, dev_priv->mch_res);
1190		BUS_RELEASE_RESOURCE(device_get_parent(vga), dev->device,
1191		    SYS_RES_MEMORY, dev_priv->mch_res_rid, dev_priv->mch_res);
1192		dev_priv->mch_res = NULL;
1193	}
1194}
1195
1196int
1197i915_driver_load(struct drm_device *dev, unsigned long flags)
1198{
1199	struct drm_i915_private *dev_priv = dev->dev_private;
1200	unsigned long base, size;
1201	int mmio_bar, ret;
1202
1203	ret = 0;
1204
1205	/* i915 has 4 more counters */
1206	dev->counters += 4;
1207	dev->types[6] = _DRM_STAT_IRQ;
1208	dev->types[7] = _DRM_STAT_PRIMARY;
1209	dev->types[8] = _DRM_STAT_SECONDARY;
1210	dev->types[9] = _DRM_STAT_DMA;
1211
1212	dev_priv = malloc(sizeof(drm_i915_private_t), DRM_MEM_DRIVER,
1213	    M_ZERO | M_WAITOK);
1214	if (dev_priv == NULL)
1215		return -ENOMEM;
1216
1217	dev->dev_private = (void *)dev_priv;
1218	dev_priv->dev = dev;
1219	dev_priv->info = i915_get_device_id(dev->pci_device);
1220
1221	if (i915_get_bridge_dev(dev)) {
1222		free(dev_priv, DRM_MEM_DRIVER);
1223		return (-EIO);
1224	}
1225	dev_priv->mm.gtt = intel_gtt_get();
1226
1227	/* Add register map (needed for suspend/resume) */
1228	mmio_bar = IS_GEN2(dev) ? 1 : 0;
1229	base = drm_get_resource_start(dev, mmio_bar);
1230	size = drm_get_resource_len(dev, mmio_bar);
1231
1232	ret = drm_addmap(dev, base, size, _DRM_REGISTERS,
1233	    _DRM_KERNEL | _DRM_DRIVER, &dev_priv->mmio_map);
1234
1235	dev_priv->tq = taskqueue_create("915", M_WAITOK,
1236	    taskqueue_thread_enqueue, &dev_priv->tq);
1237	taskqueue_start_threads(&dev_priv->tq, 1, PWAIT, "i915 taskq");
1238	mtx_init(&dev_priv->gt_lock, "915gt", NULL, MTX_DEF);
1239	mtx_init(&dev_priv->error_lock, "915err", NULL, MTX_DEF);
1240	mtx_init(&dev_priv->error_completion_lock, "915cmp", NULL, MTX_DEF);
1241	mtx_init(&dev_priv->rps_lock, "915rps", NULL, MTX_DEF);
1242
1243	dev_priv->has_gem = 1;
1244	intel_irq_init(dev);
1245
1246	intel_setup_mchbar(dev);
1247	intel_setup_gmbus(dev);
1248	intel_opregion_setup(dev);
1249
1250	intel_setup_bios(dev);
1251
1252	i915_gem_load(dev);
1253
1254	/* Init HWS */
1255	if (!I915_NEED_GFX_HWS(dev)) {
1256		ret = i915_init_phys_hws(dev);
1257		if (ret != 0) {
1258			drm_rmmap(dev, dev_priv->mmio_map);
1259			drm_free(dev_priv, sizeof(struct drm_i915_private),
1260			    DRM_MEM_DRIVER);
1261			return ret;
1262		}
1263	}
1264
1265	if (IS_PINEVIEW(dev))
1266		i915_pineview_get_mem_freq(dev);
1267	else if (IS_GEN5(dev))
1268		i915_ironlake_get_mem_freq(dev);
1269
1270	mtx_init(&dev_priv->irq_lock, "userirq", NULL, MTX_DEF);
1271
1272	if (IS_IVYBRIDGE(dev))
1273		dev_priv->num_pipe = 3;
1274	else if (IS_MOBILE(dev) || !IS_GEN2(dev))
1275		dev_priv->num_pipe = 2;
1276	else
1277		dev_priv->num_pipe = 1;
1278
1279	ret = drm_vblank_init(dev, dev_priv->num_pipe);
1280	if (ret)
1281		goto out_gem_unload;
1282
1283	/* Start out suspended */
1284	dev_priv->mm.suspended = 1;
1285
1286	intel_detect_pch(dev);
1287
1288	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1289		DRM_UNLOCK(dev);
1290		ret = i915_load_modeset_init(dev);
1291		DRM_LOCK(dev);
1292		if (ret < 0) {
1293			DRM_ERROR("failed to init modeset\n");
1294			goto out_gem_unload;
1295		}
1296	}
1297
1298	intel_opregion_init(dev);
1299
1300	callout_init(&dev_priv->hangcheck_timer, 1);
1301	callout_reset(&dev_priv->hangcheck_timer, DRM_I915_HANGCHECK_PERIOD,
1302	    i915_hangcheck_elapsed, dev);
1303
1304	if (IS_GEN5(dev)) {
1305		mtx_lock(&mchdev_lock);
1306		i915_mch_dev = dev_priv;
1307		dev_priv->mchdev_lock = &mchdev_lock;
1308		mtx_unlock(&mchdev_lock);
1309	}
1310
1311	return (0);
1312
1313out_gem_unload:
1314	/* XXXKIB */
1315	(void) i915_driver_unload_int(dev, true);
1316	return (ret);
1317}
1318
1319static int
1320i915_driver_unload_int(struct drm_device *dev, bool locked)
1321{
1322	struct drm_i915_private *dev_priv = dev->dev_private;
1323	int ret;
1324
1325	if (!locked)
1326		DRM_LOCK(dev);
1327	ret = i915_gpu_idle(dev, true);
1328	if (ret)
1329		DRM_ERROR("failed to idle hardware: %d\n", ret);
1330	if (!locked)
1331		DRM_UNLOCK(dev);
1332
1333	i915_free_hws(dev);
1334
1335	intel_teardown_mchbar(dev);
1336
1337	if (locked)
1338		DRM_UNLOCK(dev);
1339	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1340		intel_fbdev_fini(dev);
1341		intel_modeset_cleanup(dev);
1342	}
1343
1344	/* Free error state after interrupts are fully disabled. */
1345	callout_stop(&dev_priv->hangcheck_timer);
1346	callout_drain(&dev_priv->hangcheck_timer);
1347
1348	i915_destroy_error_state(dev);
1349
1350	intel_opregion_fini(dev);
1351
1352	if (locked)
1353		DRM_LOCK(dev);
1354
1355	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1356		if (!locked)
1357			DRM_LOCK(dev);
1358		i915_gem_free_all_phys_object(dev);
1359		i915_gem_cleanup_ringbuffer(dev);
1360		if (!locked)
1361			DRM_UNLOCK(dev);
1362		i915_gem_cleanup_aliasing_ppgtt(dev);
1363#if 1
1364		KIB_NOTYET();
1365#else
1366		if (I915_HAS_FBC(dev) && i915_powersave)
1367			i915_cleanup_compression(dev);
1368#endif
1369		drm_mm_takedown(&dev_priv->mm.stolen);
1370
1371		intel_cleanup_overlay(dev);
1372
1373		if (!I915_NEED_GFX_HWS(dev))
1374			i915_free_hws(dev);
1375	}
1376
1377	i915_gem_unload(dev);
1378
1379	mtx_destroy(&dev_priv->irq_lock);
1380
1381	if (dev_priv->tq != NULL)
1382		taskqueue_free(dev_priv->tq);
1383
1384	bus_generic_detach(dev->device);
1385	drm_rmmap(dev, dev_priv->mmio_map);
1386	intel_teardown_gmbus(dev);
1387
1388	mtx_destroy(&dev_priv->error_lock);
1389	mtx_destroy(&dev_priv->error_completion_lock);
1390	mtx_destroy(&dev_priv->rps_lock);
1391	drm_free(dev->dev_private, sizeof(drm_i915_private_t),
1392	    DRM_MEM_DRIVER);
1393
1394	return (0);
1395}
1396
1397int
1398i915_driver_unload(struct drm_device *dev)
1399{
1400
1401	return (i915_driver_unload_int(dev, true));
1402}
1403
1404int
1405i915_driver_open(struct drm_device *dev, struct drm_file *file_priv)
1406{
1407	struct drm_i915_file_private *i915_file_priv;
1408
1409	i915_file_priv = malloc(sizeof(*i915_file_priv), DRM_MEM_FILES,
1410	    M_WAITOK | M_ZERO);
1411
1412	mtx_init(&i915_file_priv->mm.lck, "915fp", NULL, MTX_DEF);
1413	INIT_LIST_HEAD(&i915_file_priv->mm.request_list);
1414	file_priv->driver_priv = i915_file_priv;
1415
1416	return (0);
1417}
1418
1419void
1420i915_driver_lastclose(struct drm_device * dev)
1421{
1422	drm_i915_private_t *dev_priv = dev->dev_private;
1423
1424	if (!dev_priv || drm_core_check_feature(dev, DRIVER_MODESET)) {
1425#if 1
1426		KIB_NOTYET();
1427#else
1428		drm_fb_helper_restore();
1429		vga_switcheroo_process_delayed_switch();
1430#endif
1431		return;
1432	}
1433	i915_gem_lastclose(dev);
1434	i915_dma_cleanup(dev);
1435}
1436
1437void i915_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
1438{
1439
1440	i915_gem_release(dev, file_priv);
1441}
1442
1443void i915_driver_postclose(struct drm_device *dev, struct drm_file *file_priv)
1444{
1445	struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
1446
1447	mtx_destroy(&i915_file_priv->mm.lck);
1448	drm_free(i915_file_priv, sizeof(*i915_file_priv), DRM_MEM_FILES);
1449}
1450
1451struct drm_ioctl_desc i915_ioctls[] = {
1452	DRM_IOCTL_DEF(DRM_I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1453	DRM_IOCTL_DEF(DRM_I915_FLUSH, i915_flush_ioctl, DRM_AUTH),
1454	DRM_IOCTL_DEF(DRM_I915_FLIP, i915_flip_bufs, DRM_AUTH),
1455	DRM_IOCTL_DEF(DRM_I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH),
1456	DRM_IOCTL_DEF(DRM_I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH),
1457	DRM_IOCTL_DEF(DRM_I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH),
1458	DRM_IOCTL_DEF(DRM_I915_GETPARAM, i915_getparam, DRM_AUTH),
1459	DRM_IOCTL_DEF(DRM_I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1460	DRM_IOCTL_DEF(DRM_I915_ALLOC, drm_noop, DRM_AUTH),
1461	DRM_IOCTL_DEF(DRM_I915_FREE, drm_noop, DRM_AUTH),
1462	DRM_IOCTL_DEF(DRM_I915_INIT_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1463	DRM_IOCTL_DEF(DRM_I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH),
1464	DRM_IOCTL_DEF(DRM_I915_DESTROY_HEAP,  drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ),
1465	DRM_IOCTL_DEF(DRM_I915_SET_VBLANK_PIPE,  i915_vblank_pipe_set, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ),
1466	DRM_IOCTL_DEF(DRM_I915_GET_VBLANK_PIPE,  i915_vblank_pipe_get, DRM_AUTH ),
1467	DRM_IOCTL_DEF(DRM_I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH),
1468	DRM_IOCTL_DEF(DRM_I915_HWS_ADDR, i915_set_status_page, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1469	DRM_IOCTL_DEF(DRM_I915_GEM_INIT, i915_gem_init_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1470	DRM_IOCTL_DEF(DRM_I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH | DRM_UNLOCKED),
1471	DRM_IOCTL_DEF(DRM_I915_GEM_EXECBUFFER2, i915_gem_execbuffer2, DRM_AUTH | DRM_UNLOCKED),
1472	DRM_IOCTL_DEF(DRM_I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
1473	DRM_IOCTL_DEF(DRM_I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
1474	DRM_IOCTL_DEF(DRM_I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH|DRM_UNLOCKED),
1475	DRM_IOCTL_DEF(DRM_I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH),
1476	DRM_IOCTL_DEF(DRM_I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1477	DRM_IOCTL_DEF(DRM_I915_GEM_LEAVEVT, i915_gem_leavevt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1478	DRM_IOCTL_DEF(DRM_I915_GEM_CREATE, i915_gem_create_ioctl, 0),
1479	DRM_IOCTL_DEF(DRM_I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_UNLOCKED),
1480	DRM_IOCTL_DEF(DRM_I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_UNLOCKED),
1481	DRM_IOCTL_DEF(DRM_I915_GEM_MMAP, i915_gem_mmap_ioctl, 0),
1482	DRM_IOCTL_DEF(DRM_I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, DRM_UNLOCKED),
1483	DRM_IOCTL_DEF(DRM_I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_UNLOCKED),
1484	DRM_IOCTL_DEF(DRM_I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_UNLOCKED),
1485	DRM_IOCTL_DEF(DRM_I915_GEM_SET_TILING, i915_gem_set_tiling, 0),
1486	DRM_IOCTL_DEF(DRM_I915_GEM_GET_TILING, i915_gem_get_tiling, 0),
1487	DRM_IOCTL_DEF(DRM_I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, DRM_UNLOCKED),
1488	DRM_IOCTL_DEF(DRM_I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id, DRM_UNLOCKED),
1489	DRM_IOCTL_DEF(DRM_I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_UNLOCKED),
1490	DRM_IOCTL_DEF(DRM_I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
1491	DRM_IOCTL_DEF(DRM_I915_OVERLAY_ATTRS, intel_overlay_attrs, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
1492	DRM_IOCTL_DEF(DRM_I915_SET_SPRITE_COLORKEY, intel_sprite_set_colorkey, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
1493	DRM_IOCTL_DEF(DRM_I915_GET_SPRITE_COLORKEY, intel_sprite_get_colorkey, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
1494};
1495
1496#ifdef COMPAT_FREEBSD32
1497extern drm_ioctl_desc_t i915_compat_ioctls[];
1498extern int i915_compat_ioctls_nr;
1499#endif
1500
1501struct drm_driver_info i915_driver_info = {
1502	.driver_features =   DRIVER_USE_AGP | DRIVER_REQUIRE_AGP |
1503	    DRIVER_USE_MTRR | DRIVER_HAVE_IRQ | DRIVER_LOCKLESS_IRQ |
1504	    DRIVER_GEM /*| DRIVER_MODESET*/,
1505
1506	.buf_priv_size	= sizeof(drm_i915_private_t),
1507	.load		= i915_driver_load,
1508	.open		= i915_driver_open,
1509	.unload		= i915_driver_unload,
1510	.preclose	= i915_driver_preclose,
1511	.lastclose	= i915_driver_lastclose,
1512	.postclose	= i915_driver_postclose,
1513	.device_is_agp	= i915_driver_device_is_agp,
1514	.gem_init_object = i915_gem_init_object,
1515	.gem_free_object = i915_gem_free_object,
1516	.gem_pager_ops	= &i915_gem_pager_ops,
1517	.dumb_create	= i915_gem_dumb_create,
1518	.dumb_map_offset = i915_gem_mmap_gtt,
1519	.dumb_destroy	= i915_gem_dumb_destroy,
1520	.sysctl_init	= i915_sysctl_init,
1521	.sysctl_cleanup	= i915_sysctl_cleanup,
1522
1523	.ioctls		= i915_ioctls,
1524#ifdef COMPAT_FREEBSD32
1525	.compat_ioctls  = i915_compat_ioctls,
1526	.compat_ioctls_nr = &i915_compat_ioctls_nr,
1527#endif
1528	.max_ioctl	= DRM_ARRAY_SIZE(i915_ioctls),
1529
1530	.name		= DRIVER_NAME,
1531	.desc		= DRIVER_DESC,
1532	.date		= DRIVER_DATE,
1533	.major		= DRIVER_MAJOR,
1534	.minor		= DRIVER_MINOR,
1535	.patchlevel	= DRIVER_PATCHLEVEL,
1536};
1537
1538/**
1539 * Determine if the device really is AGP or not.
1540 *
1541 * All Intel graphics chipsets are treated as AGP, even if they are really
1542 * built-in.
1543 *
1544 * \param dev   The device to be tested.
1545 *
1546 * \returns
1547 * A value of 1 is always retured to indictate every i9x5 is AGP.
1548 */
1549int i915_driver_device_is_agp(struct drm_device * dev)
1550{
1551	return 1;
1552}
1553
1554static void i915_pineview_get_mem_freq(struct drm_device *dev)
1555{
1556	drm_i915_private_t *dev_priv = dev->dev_private;
1557	u32 tmp;
1558
1559	tmp = I915_READ(CLKCFG);
1560
1561	switch (tmp & CLKCFG_FSB_MASK) {
1562	case CLKCFG_FSB_533:
1563		dev_priv->fsb_freq = 533; /* 133*4 */
1564		break;
1565	case CLKCFG_FSB_800:
1566		dev_priv->fsb_freq = 800; /* 200*4 */
1567		break;
1568	case CLKCFG_FSB_667:
1569		dev_priv->fsb_freq =  667; /* 167*4 */
1570		break;
1571	case CLKCFG_FSB_400:
1572		dev_priv->fsb_freq = 400; /* 100*4 */
1573		break;
1574	}
1575
1576	switch (tmp & CLKCFG_MEM_MASK) {
1577	case CLKCFG_MEM_533:
1578		dev_priv->mem_freq = 533;
1579		break;
1580	case CLKCFG_MEM_667:
1581		dev_priv->mem_freq = 667;
1582		break;
1583	case CLKCFG_MEM_800:
1584		dev_priv->mem_freq = 800;
1585		break;
1586	}
1587
1588	/* detect pineview DDR3 setting */
1589	tmp = I915_READ(CSHRDDR3CTL);
1590	dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
1591}
1592
1593static void i915_ironlake_get_mem_freq(struct drm_device *dev)
1594{
1595	drm_i915_private_t *dev_priv = dev->dev_private;
1596	u16 ddrpll, csipll;
1597
1598	ddrpll = I915_READ16(DDRMPLL1);
1599	csipll = I915_READ16(CSIPLL0);
1600
1601	switch (ddrpll & 0xff) {
1602	case 0xc:
1603		dev_priv->mem_freq = 800;
1604		break;
1605	case 0x10:
1606		dev_priv->mem_freq = 1066;
1607		break;
1608	case 0x14:
1609		dev_priv->mem_freq = 1333;
1610		break;
1611	case 0x18:
1612		dev_priv->mem_freq = 1600;
1613		break;
1614	default:
1615		DRM_DEBUG("unknown memory frequency 0x%02x\n",
1616				 ddrpll & 0xff);
1617		dev_priv->mem_freq = 0;
1618		break;
1619	}
1620
1621	dev_priv->r_t = dev_priv->mem_freq;
1622
1623	switch (csipll & 0x3ff) {
1624	case 0x00c:
1625		dev_priv->fsb_freq = 3200;
1626		break;
1627	case 0x00e:
1628		dev_priv->fsb_freq = 3733;
1629		break;
1630	case 0x010:
1631		dev_priv->fsb_freq = 4266;
1632		break;
1633	case 0x012:
1634		dev_priv->fsb_freq = 4800;
1635		break;
1636	case 0x014:
1637		dev_priv->fsb_freq = 5333;
1638		break;
1639	case 0x016:
1640		dev_priv->fsb_freq = 5866;
1641		break;
1642	case 0x018:
1643		dev_priv->fsb_freq = 6400;
1644		break;
1645	default:
1646		DRM_DEBUG("unknown fsb frequency 0x%04x\n",
1647				 csipll & 0x3ff);
1648		dev_priv->fsb_freq = 0;
1649		break;
1650	}
1651
1652	if (dev_priv->fsb_freq == 3200) {
1653		dev_priv->c_m = 0;
1654	} else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
1655		dev_priv->c_m = 1;
1656	} else {
1657		dev_priv->c_m = 2;
1658	}
1659}
1660
1661static const struct cparams {
1662	u16 i;
1663	u16 t;
1664	u16 m;
1665	u16 c;
1666} cparams[] = {
1667	{ 1, 1333, 301, 28664 },
1668	{ 1, 1066, 294, 24460 },
1669	{ 1, 800, 294, 25192 },
1670	{ 0, 1333, 276, 27605 },
1671	{ 0, 1066, 276, 27605 },
1672	{ 0, 800, 231, 23784 },
1673};
1674
1675unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
1676{
1677	u64 total_count, diff, ret;
1678	u32 count1, count2, count3, m = 0, c = 0;
1679	unsigned long now = jiffies_to_msecs(jiffies), diff1;
1680	int i;
1681
1682	diff1 = now - dev_priv->last_time1;
1683	/*
1684	 * sysctl(8) reads the value of sysctl twice in rapid
1685	 * succession.  There is high chance that it happens in the
1686	 * same timer tick.  Use the cached value to not divide by
1687	 * zero and give the hw a chance to gather more samples.
1688	 */
1689	if (diff1 <= 10)
1690		return (dev_priv->chipset_power);
1691
1692	count1 = I915_READ(DMIEC);
1693	count2 = I915_READ(DDREC);
1694	count3 = I915_READ(CSIEC);
1695
1696	total_count = count1 + count2 + count3;
1697
1698	/* FIXME: handle per-counter overflow */
1699	if (total_count < dev_priv->last_count1) {
1700		diff = ~0UL - dev_priv->last_count1;
1701		diff += total_count;
1702	} else {
1703		diff = total_count - dev_priv->last_count1;
1704	}
1705
1706	for (i = 0; i < DRM_ARRAY_SIZE(cparams); i++) {
1707		if (cparams[i].i == dev_priv->c_m &&
1708		    cparams[i].t == dev_priv->r_t) {
1709			m = cparams[i].m;
1710			c = cparams[i].c;
1711			break;
1712		}
1713	}
1714
1715	diff = diff / diff1;
1716	ret = ((m * diff) + c);
1717	ret = ret / 10;
1718
1719	dev_priv->last_count1 = total_count;
1720	dev_priv->last_time1 = now;
1721
1722	dev_priv->chipset_power = ret;
1723	return (ret);
1724}
1725
1726unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
1727{
1728	unsigned long m, x, b;
1729	u32 tsfs;
1730
1731	tsfs = I915_READ(TSFS);
1732
1733	m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
1734	x = I915_READ8(I915_TR1);
1735
1736	b = tsfs & TSFS_INTR_MASK;
1737
1738	return ((m * x) / 127) - b;
1739}
1740
1741static u16 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
1742{
1743	static const struct v_table {
1744		u16 vd; /* in .1 mil */
1745		u16 vm; /* in .1 mil */
1746	} v_table[] = {
1747		{ 0, 0, },
1748		{ 375, 0, },
1749		{ 500, 0, },
1750		{ 625, 0, },
1751		{ 750, 0, },
1752		{ 875, 0, },
1753		{ 1000, 0, },
1754		{ 1125, 0, },
1755		{ 4125, 3000, },
1756		{ 4125, 3000, },
1757		{ 4125, 3000, },
1758		{ 4125, 3000, },
1759		{ 4125, 3000, },
1760		{ 4125, 3000, },
1761		{ 4125, 3000, },
1762		{ 4125, 3000, },
1763		{ 4125, 3000, },
1764		{ 4125, 3000, },
1765		{ 4125, 3000, },
1766		{ 4125, 3000, },
1767		{ 4125, 3000, },
1768		{ 4125, 3000, },
1769		{ 4125, 3000, },
1770		{ 4125, 3000, },
1771		{ 4125, 3000, },
1772		{ 4125, 3000, },
1773		{ 4125, 3000, },
1774		{ 4125, 3000, },
1775		{ 4125, 3000, },
1776		{ 4125, 3000, },
1777		{ 4125, 3000, },
1778		{ 4125, 3000, },
1779		{ 4250, 3125, },
1780		{ 4375, 3250, },
1781		{ 4500, 3375, },
1782		{ 4625, 3500, },
1783		{ 4750, 3625, },
1784		{ 4875, 3750, },
1785		{ 5000, 3875, },
1786		{ 5125, 4000, },
1787		{ 5250, 4125, },
1788		{ 5375, 4250, },
1789		{ 5500, 4375, },
1790		{ 5625, 4500, },
1791		{ 5750, 4625, },
1792		{ 5875, 4750, },
1793		{ 6000, 4875, },
1794		{ 6125, 5000, },
1795		{ 6250, 5125, },
1796		{ 6375, 5250, },
1797		{ 6500, 5375, },
1798		{ 6625, 5500, },
1799		{ 6750, 5625, },
1800		{ 6875, 5750, },
1801		{ 7000, 5875, },
1802		{ 7125, 6000, },
1803		{ 7250, 6125, },
1804		{ 7375, 6250, },
1805		{ 7500, 6375, },
1806		{ 7625, 6500, },
1807		{ 7750, 6625, },
1808		{ 7875, 6750, },
1809		{ 8000, 6875, },
1810		{ 8125, 7000, },
1811		{ 8250, 7125, },
1812		{ 8375, 7250, },
1813		{ 8500, 7375, },
1814		{ 8625, 7500, },
1815		{ 8750, 7625, },
1816		{ 8875, 7750, },
1817		{ 9000, 7875, },
1818		{ 9125, 8000, },
1819		{ 9250, 8125, },
1820		{ 9375, 8250, },
1821		{ 9500, 8375, },
1822		{ 9625, 8500, },
1823		{ 9750, 8625, },
1824		{ 9875, 8750, },
1825		{ 10000, 8875, },
1826		{ 10125, 9000, },
1827		{ 10250, 9125, },
1828		{ 10375, 9250, },
1829		{ 10500, 9375, },
1830		{ 10625, 9500, },
1831		{ 10750, 9625, },
1832		{ 10875, 9750, },
1833		{ 11000, 9875, },
1834		{ 11125, 10000, },
1835		{ 11250, 10125, },
1836		{ 11375, 10250, },
1837		{ 11500, 10375, },
1838		{ 11625, 10500, },
1839		{ 11750, 10625, },
1840		{ 11875, 10750, },
1841		{ 12000, 10875, },
1842		{ 12125, 11000, },
1843		{ 12250, 11125, },
1844		{ 12375, 11250, },
1845		{ 12500, 11375, },
1846		{ 12625, 11500, },
1847		{ 12750, 11625, },
1848		{ 12875, 11750, },
1849		{ 13000, 11875, },
1850		{ 13125, 12000, },
1851		{ 13250, 12125, },
1852		{ 13375, 12250, },
1853		{ 13500, 12375, },
1854		{ 13625, 12500, },
1855		{ 13750, 12625, },
1856		{ 13875, 12750, },
1857		{ 14000, 12875, },
1858		{ 14125, 13000, },
1859		{ 14250, 13125, },
1860		{ 14375, 13250, },
1861		{ 14500, 13375, },
1862		{ 14625, 13500, },
1863		{ 14750, 13625, },
1864		{ 14875, 13750, },
1865		{ 15000, 13875, },
1866		{ 15125, 14000, },
1867		{ 15250, 14125, },
1868		{ 15375, 14250, },
1869		{ 15500, 14375, },
1870		{ 15625, 14500, },
1871		{ 15750, 14625, },
1872		{ 15875, 14750, },
1873		{ 16000, 14875, },
1874		{ 16125, 15000, },
1875	};
1876	if (dev_priv->info->is_mobile)
1877		return v_table[pxvid].vm;
1878	else
1879		return v_table[pxvid].vd;
1880}
1881
1882void i915_update_gfx_val(struct drm_i915_private *dev_priv)
1883{
1884	struct timespec now, diff1;
1885	u64 diff;
1886	unsigned long diffms;
1887	u32 count;
1888
1889	if (dev_priv->info->gen != 5)
1890		return;
1891
1892	nanotime(&now);
1893	diff1 = now;
1894	timespecsub(&diff1, &dev_priv->last_time2);
1895
1896	/* Don't divide by 0 */
1897	diffms = diff1.tv_sec * 1000 + diff1.tv_nsec / 1000000;
1898	if (!diffms)
1899		return;
1900
1901	count = I915_READ(GFXEC);
1902
1903	if (count < dev_priv->last_count2) {
1904		diff = ~0UL - dev_priv->last_count2;
1905		diff += count;
1906	} else {
1907		diff = count - dev_priv->last_count2;
1908	}
1909
1910	dev_priv->last_count2 = count;
1911	dev_priv->last_time2 = now;
1912
1913	/* More magic constants... */
1914	diff = diff * 1181;
1915	diff = diff / (diffms * 10);
1916	dev_priv->gfx_power = diff;
1917}
1918
1919unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
1920{
1921	unsigned long t, corr, state1, corr2, state2;
1922	u32 pxvid, ext_v;
1923
1924	pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->cur_delay * 4));
1925	pxvid = (pxvid >> 24) & 0x7f;
1926	ext_v = pvid_to_extvid(dev_priv, pxvid);
1927
1928	state1 = ext_v;
1929
1930	t = i915_mch_val(dev_priv);
1931
1932	/* Revel in the empirically derived constants */
1933
1934	/* Correction factor in 1/100000 units */
1935	if (t > 80)
1936		corr = ((t * 2349) + 135940);
1937	else if (t >= 50)
1938		corr = ((t * 964) + 29317);
1939	else /* < 50 */
1940		corr = ((t * 301) + 1004);
1941
1942	corr = corr * ((150142 * state1) / 10000 - 78642);
1943	corr /= 100000;
1944	corr2 = (corr * dev_priv->corr);
1945
1946	state2 = (corr2 * state1) / 10000;
1947	state2 /= 100; /* convert to mW */
1948
1949	i915_update_gfx_val(dev_priv);
1950
1951	return dev_priv->gfx_power + state2;
1952}
1953
1954/**
1955 * i915_read_mch_val - return value for IPS use
1956 *
1957 * Calculate and return a value for the IPS driver to use when deciding whether
1958 * we have thermal and power headroom to increase CPU or GPU power budget.
1959 */
1960unsigned long i915_read_mch_val(void)
1961{
1962	struct drm_i915_private *dev_priv;
1963	unsigned long chipset_val, graphics_val, ret = 0;
1964
1965	mtx_lock(&mchdev_lock);
1966	if (!i915_mch_dev)
1967		goto out_unlock;
1968	dev_priv = i915_mch_dev;
1969
1970	chipset_val = i915_chipset_val(dev_priv);
1971	graphics_val = i915_gfx_val(dev_priv);
1972
1973	ret = chipset_val + graphics_val;
1974
1975out_unlock:
1976	mtx_unlock(&mchdev_lock);
1977
1978	return ret;
1979}
1980
1981/**
1982 * i915_gpu_raise - raise GPU frequency limit
1983 *
1984 * Raise the limit; IPS indicates we have thermal headroom.
1985 */
1986bool i915_gpu_raise(void)
1987{
1988	struct drm_i915_private *dev_priv;
1989	bool ret = true;
1990
1991	mtx_lock(&mchdev_lock);
1992	if (!i915_mch_dev) {
1993		ret = false;
1994		goto out_unlock;
1995	}
1996	dev_priv = i915_mch_dev;
1997
1998	if (dev_priv->max_delay > dev_priv->fmax)
1999		dev_priv->max_delay--;
2000
2001out_unlock:
2002	mtx_unlock(&mchdev_lock);
2003
2004	return ret;
2005}
2006
2007/**
2008 * i915_gpu_lower - lower GPU frequency limit
2009 *
2010 * IPS indicates we're close to a thermal limit, so throttle back the GPU
2011 * frequency maximum.
2012 */
2013bool i915_gpu_lower(void)
2014{
2015	struct drm_i915_private *dev_priv;
2016	bool ret = true;
2017
2018	mtx_lock(&mchdev_lock);
2019	if (!i915_mch_dev) {
2020		ret = false;
2021		goto out_unlock;
2022	}
2023	dev_priv = i915_mch_dev;
2024
2025	if (dev_priv->max_delay < dev_priv->min_delay)
2026		dev_priv->max_delay++;
2027
2028out_unlock:
2029	mtx_unlock(&mchdev_lock);
2030
2031	return ret;
2032}
2033
2034/**
2035 * i915_gpu_busy - indicate GPU business to IPS
2036 *
2037 * Tell the IPS driver whether or not the GPU is busy.
2038 */
2039bool i915_gpu_busy(void)
2040{
2041	struct drm_i915_private *dev_priv;
2042	bool ret = false;
2043
2044	mtx_lock(&mchdev_lock);
2045	if (!i915_mch_dev)
2046		goto out_unlock;
2047	dev_priv = i915_mch_dev;
2048
2049	ret = dev_priv->busy;
2050
2051out_unlock:
2052	mtx_unlock(&mchdev_lock);
2053
2054	return ret;
2055}
2056
2057/**
2058 * i915_gpu_turbo_disable - disable graphics turbo
2059 *
2060 * Disable graphics turbo by resetting the max frequency and setting the
2061 * current frequency to the default.
2062 */
2063bool i915_gpu_turbo_disable(void)
2064{
2065	struct drm_i915_private *dev_priv;
2066	bool ret = true;
2067
2068	mtx_lock(&mchdev_lock);
2069	if (!i915_mch_dev) {
2070		ret = false;
2071		goto out_unlock;
2072	}
2073	dev_priv = i915_mch_dev;
2074
2075	dev_priv->max_delay = dev_priv->fstart;
2076
2077	if (!ironlake_set_drps(dev_priv->dev, dev_priv->fstart))
2078		ret = false;
2079
2080out_unlock:
2081	mtx_unlock(&mchdev_lock);
2082
2083	return ret;
2084}
2085