xref: /openbmc/linux/drivers/gpu/drm/gma500/psb_drv.c (revision e40f97ef12772f8eb04b6a155baa1e0e2e8f3ecc)
1a61127c2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
25c49fd3aSAlan Cox /**************************************************************************
35c49fd3aSAlan Cox  * Copyright (c) 2007-2011, Intel Corporation.
45c49fd3aSAlan Cox  * All Rights Reserved.
55c49fd3aSAlan Cox  * Copyright (c) 2008, Tungsten Graphics, Inc. Cedar Park, TX., USA.
65c49fd3aSAlan Cox  * All Rights Reserved.
75c49fd3aSAlan Cox  *
85c49fd3aSAlan Cox  **************************************************************************/
95c49fd3aSAlan Cox 
100c7b178aSSam Ravnborg #include <linux/cpu.h>
110c7b178aSSam Ravnborg #include <linux/module.h>
120c7b178aSSam Ravnborg #include <linux/notifier.h>
130c7b178aSSam Ravnborg #include <linux/pm_runtime.h>
140c7b178aSSam Ravnborg #include <linux/spinlock.h>
150c7b178aSSam Ravnborg 
160c7b178aSSam Ravnborg #include <asm/set_memory.h>
170c7b178aSSam Ravnborg 
180c7b178aSSam Ravnborg #include <acpi/video.h>
190c7b178aSSam Ravnborg 
205c49fd3aSAlan Cox #include <drm/drm.h>
210c7b178aSSam Ravnborg #include <drm/drm_drv.h>
226b7ce2c4SThomas Zimmermann #include <drm/drm_fb_helper.h>
230c7b178aSSam Ravnborg #include <drm/drm_file.h>
240c7b178aSSam Ravnborg #include <drm/drm_ioctl.h>
250c7b178aSSam Ravnborg #include <drm/drm_irq.h>
260c7b178aSSam Ravnborg #include <drm/drm_pciids.h>
270c7b178aSSam Ravnborg #include <drm/drm_vblank.h>
280c7b178aSSam Ravnborg 
295c49fd3aSAlan Cox #include "framebuffer.h"
305c49fd3aSAlan Cox #include "intel_bios.h"
315c49fd3aSAlan Cox #include "mid_bios.h"
325c49fd3aSAlan Cox #include "power.h"
330c7b178aSSam Ravnborg #include "psb_drv.h"
340c7b178aSSam Ravnborg #include "psb_intel_reg.h"
350c7b178aSSam Ravnborg #include "psb_reg.h"
365c49fd3aSAlan Cox 
3770a59dd8SDaniel Vetter static const struct drm_driver driver;
38f90cd811SArthur Borsboom static int psb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
395c49fd3aSAlan Cox 
409083eb38SArthur Borsboom /*
419083eb38SArthur Borsboom  * The table below contains a mapping of the PCI vendor ID and the PCI Device ID
429083eb38SArthur Borsboom  * to the different groups of PowerVR 5-series chip designs
439083eb38SArthur Borsboom  *
449083eb38SArthur Borsboom  * 0x8086 = Intel Corporation
459083eb38SArthur Borsboom  *
469083eb38SArthur Borsboom  * PowerVR SGX535    - Poulsbo    - Intel GMA 500, Intel Atom Z5xx
479083eb38SArthur Borsboom  * PowerVR SGX535    - Moorestown - Intel GMA 600
489083eb38SArthur Borsboom  * PowerVR SGX535    - Oaktrail   - Intel GMA 600, Intel Atom Z6xx, E6xx
499083eb38SArthur Borsboom  * PowerVR SGX545    - Cedartrail - Intel GMA 3600, Intel Atom D2500, N2600
509083eb38SArthur Borsboom  * PowerVR SGX545    - Cedartrail - Intel GMA 3650, Intel Atom D2550, D2700,
519083eb38SArthur Borsboom  *                                  N2800
529083eb38SArthur Borsboom  */
539baa3c34SBenoit Taine static const struct pci_device_id pciidlist[] = {
54837f23bbSThomas Zimmermann 	/* Poulsbo */
555c49fd3aSAlan Cox 	{ 0x8086, 0x8108, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &psb_chip_ops },
565c49fd3aSAlan Cox 	{ 0x8086, 0x8109, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &psb_chip_ops },
57*e40f97efSThomas Zimmermann 	/* Oak Trail */
585c49fd3aSAlan Cox 	{ 0x8086, 0x4100, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops },
595c49fd3aSAlan Cox 	{ 0x8086, 0x4101, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops },
605c49fd3aSAlan Cox 	{ 0x8086, 0x4102, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops },
615c49fd3aSAlan Cox 	{ 0x8086, 0x4103, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops },
625c49fd3aSAlan Cox 	{ 0x8086, 0x4104, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops },
635c49fd3aSAlan Cox 	{ 0x8086, 0x4105, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops },
645c49fd3aSAlan Cox 	{ 0x8086, 0x4106, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops },
655c49fd3aSAlan Cox 	{ 0x8086, 0x4107, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops },
6656125db1SAlan Cox 	{ 0x8086, 0x4108, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops },
67*e40f97efSThomas Zimmermann 	/* Cedar Trail */
685c49fd3aSAlan Cox 	{ 0x8086, 0x0be0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
695c49fd3aSAlan Cox 	{ 0x8086, 0x0be1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
705c49fd3aSAlan Cox 	{ 0x8086, 0x0be2, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
715c49fd3aSAlan Cox 	{ 0x8086, 0x0be3, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
725c49fd3aSAlan Cox 	{ 0x8086, 0x0be4, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
735c49fd3aSAlan Cox 	{ 0x8086, 0x0be5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
745c49fd3aSAlan Cox 	{ 0x8086, 0x0be6, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
755c49fd3aSAlan Cox 	{ 0x8086, 0x0be7, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
769aa65a2bSAlan Cox 	{ 0x8086, 0x0be8, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
779aa65a2bSAlan Cox 	{ 0x8086, 0x0be9, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
789aa65a2bSAlan Cox 	{ 0x8086, 0x0bea, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
799aa65a2bSAlan Cox 	{ 0x8086, 0x0beb, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
809aa65a2bSAlan Cox 	{ 0x8086, 0x0bec, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
819aa65a2bSAlan Cox 	{ 0x8086, 0x0bed, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
829aa65a2bSAlan Cox 	{ 0x8086, 0x0bee, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
839aa65a2bSAlan Cox 	{ 0x8086, 0x0bef, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
84aa53f533SKirill A. Shutemov 	{ 0, }
855c49fd3aSAlan Cox };
865c49fd3aSAlan Cox MODULE_DEVICE_TABLE(pci, pciidlist);
875c49fd3aSAlan Cox 
885c49fd3aSAlan Cox /*
895c49fd3aSAlan Cox  * Standard IOCTLs.
905c49fd3aSAlan Cox  */
91baa70943SRob Clark static const struct drm_ioctl_desc psb_ioctls[] = {
925c49fd3aSAlan Cox };
935c49fd3aSAlan Cox 
945c49fd3aSAlan Cox static int psb_do_init(struct drm_device *dev)
955c49fd3aSAlan Cox {
965c49fd3aSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
975c49fd3aSAlan Cox 	struct psb_gtt *pg = &dev_priv->gtt;
985c49fd3aSAlan Cox 
995c49fd3aSAlan Cox 	uint32_t stolen_gtt;
1005c49fd3aSAlan Cox 
1015c49fd3aSAlan Cox 	if (pg->mmu_gatt_start & 0x0FFFFFFF) {
1025c49fd3aSAlan Cox 		dev_err(dev->dev, "Gatt must be 256M aligned. This is a bug.\n");
103af3765c7SArthur Borsboom 		return -EINVAL;
1045c49fd3aSAlan Cox 	}
1055c49fd3aSAlan Cox 
1065c49fd3aSAlan Cox 	stolen_gtt = (pg->stolen_size >> PAGE_SHIFT) * 4;
1075c49fd3aSAlan Cox 	stolen_gtt = (stolen_gtt + PAGE_SIZE - 1) >> PAGE_SHIFT;
108f90cd811SArthur Borsboom 	stolen_gtt = (stolen_gtt < pg->gtt_pages) ? stolen_gtt : pg->gtt_pages;
1095c49fd3aSAlan Cox 
1105c49fd3aSAlan Cox 	dev_priv->gatt_free_offset = pg->mmu_gatt_start +
1115c49fd3aSAlan Cox 	    (stolen_gtt << PAGE_SHIFT) * 1024;
1125c49fd3aSAlan Cox 
1135c49fd3aSAlan Cox 	spin_lock_init(&dev_priv->irqmask_lock);
1145c49fd3aSAlan Cox 
1155c49fd3aSAlan Cox 	PSB_WSGX32(0x00000000, PSB_CR_BIF_BANK0);
1165c49fd3aSAlan Cox 	PSB_WSGX32(0x00000000, PSB_CR_BIF_BANK1);
1175c49fd3aSAlan Cox 	PSB_RSGX32(PSB_CR_BIF_BANK1);
118ae012bdcSPatrik Jakobsson 
119ae012bdcSPatrik Jakobsson 	/* Do not bypass any MMU access, let them pagefault instead */
120ae012bdcSPatrik Jakobsson 	PSB_WSGX32((PSB_RSGX32(PSB_CR_BIF_CTRL) & ~_PSB_MMU_ER_MASK),
1215c49fd3aSAlan Cox 		   PSB_CR_BIF_CTRL);
122ae012bdcSPatrik Jakobsson 	PSB_RSGX32(PSB_CR_BIF_CTRL);
123ae012bdcSPatrik Jakobsson 
1245c49fd3aSAlan Cox 	psb_spank(dev_priv);
1255c49fd3aSAlan Cox 
1265c49fd3aSAlan Cox 	/* mmu_gatt ?? */
1275c49fd3aSAlan Cox 	PSB_WSGX32(pg->gatt_start, PSB_CR_BIF_TWOD_REQ_BASE);
128ae012bdcSPatrik Jakobsson 	PSB_RSGX32(PSB_CR_BIF_TWOD_REQ_BASE); /* Post */
129ae012bdcSPatrik Jakobsson 
1305c49fd3aSAlan Cox 	return 0;
1315c49fd3aSAlan Cox }
1325c49fd3aSAlan Cox 
13311b3c20bSGabriel Krisman Bertazi static void psb_driver_unload(struct drm_device *dev)
1345c49fd3aSAlan Cox {
1355c49fd3aSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
1365c49fd3aSAlan Cox 
1379083eb38SArthur Borsboom 	/* TODO: Kill vblank etc here */
1385c49fd3aSAlan Cox 
1395c49fd3aSAlan Cox 	if (dev_priv) {
1404ab2c7f1SAlan Cox 		if (dev_priv->backlight_device)
1414ab2c7f1SAlan Cox 			gma_backlight_exit(dev);
1424ab2c7f1SAlan Cox 		psb_modeset_cleanup(dev);
1435c49fd3aSAlan Cox 
1445c49fd3aSAlan Cox 		if (dev_priv->ops->chip_teardown)
1455c49fd3aSAlan Cox 			dev_priv->ops->chip_teardown(dev);
1466607e024SAlan Cox 
1476607e024SAlan Cox 		psb_intel_opregion_fini(dev);
1485c49fd3aSAlan Cox 
1495c49fd3aSAlan Cox 		if (dev_priv->pf_pd) {
1505c49fd3aSAlan Cox 			psb_mmu_free_pagedir(dev_priv->pf_pd);
1515c49fd3aSAlan Cox 			dev_priv->pf_pd = NULL;
1525c49fd3aSAlan Cox 		}
1535c49fd3aSAlan Cox 		if (dev_priv->mmu) {
1545c49fd3aSAlan Cox 			struct psb_gtt *pg = &dev_priv->gtt;
1555c49fd3aSAlan Cox 
1565c49fd3aSAlan Cox 			down_read(&pg->sem);
1575c49fd3aSAlan Cox 			psb_mmu_remove_pfn_sequence(
1585c49fd3aSAlan Cox 				psb_mmu_get_default_pd
1595c49fd3aSAlan Cox 				(dev_priv->mmu),
1605c49fd3aSAlan Cox 				pg->mmu_gatt_start,
1615c49fd3aSAlan Cox 				dev_priv->vram_stolen_size >> PAGE_SHIFT);
1625c49fd3aSAlan Cox 			up_read(&pg->sem);
1635c49fd3aSAlan Cox 			psb_mmu_driver_takedown(dev_priv->mmu);
1645c49fd3aSAlan Cox 			dev_priv->mmu = NULL;
1655c49fd3aSAlan Cox 		}
1665c49fd3aSAlan Cox 		psb_gtt_takedown(dev);
1675c49fd3aSAlan Cox 		if (dev_priv->scratch_page) {
1681896344bSAlan Cox 			set_pages_wb(dev_priv->scratch_page, 1);
1695c49fd3aSAlan Cox 			__free_page(dev_priv->scratch_page);
1705c49fd3aSAlan Cox 			dev_priv->scratch_page = NULL;
1715c49fd3aSAlan Cox 		}
1725c49fd3aSAlan Cox 		if (dev_priv->vdc_reg) {
1735c49fd3aSAlan Cox 			iounmap(dev_priv->vdc_reg);
1745c49fd3aSAlan Cox 			dev_priv->vdc_reg = NULL;
1755c49fd3aSAlan Cox 		}
1765c49fd3aSAlan Cox 		if (dev_priv->sgx_reg) {
1775c49fd3aSAlan Cox 			iounmap(dev_priv->sgx_reg);
1785c49fd3aSAlan Cox 			dev_priv->sgx_reg = NULL;
1795c49fd3aSAlan Cox 		}
1802657929dSPatrik Jakobsson 		if (dev_priv->aux_reg) {
1812657929dSPatrik Jakobsson 			iounmap(dev_priv->aux_reg);
1822657929dSPatrik Jakobsson 			dev_priv->aux_reg = NULL;
1832657929dSPatrik Jakobsson 		}
1842657929dSPatrik Jakobsson 		pci_dev_put(dev_priv->aux_pdev);
1855a52b1f2SJan Safrata 		pci_dev_put(dev_priv->lpc_pdev);
1865c49fd3aSAlan Cox 
1874ab2c7f1SAlan Cox 		/* Destroy VBT data */
1884ab2c7f1SAlan Cox 		psb_intel_destroy_bios(dev);
1894ab2c7f1SAlan Cox 
1905c49fd3aSAlan Cox 		kfree(dev_priv);
1915c49fd3aSAlan Cox 		dev->dev_private = NULL;
1925c49fd3aSAlan Cox 	}
1935c49fd3aSAlan Cox 	gma_power_uninit(dev);
1945c49fd3aSAlan Cox }
1955c49fd3aSAlan Cox 
196f90cd811SArthur Borsboom static int psb_driver_load(struct drm_device *dev, unsigned long flags)
1975c49fd3aSAlan Cox {
198a2c68495SThomas Zimmermann 	struct pci_dev *pdev = to_pci_dev(dev->dev);
1995c49fd3aSAlan Cox 	struct drm_psb_private *dev_priv;
2002657929dSPatrik Jakobsson 	unsigned long resource_start, resource_len;
2015c49fd3aSAlan Cox 	unsigned long irqflags;
2025c49fd3aSAlan Cox 	int ret = -ENOMEM;
2035c49fd3aSAlan Cox 	struct drm_connector *connector;
204367e4408SPatrik Jakobsson 	struct gma_encoder *gma_encoder;
205ae012bdcSPatrik Jakobsson 	struct psb_gtt *pg;
2065c49fd3aSAlan Cox 
2079083eb38SArthur Borsboom 	/* allocating and initializing driver private data */
2085c49fd3aSAlan Cox 	dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
2095c49fd3aSAlan Cox 	if (dev_priv == NULL)
2105c49fd3aSAlan Cox 		return -ENOMEM;
2115c49fd3aSAlan Cox 
212f90cd811SArthur Borsboom 	dev_priv->ops = (struct psb_ops *)flags;
2135c49fd3aSAlan Cox 	dev_priv->dev = dev;
2145c49fd3aSAlan Cox 	dev->dev_private = (void *) dev_priv;
2155c49fd3aSAlan Cox 
216ae012bdcSPatrik Jakobsson 	pg = &dev_priv->gtt;
217ae012bdcSPatrik Jakobsson 
218a2c68495SThomas Zimmermann 	pci_set_master(pdev);
219466e69b8SDave Airlie 
2205c49fd3aSAlan Cox 	dev_priv->num_pipe = dev_priv->ops->pipes;
2215c49fd3aSAlan Cox 
222a2c68495SThomas Zimmermann 	resource_start = pci_resource_start(pdev, PSB_MMIO_RESOURCE);
2235c49fd3aSAlan Cox 
2245c49fd3aSAlan Cox 	dev_priv->vdc_reg =
2255c49fd3aSAlan Cox 	    ioremap(resource_start + PSB_VDC_OFFSET, PSB_VDC_SIZE);
2265c49fd3aSAlan Cox 	if (!dev_priv->vdc_reg)
2275c49fd3aSAlan Cox 		goto out_err;
2285c49fd3aSAlan Cox 
2295c49fd3aSAlan Cox 	dev_priv->sgx_reg = ioremap(resource_start + dev_priv->ops->sgx_offset,
2305c49fd3aSAlan Cox 							PSB_SGX_SIZE);
2315c49fd3aSAlan Cox 	if (!dev_priv->sgx_reg)
2325c49fd3aSAlan Cox 		goto out_err;
2335c49fd3aSAlan Cox 
2342657929dSPatrik Jakobsson 	if (IS_MRST(dev)) {
235a2c68495SThomas Zimmermann 		int domain = pci_domain_nr(pdev->bus);
236ba99d834SSinan Kaya 
237ba99d834SSinan Kaya 		dev_priv->aux_pdev =
238ba99d834SSinan Kaya 			pci_get_domain_bus_and_slot(domain, 0,
239ba99d834SSinan Kaya 						    PCI_DEVFN(3, 0));
2402657929dSPatrik Jakobsson 
2412657929dSPatrik Jakobsson 		if (dev_priv->aux_pdev) {
2422657929dSPatrik Jakobsson 			resource_start = pci_resource_start(dev_priv->aux_pdev,
2432657929dSPatrik Jakobsson 							    PSB_AUX_RESOURCE);
2442657929dSPatrik Jakobsson 			resource_len = pci_resource_len(dev_priv->aux_pdev,
2452657929dSPatrik Jakobsson 							PSB_AUX_RESOURCE);
2464bdc0d67SChristoph Hellwig 			dev_priv->aux_reg = ioremap(resource_start,
2472657929dSPatrik Jakobsson 							    resource_len);
2482657929dSPatrik Jakobsson 			if (!dev_priv->aux_reg)
2492657929dSPatrik Jakobsson 				goto out_err;
2502657929dSPatrik Jakobsson 
2512657929dSPatrik Jakobsson 			DRM_DEBUG_KMS("Found aux vdc");
2522657929dSPatrik Jakobsson 		} else {
2532657929dSPatrik Jakobsson 			/* Couldn't find the aux vdc so map to primary vdc */
2542657929dSPatrik Jakobsson 			dev_priv->aux_reg = dev_priv->vdc_reg;
2552657929dSPatrik Jakobsson 			DRM_DEBUG_KMS("Couldn't find aux pci device");
2562657929dSPatrik Jakobsson 		}
2572657929dSPatrik Jakobsson 		dev_priv->gmbus_reg = dev_priv->aux_reg;
2585a52b1f2SJan Safrata 
259ba99d834SSinan Kaya 		dev_priv->lpc_pdev =
260ba99d834SSinan Kaya 			pci_get_domain_bus_and_slot(domain, 0,
261ba99d834SSinan Kaya 						    PCI_DEVFN(31, 0));
2625a52b1f2SJan Safrata 		if (dev_priv->lpc_pdev) {
2635a52b1f2SJan Safrata 			pci_read_config_word(dev_priv->lpc_pdev, PSB_LPC_GBA,
2645a52b1f2SJan Safrata 				&dev_priv->lpc_gpio_base);
2655a52b1f2SJan Safrata 			pci_write_config_dword(dev_priv->lpc_pdev, PSB_LPC_GBA,
2665a52b1f2SJan Safrata 				(u32)dev_priv->lpc_gpio_base | (1L<<31));
2675a52b1f2SJan Safrata 			pci_read_config_word(dev_priv->lpc_pdev, PSB_LPC_GBA,
2685a52b1f2SJan Safrata 				&dev_priv->lpc_gpio_base);
2695a52b1f2SJan Safrata 			dev_priv->lpc_gpio_base &= 0xffc0;
2705a52b1f2SJan Safrata 			if (dev_priv->lpc_gpio_base)
2715a52b1f2SJan Safrata 				DRM_DEBUG_KMS("Found LPC GPIO at 0x%04x\n",
2725a52b1f2SJan Safrata 						dev_priv->lpc_gpio_base);
2735a52b1f2SJan Safrata 			else {
2745a52b1f2SJan Safrata 				pci_dev_put(dev_priv->lpc_pdev);
2755a52b1f2SJan Safrata 				dev_priv->lpc_pdev = NULL;
2765a52b1f2SJan Safrata 			}
2775a52b1f2SJan Safrata 		}
2782657929dSPatrik Jakobsson 	} else {
2792657929dSPatrik Jakobsson 		dev_priv->gmbus_reg = dev_priv->vdc_reg;
2802657929dSPatrik Jakobsson 	}
2812657929dSPatrik Jakobsson 
282d839ede4SAlan Cox 	psb_intel_opregion_setup(dev);
283d839ede4SAlan Cox 
2845c49fd3aSAlan Cox 	ret = dev_priv->ops->chip_setup(dev);
2855c49fd3aSAlan Cox 	if (ret)
2865c49fd3aSAlan Cox 		goto out_err;
2875c49fd3aSAlan Cox 
2885c49fd3aSAlan Cox 	/* Init OSPM support */
2895c49fd3aSAlan Cox 	gma_power_init(dev);
2905c49fd3aSAlan Cox 
2915c49fd3aSAlan Cox 	ret = -ENOMEM;
2925c49fd3aSAlan Cox 
2935c49fd3aSAlan Cox 	dev_priv->scratch_page = alloc_page(GFP_DMA32 | __GFP_ZERO);
2945c49fd3aSAlan Cox 	if (!dev_priv->scratch_page)
2955c49fd3aSAlan Cox 		goto out_err;
2965c49fd3aSAlan Cox 
2975c49fd3aSAlan Cox 	set_pages_uc(dev_priv->scratch_page, 1);
2985c49fd3aSAlan Cox 
2995c49fd3aSAlan Cox 	ret = psb_gtt_init(dev, 0);
3005c49fd3aSAlan Cox 	if (ret)
3015c49fd3aSAlan Cox 		goto out_err;
3025c49fd3aSAlan Cox 
3036926872aSJialin Zhang 	ret = -ENOMEM;
3046926872aSJialin Zhang 
3058f394872SPatrik Jakobsson 	dev_priv->mmu = psb_mmu_driver_init(dev, 1, 0, 0);
3065c49fd3aSAlan Cox 	if (!dev_priv->mmu)
3075c49fd3aSAlan Cox 		goto out_err;
3085c49fd3aSAlan Cox 
3095c49fd3aSAlan Cox 	dev_priv->pf_pd = psb_mmu_alloc_pd(dev_priv->mmu, 1, 0);
3105c49fd3aSAlan Cox 	if (!dev_priv->pf_pd)
3115c49fd3aSAlan Cox 		goto out_err;
3125c49fd3aSAlan Cox 
3135c49fd3aSAlan Cox 	ret = psb_do_init(dev);
3145c49fd3aSAlan Cox 	if (ret)
3155c49fd3aSAlan Cox 		return ret;
3165c49fd3aSAlan Cox 
317ae012bdcSPatrik Jakobsson 	/* Add stolen memory to SGX MMU */
318ae012bdcSPatrik Jakobsson 	down_read(&pg->sem);
319ae012bdcSPatrik Jakobsson 	ret = psb_mmu_insert_pfn_sequence(psb_mmu_get_default_pd(dev_priv->mmu),
320ae012bdcSPatrik Jakobsson 					  dev_priv->stolen_base >> PAGE_SHIFT,
321ae012bdcSPatrik Jakobsson 					  pg->gatt_start,
322ae012bdcSPatrik Jakobsson 					  pg->stolen_size >> PAGE_SHIFT, 0);
323ae012bdcSPatrik Jakobsson 	up_read(&pg->sem);
324ae012bdcSPatrik Jakobsson 
325ae012bdcSPatrik Jakobsson 	psb_mmu_set_pd_context(psb_mmu_get_default_pd(dev_priv->mmu), 0);
326ae012bdcSPatrik Jakobsson 	psb_mmu_set_pd_context(dev_priv->pf_pd, 1);
327ae012bdcSPatrik Jakobsson 
3285c49fd3aSAlan Cox 	PSB_WSGX32(0x20000000, PSB_CR_PDS_EXEC_BASE);
3295c49fd3aSAlan Cox 	PSB_WSGX32(0x30000000, PSB_CR_BIF_3D_REQ_BASE);
3305c49fd3aSAlan Cox 
331d839ede4SAlan Cox 	acpi_video_register();
3325c49fd3aSAlan Cox 
3339083eb38SArthur Borsboom 	/* Setup vertical blanking handling */
3345c49fd3aSAlan Cox 	ret = drm_vblank_init(dev, dev_priv->num_pipe);
3355c49fd3aSAlan Cox 	if (ret)
3365c49fd3aSAlan Cox 		goto out_err;
3375c49fd3aSAlan Cox 
3385c49fd3aSAlan Cox 	/*
3395c49fd3aSAlan Cox 	 * Install interrupt handlers prior to powering off SGX or else we will
3405c49fd3aSAlan Cox 	 * crash.
3415c49fd3aSAlan Cox 	 */
3425c49fd3aSAlan Cox 	dev_priv->vdc_irq_mask = 0;
3435c49fd3aSAlan Cox 	dev_priv->pipestat[0] = 0;
3445c49fd3aSAlan Cox 	dev_priv->pipestat[1] = 0;
3455c49fd3aSAlan Cox 	dev_priv->pipestat[2] = 0;
3465c49fd3aSAlan Cox 	spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
3475c49fd3aSAlan Cox 	PSB_WVDC32(0xFFFFFFFF, PSB_HWSTAM);
3485c49fd3aSAlan Cox 	PSB_WVDC32(0x00000000, PSB_INT_ENABLE_R);
3495c49fd3aSAlan Cox 	PSB_WVDC32(0xFFFFFFFF, PSB_INT_MASK_R);
3505c49fd3aSAlan Cox 	spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
35162363a48SAlan Cox 
352a2c68495SThomas Zimmermann 	drm_irq_install(dev, pdev->irq);
3535c49fd3aSAlan Cox 
3545c49fd3aSAlan Cox 	dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
3555c49fd3aSAlan Cox 
3565c49fd3aSAlan Cox 	psb_modeset_init(dev);
3575c49fd3aSAlan Cox 	psb_fbdev_init(dev);
3585c49fd3aSAlan Cox 	drm_kms_helper_poll_init(dev);
3595c49fd3aSAlan Cox 
3605c49fd3aSAlan Cox 	/* Only add backlight support if we have LVDS output */
3615c49fd3aSAlan Cox 	list_for_each_entry(connector, &dev->mode_config.connector_list,
3625c49fd3aSAlan Cox 			    head) {
363367e4408SPatrik Jakobsson 		gma_encoder = gma_attached_encoder(connector);
3645c49fd3aSAlan Cox 
365367e4408SPatrik Jakobsson 		switch (gma_encoder->type) {
3665c49fd3aSAlan Cox 		case INTEL_OUTPUT_LVDS:
3675c49fd3aSAlan Cox 		case INTEL_OUTPUT_MIPI:
3685c49fd3aSAlan Cox 			ret = gma_backlight_init(dev);
3695c49fd3aSAlan Cox 			break;
3705c49fd3aSAlan Cox 		}
3715c49fd3aSAlan Cox 	}
3725c49fd3aSAlan Cox 
3735c49fd3aSAlan Cox 	if (ret)
3745c49fd3aSAlan Cox 		return ret;
375166973e5SAlan Cox 	psb_intel_opregion_enable_asle(dev);
3765c49fd3aSAlan Cox #if 0
3779083eb38SArthur Borsboom 	/* Enable runtime pm at last */
378a2c68495SThomas Zimmermann 	pm_runtime_enable(dev->dev);
379a2c68495SThomas Zimmermann 	pm_runtime_set_active(dev->dev);
3805c49fd3aSAlan Cox #endif
3815c49fd3aSAlan Cox 	/* Intel drm driver load is done, continue doing pvr load */
3825c49fd3aSAlan Cox 	return 0;
3835c49fd3aSAlan Cox out_err:
3845c49fd3aSAlan Cox 	psb_driver_unload(dev);
3855c49fd3aSAlan Cox 	return ret;
3865c49fd3aSAlan Cox }
3875c49fd3aSAlan Cox 
3885c49fd3aSAlan Cox static inline void get_brightness(struct backlight_device *bd)
3895c49fd3aSAlan Cox {
3905c49fd3aSAlan Cox #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
3915c49fd3aSAlan Cox 	if (bd) {
3925c49fd3aSAlan Cox 		bd->props.brightness = bd->ops->get_brightness(bd);
3935c49fd3aSAlan Cox 		backlight_update_status(bd);
3945c49fd3aSAlan Cox 	}
3955c49fd3aSAlan Cox #endif
3965c49fd3aSAlan Cox }
3975c49fd3aSAlan Cox 
3985c49fd3aSAlan Cox static long psb_unlocked_ioctl(struct file *filp, unsigned int cmd,
3995c49fd3aSAlan Cox 			       unsigned long arg)
4005c49fd3aSAlan Cox {
4015c49fd3aSAlan Cox 	struct drm_file *file_priv = filp->private_data;
4025c49fd3aSAlan Cox 	struct drm_device *dev = file_priv->minor->dev;
4035c49fd3aSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
4045c49fd3aSAlan Cox 	static unsigned int runtime_allowed;
4055c49fd3aSAlan Cox 
4065c49fd3aSAlan Cox 	if (runtime_allowed == 1 && dev_priv->is_lvds_on) {
4075c49fd3aSAlan Cox 		runtime_allowed++;
408a2c68495SThomas Zimmermann 		pm_runtime_allow(dev->dev);
4095c49fd3aSAlan Cox 		dev_priv->rpm_enabled = 1;
4105c49fd3aSAlan Cox 	}
4115c49fd3aSAlan Cox 	return drm_ioctl(filp, cmd, arg);
4125c49fd3aSAlan Cox 	/* FIXME: do we need to wrap the other side of this */
4135c49fd3aSAlan Cox }
4145c49fd3aSAlan Cox 
415f90cd811SArthur Borsboom static int psb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
416f90cd811SArthur Borsboom {
41750cbe0e3SThomas Zimmermann 	struct drm_device *dev;
41850cbe0e3SThomas Zimmermann 	int ret;
41950cbe0e3SThomas Zimmermann 
42050cbe0e3SThomas Zimmermann 	ret = pci_enable_device(pdev);
42150cbe0e3SThomas Zimmermann 	if (ret)
42250cbe0e3SThomas Zimmermann 		return ret;
42350cbe0e3SThomas Zimmermann 
42450cbe0e3SThomas Zimmermann 	dev = drm_dev_alloc(&driver, &pdev->dev);
42550cbe0e3SThomas Zimmermann 	if (IS_ERR(dev)) {
42650cbe0e3SThomas Zimmermann 		ret = PTR_ERR(dev);
42750cbe0e3SThomas Zimmermann 		goto err_pci_disable_device;
428f90cd811SArthur Borsboom 	}
429f90cd811SArthur Borsboom 
43050cbe0e3SThomas Zimmermann 	pci_set_drvdata(pdev, dev);
43150cbe0e3SThomas Zimmermann 
43250cbe0e3SThomas Zimmermann 	ret = psb_driver_load(dev, ent->driver_data);
43350cbe0e3SThomas Zimmermann 	if (ret)
43450cbe0e3SThomas Zimmermann 		goto err_drm_dev_put;
43550cbe0e3SThomas Zimmermann 
43650cbe0e3SThomas Zimmermann 	ret = drm_dev_register(dev, ent->driver_data);
43750cbe0e3SThomas Zimmermann 	if (ret)
43850cbe0e3SThomas Zimmermann 		goto err_psb_driver_unload;
43950cbe0e3SThomas Zimmermann 
44050cbe0e3SThomas Zimmermann 	return 0;
44150cbe0e3SThomas Zimmermann 
44250cbe0e3SThomas Zimmermann err_psb_driver_unload:
44350cbe0e3SThomas Zimmermann 	psb_driver_unload(dev);
44450cbe0e3SThomas Zimmermann err_drm_dev_put:
44550cbe0e3SThomas Zimmermann 	drm_dev_put(dev);
44650cbe0e3SThomas Zimmermann err_pci_disable_device:
44750cbe0e3SThomas Zimmermann 	pci_disable_device(pdev);
44850cbe0e3SThomas Zimmermann 	return ret;
44950cbe0e3SThomas Zimmermann }
450f90cd811SArthur Borsboom 
451f90cd811SArthur Borsboom static void psb_pci_remove(struct pci_dev *pdev)
4525c49fd3aSAlan Cox {
4535c49fd3aSAlan Cox 	struct drm_device *dev = pci_get_drvdata(pdev);
45450cbe0e3SThomas Zimmermann 
45550cbe0e3SThomas Zimmermann 	drm_dev_unregister(dev);
45650cbe0e3SThomas Zimmermann 	psb_driver_unload(dev);
45750cbe0e3SThomas Zimmermann 	drm_dev_put(dev);
4585c49fd3aSAlan Cox }
4595c49fd3aSAlan Cox 
4605c49fd3aSAlan Cox static const struct dev_pm_ops psb_pm_ops = {
4615c49fd3aSAlan Cox 	.resume = gma_power_resume,
4625c49fd3aSAlan Cox 	.suspend = gma_power_suspend,
463888eef2eSPatrik Jakobsson 	.thaw = gma_power_thaw,
464888eef2eSPatrik Jakobsson 	.freeze = gma_power_freeze,
465888eef2eSPatrik Jakobsson 	.restore = gma_power_restore,
4665c49fd3aSAlan Cox 	.runtime_suspend = psb_runtime_suspend,
4675c49fd3aSAlan Cox 	.runtime_resume = psb_runtime_resume,
4685c49fd3aSAlan Cox 	.runtime_idle = psb_runtime_idle,
4695c49fd3aSAlan Cox };
4705c49fd3aSAlan Cox 
4711595c568SIlija Hadzic static const struct file_operations psb_gem_fops = {
4721595c568SIlija Hadzic 	.owner = THIS_MODULE,
4731595c568SIlija Hadzic 	.open = drm_open,
4741595c568SIlija Hadzic 	.release = drm_release,
4751595c568SIlija Hadzic 	.unlocked_ioctl = psb_unlocked_ioctl,
4760a97c81aSPatrik Jakobsson 	.compat_ioctl = drm_compat_ioctl,
4771595c568SIlija Hadzic 	.mmap = drm_gem_mmap,
4781595c568SIlija Hadzic 	.poll = drm_poll,
4791595c568SIlija Hadzic 	.read = drm_read,
4801595c568SIlija Hadzic };
4811595c568SIlija Hadzic 
48270a59dd8SDaniel Vetter static const struct drm_driver driver = {
4831ff49481SDaniel Vetter 	.driver_features = DRIVER_MODESET | DRIVER_GEM,
484cdc13f68SNoralf Trønnes 	.lastclose = drm_fb_helper_lastclose,
4855c49fd3aSAlan Cox 
486f95aeb17SDamien Lespiau 	.num_ioctls = ARRAY_SIZE(psb_ioctls),
4875c49fd3aSAlan Cox 	.irq_preinstall = psb_irq_preinstall,
4885c49fd3aSAlan Cox 	.irq_postinstall = psb_irq_postinstall,
4895c49fd3aSAlan Cox 	.irq_uninstall = psb_irq_uninstall,
4905c49fd3aSAlan Cox 	.irq_handler = psb_irq_handler,
4915c49fd3aSAlan Cox 
4925c49fd3aSAlan Cox 	.dumb_create = psb_gem_dumb_create,
493f90cd811SArthur Borsboom 	.ioctls = psb_ioctls,
4941595c568SIlija Hadzic 	.fops = &psb_gem_fops,
4955c49fd3aSAlan Cox 	.name = DRIVER_NAME,
4965c49fd3aSAlan Cox 	.desc = DRIVER_DESC,
497f90cd811SArthur Borsboom 	.date = DRIVER_DATE,
498f90cd811SArthur Borsboom 	.major = DRIVER_MAJOR,
499f90cd811SArthur Borsboom 	.minor = DRIVER_MINOR,
500f90cd811SArthur Borsboom 	.patchlevel = DRIVER_PATCHLEVEL
5015c49fd3aSAlan Cox };
5025c49fd3aSAlan Cox 
5035c49fd3aSAlan Cox static struct pci_driver psb_pci_driver = {
5045c49fd3aSAlan Cox 	.name = DRIVER_NAME,
5055c49fd3aSAlan Cox 	.id_table = pciidlist,
506f90cd811SArthur Borsboom 	.probe = psb_pci_probe,
507f90cd811SArthur Borsboom 	.remove = psb_pci_remove,
508f90cd811SArthur Borsboom 	.driver.pm = &psb_pm_ops,
5095c49fd3aSAlan Cox };
5105c49fd3aSAlan Cox 
5115c49fd3aSAlan Cox static int __init psb_init(void)
5125c49fd3aSAlan Cox {
51310631d72SDaniel Vetter 	return pci_register_driver(&psb_pci_driver);
5145c49fd3aSAlan Cox }
5155c49fd3aSAlan Cox 
5165c49fd3aSAlan Cox static void __exit psb_exit(void)
5175c49fd3aSAlan Cox {
51810631d72SDaniel Vetter 	pci_unregister_driver(&psb_pci_driver);
5195c49fd3aSAlan Cox }
5205c49fd3aSAlan Cox 
5215c49fd3aSAlan Cox late_initcall(psb_init);
5225c49fd3aSAlan Cox module_exit(psb_exit);
5235c49fd3aSAlan Cox 
524f90cd811SArthur Borsboom MODULE_AUTHOR(DRIVER_AUTHOR);
5255c49fd3aSAlan Cox MODULE_DESCRIPTION(DRIVER_DESC);
5260df12b3fSGreg Kroah-Hartman MODULE_LICENSE("GPL");
527