xref: /openbmc/linux/drivers/gpu/drm/gma500/psb_drv.c (revision d35a4bf66079b92e232ac85b08f19312be9b7eca)
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>
155c209d80SPatrik Jakobsson #include <linux/delay.h>
160c7b178aSSam Ravnborg 
170c7b178aSSam Ravnborg #include <asm/set_memory.h>
180c7b178aSSam Ravnborg 
190c7b178aSSam Ravnborg #include <acpi/video.h>
200c7b178aSSam Ravnborg 
215c49fd3aSAlan Cox #include <drm/drm.h>
22035f79f9SThomas Zimmermann #include <drm/drm_aperture.h>
230c7b178aSSam Ravnborg #include <drm/drm_drv.h>
246b7ce2c4SThomas Zimmermann #include <drm/drm_fb_helper.h>
250c7b178aSSam Ravnborg #include <drm/drm_file.h>
260c7b178aSSam Ravnborg #include <drm/drm_ioctl.h>
270c7b178aSSam Ravnborg #include <drm/drm_pciids.h>
280c7b178aSSam Ravnborg #include <drm/drm_vblank.h>
290c7b178aSSam Ravnborg 
305c49fd3aSAlan Cox #include "framebuffer.h"
31d339386cSThomas Zimmermann #include "gem.h"
325c49fd3aSAlan Cox #include "intel_bios.h"
335c49fd3aSAlan Cox #include "mid_bios.h"
345c49fd3aSAlan Cox #include "power.h"
350c7b178aSSam Ravnborg #include "psb_drv.h"
360c7b178aSSam Ravnborg #include "psb_intel_reg.h"
3722908507SThomas Zimmermann #include "psb_irq.h"
380c7b178aSSam Ravnborg #include "psb_reg.h"
395c49fd3aSAlan Cox 
4070a59dd8SDaniel Vetter static const struct drm_driver driver;
41f90cd811SArthur Borsboom static int psb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
425c49fd3aSAlan Cox 
439083eb38SArthur Borsboom /*
449083eb38SArthur Borsboom  * The table below contains a mapping of the PCI vendor ID and the PCI Device ID
459083eb38SArthur Borsboom  * to the different groups of PowerVR 5-series chip designs
469083eb38SArthur Borsboom  *
479083eb38SArthur Borsboom  * 0x8086 = Intel Corporation
489083eb38SArthur Borsboom  *
499083eb38SArthur Borsboom  * PowerVR SGX535    - Poulsbo    - Intel GMA 500, Intel Atom Z5xx
509083eb38SArthur Borsboom  * PowerVR SGX535    - Moorestown - Intel GMA 600
519083eb38SArthur Borsboom  * PowerVR SGX535    - Oaktrail   - Intel GMA 600, Intel Atom Z6xx, E6xx
529083eb38SArthur Borsboom  * PowerVR SGX545    - Cedartrail - Intel GMA 3600, Intel Atom D2500, N2600
539083eb38SArthur Borsboom  * PowerVR SGX545    - Cedartrail - Intel GMA 3650, Intel Atom D2550, D2700,
549083eb38SArthur Borsboom  *                                  N2800
559083eb38SArthur Borsboom  */
569baa3c34SBenoit Taine static const struct pci_device_id pciidlist[] = {
57837f23bbSThomas Zimmermann 	/* Poulsbo */
585c49fd3aSAlan Cox 	{ 0x8086, 0x8108, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &psb_chip_ops },
595c49fd3aSAlan Cox 	{ 0x8086, 0x8109, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &psb_chip_ops },
60e40f97efSThomas Zimmermann 	/* Oak Trail */
615c49fd3aSAlan Cox 	{ 0x8086, 0x4100, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops },
625c49fd3aSAlan Cox 	{ 0x8086, 0x4101, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops },
635c49fd3aSAlan Cox 	{ 0x8086, 0x4102, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops },
645c49fd3aSAlan Cox 	{ 0x8086, 0x4103, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops },
655c49fd3aSAlan Cox 	{ 0x8086, 0x4104, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops },
665c49fd3aSAlan Cox 	{ 0x8086, 0x4105, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops },
675c49fd3aSAlan Cox 	{ 0x8086, 0x4106, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops },
685c49fd3aSAlan Cox 	{ 0x8086, 0x4107, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops },
6956125db1SAlan Cox 	{ 0x8086, 0x4108, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops },
70e40f97efSThomas Zimmermann 	/* Cedar Trail */
715c49fd3aSAlan Cox 	{ 0x8086, 0x0be0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
725c49fd3aSAlan Cox 	{ 0x8086, 0x0be1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
735c49fd3aSAlan Cox 	{ 0x8086, 0x0be2, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
745c49fd3aSAlan Cox 	{ 0x8086, 0x0be3, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
755c49fd3aSAlan Cox 	{ 0x8086, 0x0be4, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
765c49fd3aSAlan Cox 	{ 0x8086, 0x0be5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
775c49fd3aSAlan Cox 	{ 0x8086, 0x0be6, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
785c49fd3aSAlan Cox 	{ 0x8086, 0x0be7, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
799aa65a2bSAlan Cox 	{ 0x8086, 0x0be8, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
809aa65a2bSAlan Cox 	{ 0x8086, 0x0be9, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
819aa65a2bSAlan Cox 	{ 0x8086, 0x0bea, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
829aa65a2bSAlan Cox 	{ 0x8086, 0x0beb, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
839aa65a2bSAlan Cox 	{ 0x8086, 0x0bec, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
849aa65a2bSAlan Cox 	{ 0x8086, 0x0bed, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
859aa65a2bSAlan Cox 	{ 0x8086, 0x0bee, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
869aa65a2bSAlan Cox 	{ 0x8086, 0x0bef, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
87aa53f533SKirill A. Shutemov 	{ 0, }
885c49fd3aSAlan Cox };
895c49fd3aSAlan Cox MODULE_DEVICE_TABLE(pci, pciidlist);
905c49fd3aSAlan Cox 
915c49fd3aSAlan Cox /*
925c49fd3aSAlan Cox  * Standard IOCTLs.
935c49fd3aSAlan Cox  */
94baa70943SRob Clark static const struct drm_ioctl_desc psb_ioctls[] = {
955c49fd3aSAlan Cox };
965c49fd3aSAlan Cox 
975c209d80SPatrik Jakobsson /**
985c209d80SPatrik Jakobsson  *	psb_spank		-	reset the 2D engine
995c209d80SPatrik Jakobsson  *	@dev_priv: our PSB DRM device
1005c209d80SPatrik Jakobsson  *
1015c209d80SPatrik Jakobsson  *	Soft reset the graphics engine and then reload the necessary registers.
1025c209d80SPatrik Jakobsson  */
103d9f6e812SPatrik Jakobsson static void psb_spank(struct drm_psb_private *dev_priv)
1045c209d80SPatrik Jakobsson {
1055c209d80SPatrik Jakobsson 	PSB_WSGX32(_PSB_CS_RESET_BIF_RESET | _PSB_CS_RESET_DPM_RESET |
1065c209d80SPatrik Jakobsson 		_PSB_CS_RESET_TA_RESET | _PSB_CS_RESET_USE_RESET |
1075c209d80SPatrik Jakobsson 		_PSB_CS_RESET_ISP_RESET | _PSB_CS_RESET_TSP_RESET |
1085c209d80SPatrik Jakobsson 		_PSB_CS_RESET_TWOD_RESET, PSB_CR_SOFT_RESET);
1095c209d80SPatrik Jakobsson 	PSB_RSGX32(PSB_CR_SOFT_RESET);
1105c209d80SPatrik Jakobsson 
1115c209d80SPatrik Jakobsson 	msleep(1);
1125c209d80SPatrik Jakobsson 
1135c209d80SPatrik Jakobsson 	PSB_WSGX32(0, PSB_CR_SOFT_RESET);
1145c209d80SPatrik Jakobsson 	wmb();
1155c209d80SPatrik Jakobsson 	PSB_WSGX32(PSB_RSGX32(PSB_CR_BIF_CTRL) | _PSB_CB_CTRL_CLEAR_FAULT,
1165c209d80SPatrik Jakobsson 		   PSB_CR_BIF_CTRL);
1175c209d80SPatrik Jakobsson 	wmb();
1185c209d80SPatrik Jakobsson 	(void) PSB_RSGX32(PSB_CR_BIF_CTRL);
1195c209d80SPatrik Jakobsson 
1205c209d80SPatrik Jakobsson 	msleep(1);
1215c209d80SPatrik Jakobsson 	PSB_WSGX32(PSB_RSGX32(PSB_CR_BIF_CTRL) & ~_PSB_CB_CTRL_CLEAR_FAULT,
1225c209d80SPatrik Jakobsson 		   PSB_CR_BIF_CTRL);
1235c209d80SPatrik Jakobsson 	(void) PSB_RSGX32(PSB_CR_BIF_CTRL);
1245c209d80SPatrik Jakobsson 	PSB_WSGX32(dev_priv->gtt.gatt_start, PSB_CR_BIF_TWOD_REQ_BASE);
1255c209d80SPatrik Jakobsson }
1265c209d80SPatrik Jakobsson 
1275c49fd3aSAlan Cox static int psb_do_init(struct drm_device *dev)
1285c49fd3aSAlan Cox {
129f71635e8SThomas Zimmermann 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
1305c49fd3aSAlan Cox 	struct psb_gtt *pg = &dev_priv->gtt;
1315c49fd3aSAlan Cox 
1325c49fd3aSAlan Cox 	uint32_t stolen_gtt;
1335c49fd3aSAlan Cox 
1345c49fd3aSAlan Cox 	if (pg->mmu_gatt_start & 0x0FFFFFFF) {
1355c49fd3aSAlan Cox 		dev_err(dev->dev, "Gatt must be 256M aligned. This is a bug.\n");
136af3765c7SArthur Borsboom 		return -EINVAL;
1375c49fd3aSAlan Cox 	}
1385c49fd3aSAlan Cox 
1395c49fd3aSAlan Cox 	stolen_gtt = (pg->stolen_size >> PAGE_SHIFT) * 4;
1405c49fd3aSAlan Cox 	stolen_gtt = (stolen_gtt + PAGE_SIZE - 1) >> PAGE_SHIFT;
141f90cd811SArthur Borsboom 	stolen_gtt = (stolen_gtt < pg->gtt_pages) ? stolen_gtt : pg->gtt_pages;
1425c49fd3aSAlan Cox 
1435c49fd3aSAlan Cox 	dev_priv->gatt_free_offset = pg->mmu_gatt_start +
1445c49fd3aSAlan Cox 	    (stolen_gtt << PAGE_SHIFT) * 1024;
1455c49fd3aSAlan Cox 
1465c49fd3aSAlan Cox 	spin_lock_init(&dev_priv->irqmask_lock);
1475c49fd3aSAlan Cox 
1485c49fd3aSAlan Cox 	PSB_WSGX32(0x00000000, PSB_CR_BIF_BANK0);
1495c49fd3aSAlan Cox 	PSB_WSGX32(0x00000000, PSB_CR_BIF_BANK1);
1505c49fd3aSAlan Cox 	PSB_RSGX32(PSB_CR_BIF_BANK1);
151ae012bdcSPatrik Jakobsson 
152ae012bdcSPatrik Jakobsson 	/* Do not bypass any MMU access, let them pagefault instead */
153ae012bdcSPatrik Jakobsson 	PSB_WSGX32((PSB_RSGX32(PSB_CR_BIF_CTRL) & ~_PSB_MMU_ER_MASK),
1545c49fd3aSAlan Cox 		   PSB_CR_BIF_CTRL);
155ae012bdcSPatrik Jakobsson 	PSB_RSGX32(PSB_CR_BIF_CTRL);
156ae012bdcSPatrik Jakobsson 
1575c49fd3aSAlan Cox 	psb_spank(dev_priv);
1585c49fd3aSAlan Cox 
1595c49fd3aSAlan Cox 	/* mmu_gatt ?? */
1605c49fd3aSAlan Cox 	PSB_WSGX32(pg->gatt_start, PSB_CR_BIF_TWOD_REQ_BASE);
161ae012bdcSPatrik Jakobsson 	PSB_RSGX32(PSB_CR_BIF_TWOD_REQ_BASE); /* Post */
162ae012bdcSPatrik Jakobsson 
1635c49fd3aSAlan Cox 	return 0;
1645c49fd3aSAlan Cox }
1655c49fd3aSAlan Cox 
16611b3c20bSGabriel Krisman Bertazi static void psb_driver_unload(struct drm_device *dev)
1675c49fd3aSAlan Cox {
168f71635e8SThomas Zimmermann 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
1695c49fd3aSAlan Cox 
1709083eb38SArthur Borsboom 	/* TODO: Kill vblank etc here */
1715c49fd3aSAlan Cox 
1724ab2c7f1SAlan Cox 	if (dev_priv->backlight_device)
1734ab2c7f1SAlan Cox 		gma_backlight_exit(dev);
1744ab2c7f1SAlan Cox 	psb_modeset_cleanup(dev);
1755c49fd3aSAlan Cox 
1763e712b3cSPatrik Jakobsson 	gma_irq_uninstall(dev);
1773e712b3cSPatrik Jakobsson 
1785c49fd3aSAlan Cox 	if (dev_priv->ops->chip_teardown)
1795c49fd3aSAlan Cox 		dev_priv->ops->chip_teardown(dev);
1806607e024SAlan Cox 
1816607e024SAlan Cox 	psb_intel_opregion_fini(dev);
1825c49fd3aSAlan Cox 
1835c49fd3aSAlan Cox 	if (dev_priv->pf_pd) {
1845c49fd3aSAlan Cox 		psb_mmu_free_pagedir(dev_priv->pf_pd);
1855c49fd3aSAlan Cox 		dev_priv->pf_pd = NULL;
1865c49fd3aSAlan Cox 	}
1875c49fd3aSAlan Cox 	if (dev_priv->mmu) {
1885c49fd3aSAlan Cox 		struct psb_gtt *pg = &dev_priv->gtt;
1895c49fd3aSAlan Cox 
1905c49fd3aSAlan Cox 		psb_mmu_remove_pfn_sequence(
1915c49fd3aSAlan Cox 			psb_mmu_get_default_pd
1925c49fd3aSAlan Cox 			(dev_priv->mmu),
1935c49fd3aSAlan Cox 			pg->mmu_gatt_start,
1945c49fd3aSAlan Cox 			dev_priv->vram_stolen_size >> PAGE_SHIFT);
1955c49fd3aSAlan Cox 		psb_mmu_driver_takedown(dev_priv->mmu);
1965c49fd3aSAlan Cox 		dev_priv->mmu = NULL;
1975c49fd3aSAlan Cox 	}
198d00f44ddSThomas Zimmermann 	psb_gem_mm_fini(dev);
1996069fd81SThomas Zimmermann 	psb_gtt_fini(dev);
2005c49fd3aSAlan Cox 	if (dev_priv->scratch_page) {
2011896344bSAlan Cox 		set_pages_wb(dev_priv->scratch_page, 1);
2025c49fd3aSAlan Cox 		__free_page(dev_priv->scratch_page);
2035c49fd3aSAlan Cox 		dev_priv->scratch_page = NULL;
2045c49fd3aSAlan Cox 	}
2055c49fd3aSAlan Cox 	if (dev_priv->vdc_reg) {
2065c49fd3aSAlan Cox 		iounmap(dev_priv->vdc_reg);
2075c49fd3aSAlan Cox 		dev_priv->vdc_reg = NULL;
2085c49fd3aSAlan Cox 	}
2095c49fd3aSAlan Cox 	if (dev_priv->sgx_reg) {
2105c49fd3aSAlan Cox 		iounmap(dev_priv->sgx_reg);
2115c49fd3aSAlan Cox 		dev_priv->sgx_reg = NULL;
2125c49fd3aSAlan Cox 	}
2132657929dSPatrik Jakobsson 	if (dev_priv->aux_reg) {
2142657929dSPatrik Jakobsson 		iounmap(dev_priv->aux_reg);
2152657929dSPatrik Jakobsson 		dev_priv->aux_reg = NULL;
2162657929dSPatrik Jakobsson 	}
2172657929dSPatrik Jakobsson 	pci_dev_put(dev_priv->aux_pdev);
2185a52b1f2SJan Safrata 	pci_dev_put(dev_priv->lpc_pdev);
2195c49fd3aSAlan Cox 
2204ab2c7f1SAlan Cox 	/* Destroy VBT data */
2214ab2c7f1SAlan Cox 	psb_intel_destroy_bios(dev);
22269831880SThomas Zimmermann 
2235c49fd3aSAlan Cox 	gma_power_uninit(dev);
2245c49fd3aSAlan Cox }
2255c49fd3aSAlan Cox 
226dfbc6cb6SThomas Zimmermann static void psb_device_release(void *data)
227dfbc6cb6SThomas Zimmermann {
228dfbc6cb6SThomas Zimmermann 	struct drm_device *dev = data;
229dfbc6cb6SThomas Zimmermann 
230dfbc6cb6SThomas Zimmermann 	psb_driver_unload(dev);
231dfbc6cb6SThomas Zimmermann }
232dfbc6cb6SThomas Zimmermann 
233f90cd811SArthur Borsboom static int psb_driver_load(struct drm_device *dev, unsigned long flags)
2345c49fd3aSAlan Cox {
235a2c68495SThomas Zimmermann 	struct pci_dev *pdev = to_pci_dev(dev->dev);
236c2f17e60SThomas Zimmermann 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
2372657929dSPatrik Jakobsson 	unsigned long resource_start, resource_len;
2385c49fd3aSAlan Cox 	unsigned long irqflags;
239b1a7d0ddSPatrik Jakobsson 	struct drm_connector_list_iter conn_iter;
2405c49fd3aSAlan Cox 	struct drm_connector *connector;
241367e4408SPatrik Jakobsson 	struct gma_encoder *gma_encoder;
242ae012bdcSPatrik Jakobsson 	struct psb_gtt *pg;
243b1a7d0ddSPatrik Jakobsson 	int ret = -ENOMEM;
2445c49fd3aSAlan Cox 
245c2f17e60SThomas Zimmermann 	/* initializing driver private data */
2465c49fd3aSAlan Cox 
247f90cd811SArthur Borsboom 	dev_priv->ops = (struct psb_ops *)flags;
2485c49fd3aSAlan Cox 
249ae012bdcSPatrik Jakobsson 	pg = &dev_priv->gtt;
250ae012bdcSPatrik Jakobsson 
251a2c68495SThomas Zimmermann 	pci_set_master(pdev);
252466e69b8SDave Airlie 
2535c49fd3aSAlan Cox 	dev_priv->num_pipe = dev_priv->ops->pipes;
2545c49fd3aSAlan Cox 
255a2c68495SThomas Zimmermann 	resource_start = pci_resource_start(pdev, PSB_MMIO_RESOURCE);
2565c49fd3aSAlan Cox 
2575c49fd3aSAlan Cox 	dev_priv->vdc_reg =
2585c49fd3aSAlan Cox 	    ioremap(resource_start + PSB_VDC_OFFSET, PSB_VDC_SIZE);
2595c49fd3aSAlan Cox 	if (!dev_priv->vdc_reg)
2605c49fd3aSAlan Cox 		goto out_err;
2615c49fd3aSAlan Cox 
2625c49fd3aSAlan Cox 	dev_priv->sgx_reg = ioremap(resource_start + dev_priv->ops->sgx_offset,
2635c49fd3aSAlan Cox 							PSB_SGX_SIZE);
2645c49fd3aSAlan Cox 	if (!dev_priv->sgx_reg)
2655c49fd3aSAlan Cox 		goto out_err;
2665c49fd3aSAlan Cox 
2672657929dSPatrik Jakobsson 	if (IS_MRST(dev)) {
268a2c68495SThomas Zimmermann 		int domain = pci_domain_nr(pdev->bus);
269ba99d834SSinan Kaya 
270ba99d834SSinan Kaya 		dev_priv->aux_pdev =
271ba99d834SSinan Kaya 			pci_get_domain_bus_and_slot(domain, 0,
272ba99d834SSinan Kaya 						    PCI_DEVFN(3, 0));
2732657929dSPatrik Jakobsson 
2742657929dSPatrik Jakobsson 		if (dev_priv->aux_pdev) {
2752657929dSPatrik Jakobsson 			resource_start = pci_resource_start(dev_priv->aux_pdev,
2762657929dSPatrik Jakobsson 							    PSB_AUX_RESOURCE);
2772657929dSPatrik Jakobsson 			resource_len = pci_resource_len(dev_priv->aux_pdev,
2782657929dSPatrik Jakobsson 							PSB_AUX_RESOURCE);
2794bdc0d67SChristoph Hellwig 			dev_priv->aux_reg = ioremap(resource_start,
2802657929dSPatrik Jakobsson 							    resource_len);
2812657929dSPatrik Jakobsson 			if (!dev_priv->aux_reg)
2822657929dSPatrik Jakobsson 				goto out_err;
2832657929dSPatrik Jakobsson 
2842657929dSPatrik Jakobsson 			DRM_DEBUG_KMS("Found aux vdc");
2852657929dSPatrik Jakobsson 		} else {
2862657929dSPatrik Jakobsson 			/* Couldn't find the aux vdc so map to primary vdc */
2872657929dSPatrik Jakobsson 			dev_priv->aux_reg = dev_priv->vdc_reg;
2882657929dSPatrik Jakobsson 			DRM_DEBUG_KMS("Couldn't find aux pci device");
2892657929dSPatrik Jakobsson 		}
2902657929dSPatrik Jakobsson 		dev_priv->gmbus_reg = dev_priv->aux_reg;
2915a52b1f2SJan Safrata 
292ba99d834SSinan Kaya 		dev_priv->lpc_pdev =
293ba99d834SSinan Kaya 			pci_get_domain_bus_and_slot(domain, 0,
294ba99d834SSinan Kaya 						    PCI_DEVFN(31, 0));
2955a52b1f2SJan Safrata 		if (dev_priv->lpc_pdev) {
2965a52b1f2SJan Safrata 			pci_read_config_word(dev_priv->lpc_pdev, PSB_LPC_GBA,
2975a52b1f2SJan Safrata 				&dev_priv->lpc_gpio_base);
2985a52b1f2SJan Safrata 			pci_write_config_dword(dev_priv->lpc_pdev, PSB_LPC_GBA,
2995a52b1f2SJan Safrata 				(u32)dev_priv->lpc_gpio_base | (1L<<31));
3005a52b1f2SJan Safrata 			pci_read_config_word(dev_priv->lpc_pdev, PSB_LPC_GBA,
3015a52b1f2SJan Safrata 				&dev_priv->lpc_gpio_base);
3025a52b1f2SJan Safrata 			dev_priv->lpc_gpio_base &= 0xffc0;
3035a52b1f2SJan Safrata 			if (dev_priv->lpc_gpio_base)
3045a52b1f2SJan Safrata 				DRM_DEBUG_KMS("Found LPC GPIO at 0x%04x\n",
3055a52b1f2SJan Safrata 						dev_priv->lpc_gpio_base);
3065a52b1f2SJan Safrata 			else {
3075a52b1f2SJan Safrata 				pci_dev_put(dev_priv->lpc_pdev);
3085a52b1f2SJan Safrata 				dev_priv->lpc_pdev = NULL;
3095a52b1f2SJan Safrata 			}
3105a52b1f2SJan Safrata 		}
3112657929dSPatrik Jakobsson 	} else {
3122657929dSPatrik Jakobsson 		dev_priv->gmbus_reg = dev_priv->vdc_reg;
3132657929dSPatrik Jakobsson 	}
3142657929dSPatrik Jakobsson 
315d839ede4SAlan Cox 	psb_intel_opregion_setup(dev);
316d839ede4SAlan Cox 
3175c49fd3aSAlan Cox 	ret = dev_priv->ops->chip_setup(dev);
3185c49fd3aSAlan Cox 	if (ret)
3195c49fd3aSAlan Cox 		goto out_err;
3205c49fd3aSAlan Cox 
3215c49fd3aSAlan Cox 	/* Init OSPM support */
3225c49fd3aSAlan Cox 	gma_power_init(dev);
3235c49fd3aSAlan Cox 
3245c49fd3aSAlan Cox 	ret = -ENOMEM;
3255c49fd3aSAlan Cox 
3265c49fd3aSAlan Cox 	dev_priv->scratch_page = alloc_page(GFP_DMA32 | __GFP_ZERO);
3275c49fd3aSAlan Cox 	if (!dev_priv->scratch_page)
3285c49fd3aSAlan Cox 		goto out_err;
3295c49fd3aSAlan Cox 
3305c49fd3aSAlan Cox 	set_pages_uc(dev_priv->scratch_page, 1);
3315c49fd3aSAlan Cox 
33297bd66c4SThomas Zimmermann 	ret = psb_gtt_init(dev);
3335c49fd3aSAlan Cox 	if (ret)
3345c49fd3aSAlan Cox 		goto out_err;
335d00f44ddSThomas Zimmermann 	ret = psb_gem_mm_init(dev);
336d00f44ddSThomas Zimmermann 	if (ret)
337d00f44ddSThomas Zimmermann 		goto out_err;
3385c49fd3aSAlan Cox 
3396926872aSJialin Zhang 	ret = -ENOMEM;
3406926872aSJialin Zhang 
3418f214d43SYang Li 	dev_priv->mmu = psb_mmu_driver_init(dev, 1, 0, NULL);
3425c49fd3aSAlan Cox 	if (!dev_priv->mmu)
3435c49fd3aSAlan Cox 		goto out_err;
3445c49fd3aSAlan Cox 
3455c49fd3aSAlan Cox 	dev_priv->pf_pd = psb_mmu_alloc_pd(dev_priv->mmu, 1, 0);
3465c49fd3aSAlan Cox 	if (!dev_priv->pf_pd)
3475c49fd3aSAlan Cox 		goto out_err;
3485c49fd3aSAlan Cox 
3495c49fd3aSAlan Cox 	ret = psb_do_init(dev);
3505c49fd3aSAlan Cox 	if (ret)
3515c49fd3aSAlan Cox 		return ret;
3525c49fd3aSAlan Cox 
353ae012bdcSPatrik Jakobsson 	/* Add stolen memory to SGX MMU */
354ae012bdcSPatrik Jakobsson 	ret = psb_mmu_insert_pfn_sequence(psb_mmu_get_default_pd(dev_priv->mmu),
355ae012bdcSPatrik Jakobsson 					  dev_priv->stolen_base >> PAGE_SHIFT,
356ae012bdcSPatrik Jakobsson 					  pg->gatt_start,
357ae012bdcSPatrik Jakobsson 					  pg->stolen_size >> PAGE_SHIFT, 0);
358ae012bdcSPatrik Jakobsson 
359ae012bdcSPatrik Jakobsson 	psb_mmu_set_pd_context(psb_mmu_get_default_pd(dev_priv->mmu), 0);
360ae012bdcSPatrik Jakobsson 	psb_mmu_set_pd_context(dev_priv->pf_pd, 1);
361ae012bdcSPatrik Jakobsson 
3625c49fd3aSAlan Cox 	PSB_WSGX32(0x20000000, PSB_CR_PDS_EXEC_BASE);
3635c49fd3aSAlan Cox 	PSB_WSGX32(0x30000000, PSB_CR_BIF_3D_REQ_BASE);
3645c49fd3aSAlan Cox 
365d839ede4SAlan Cox 	acpi_video_register();
3665c49fd3aSAlan Cox 
3679083eb38SArthur Borsboom 	/* Setup vertical blanking handling */
3685c49fd3aSAlan Cox 	ret = drm_vblank_init(dev, dev_priv->num_pipe);
3695c49fd3aSAlan Cox 	if (ret)
3705c49fd3aSAlan Cox 		goto out_err;
3715c49fd3aSAlan Cox 
3725c49fd3aSAlan Cox 	/*
3735c49fd3aSAlan Cox 	 * Install interrupt handlers prior to powering off SGX or else we will
3745c49fd3aSAlan Cox 	 * crash.
3755c49fd3aSAlan Cox 	 */
3765c49fd3aSAlan Cox 	dev_priv->vdc_irq_mask = 0;
3775c49fd3aSAlan Cox 	dev_priv->pipestat[0] = 0;
3785c49fd3aSAlan Cox 	dev_priv->pipestat[1] = 0;
3795c49fd3aSAlan Cox 	dev_priv->pipestat[2] = 0;
3805c49fd3aSAlan Cox 	spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
3815c49fd3aSAlan Cox 	PSB_WVDC32(0xFFFFFFFF, PSB_HWSTAM);
3825c49fd3aSAlan Cox 	PSB_WVDC32(0x00000000, PSB_INT_ENABLE_R);
3835c49fd3aSAlan Cox 	PSB_WVDC32(0xFFFFFFFF, PSB_INT_MASK_R);
3845c49fd3aSAlan Cox 	spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
38562363a48SAlan Cox 
3869b6a1657SHans de Goede 	gma_irq_install(dev);
3875c49fd3aSAlan Cox 
3885c49fd3aSAlan Cox 	dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
3895c49fd3aSAlan Cox 
3905c49fd3aSAlan Cox 	psb_modeset_init(dev);
3915c49fd3aSAlan Cox 	psb_fbdev_init(dev);
3925c49fd3aSAlan Cox 	drm_kms_helper_poll_init(dev);
3935c49fd3aSAlan Cox 
394b1a7d0ddSPatrik Jakobsson 	/* Only add backlight support if we have LVDS or MIPI output */
395b1a7d0ddSPatrik Jakobsson 	drm_connector_list_iter_begin(dev, &conn_iter);
396b1a7d0ddSPatrik Jakobsson 	drm_for_each_connector_iter(connector, &conn_iter) {
397367e4408SPatrik Jakobsson 		gma_encoder = gma_attached_encoder(connector);
3985c49fd3aSAlan Cox 
399ac2f033aSXiaomeng Tong 		if (gma_encoder->type == INTEL_OUTPUT_LVDS ||
400ac2f033aSXiaomeng Tong 		    gma_encoder->type == INTEL_OUTPUT_MIPI) {
4015c49fd3aSAlan Cox 			ret = gma_backlight_init(dev);
4025c49fd3aSAlan Cox 			break;
4035c49fd3aSAlan Cox 		}
4045c49fd3aSAlan Cox 	}
405b1a7d0ddSPatrik Jakobsson 	drm_connector_list_iter_end(&conn_iter);
4065c49fd3aSAlan Cox 
4075c49fd3aSAlan Cox 	if (ret)
4085c49fd3aSAlan Cox 		return ret;
409166973e5SAlan Cox 	psb_intel_opregion_enable_asle(dev);
4105c49fd3aSAlan Cox #if 0
4119083eb38SArthur Borsboom 	/* Enable runtime pm at last */
412a2c68495SThomas Zimmermann 	pm_runtime_enable(dev->dev);
413a2c68495SThomas Zimmermann 	pm_runtime_set_active(dev->dev);
4145c49fd3aSAlan Cox #endif
415dfbc6cb6SThomas Zimmermann 
416dfbc6cb6SThomas Zimmermann 	return devm_add_action_or_reset(dev->dev, psb_device_release, dev);
417dfbc6cb6SThomas Zimmermann 
4185c49fd3aSAlan Cox out_err:
4195c49fd3aSAlan Cox 	psb_driver_unload(dev);
4205c49fd3aSAlan Cox 	return ret;
4215c49fd3aSAlan Cox }
4225c49fd3aSAlan Cox 
4235c49fd3aSAlan Cox static inline void get_brightness(struct backlight_device *bd)
4245c49fd3aSAlan Cox {
4255c49fd3aSAlan Cox #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
4265c49fd3aSAlan Cox 	if (bd) {
4275c49fd3aSAlan Cox 		bd->props.brightness = bd->ops->get_brightness(bd);
4285c49fd3aSAlan Cox 		backlight_update_status(bd);
4295c49fd3aSAlan Cox 	}
4305c49fd3aSAlan Cox #endif
4315c49fd3aSAlan Cox }
4325c49fd3aSAlan Cox 
433f90cd811SArthur Borsboom static int psb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
434f90cd811SArthur Borsboom {
435c2f17e60SThomas Zimmermann 	struct drm_psb_private *dev_priv;
43650cbe0e3SThomas Zimmermann 	struct drm_device *dev;
43750cbe0e3SThomas Zimmermann 	int ret;
43850cbe0e3SThomas Zimmermann 
439035f79f9SThomas Zimmermann 	/*
440035f79f9SThomas Zimmermann 	 * We cannot yet easily find the framebuffer's location in memory. So
441035f79f9SThomas Zimmermann 	 * remove all framebuffers here.
442035f79f9SThomas Zimmermann 	 *
443035f79f9SThomas Zimmermann 	 * TODO: Refactor psb_driver_load() to map vdc_reg earlier. Then we
444035f79f9SThomas Zimmermann 	 *       might be able to read the framebuffer range from the device.
445035f79f9SThomas Zimmermann 	 */
446035f79f9SThomas Zimmermann 	ret = drm_aperture_remove_framebuffers(true, &driver);
447035f79f9SThomas Zimmermann 	if (ret)
448035f79f9SThomas Zimmermann 		return ret;
449035f79f9SThomas Zimmermann 
4502df94510SThomas Zimmermann 	ret = pcim_enable_device(pdev);
45150cbe0e3SThomas Zimmermann 	if (ret)
45250cbe0e3SThomas Zimmermann 		return ret;
45350cbe0e3SThomas Zimmermann 
454c2f17e60SThomas Zimmermann 	dev_priv = devm_drm_dev_alloc(&pdev->dev, &driver, struct drm_psb_private, dev);
455c2f17e60SThomas Zimmermann 	if (IS_ERR(dev_priv))
456ebe180d3SYang Yingliang 		return PTR_ERR(dev_priv);
457c2f17e60SThomas Zimmermann 	dev = &dev_priv->dev;
458f90cd811SArthur Borsboom 
45950cbe0e3SThomas Zimmermann 	pci_set_drvdata(pdev, dev);
46050cbe0e3SThomas Zimmermann 
46150cbe0e3SThomas Zimmermann 	ret = psb_driver_load(dev, ent->driver_data);
46250cbe0e3SThomas Zimmermann 	if (ret)
463c2f17e60SThomas Zimmermann 		return ret;
46450cbe0e3SThomas Zimmermann 
46550cbe0e3SThomas Zimmermann 	ret = drm_dev_register(dev, ent->driver_data);
46650cbe0e3SThomas Zimmermann 	if (ret)
467dfbc6cb6SThomas Zimmermann 		return ret;
46850cbe0e3SThomas Zimmermann 
46950cbe0e3SThomas Zimmermann 	return 0;
47050cbe0e3SThomas Zimmermann }
471f90cd811SArthur Borsboom 
472f90cd811SArthur Borsboom static void psb_pci_remove(struct pci_dev *pdev)
4735c49fd3aSAlan Cox {
4745c49fd3aSAlan Cox 	struct drm_device *dev = pci_get_drvdata(pdev);
47550cbe0e3SThomas Zimmermann 
47650cbe0e3SThomas Zimmermann 	drm_dev_unregister(dev);
4775c49fd3aSAlan Cox }
4785c49fd3aSAlan Cox 
4795c49fd3aSAlan Cox static const struct dev_pm_ops psb_pm_ops = {
4805c49fd3aSAlan Cox 	.resume = gma_power_resume,
4815c49fd3aSAlan Cox 	.suspend = gma_power_suspend,
482888eef2eSPatrik Jakobsson 	.thaw = gma_power_thaw,
483888eef2eSPatrik Jakobsson 	.freeze = gma_power_freeze,
484888eef2eSPatrik Jakobsson 	.restore = gma_power_restore,
4855c49fd3aSAlan Cox 	.runtime_suspend = psb_runtime_suspend,
4865c49fd3aSAlan Cox 	.runtime_resume = psb_runtime_resume,
4875c49fd3aSAlan Cox 	.runtime_idle = psb_runtime_idle,
4885c49fd3aSAlan Cox };
4895c49fd3aSAlan Cox 
4901595c568SIlija Hadzic static const struct file_operations psb_gem_fops = {
4911595c568SIlija Hadzic 	.owner = THIS_MODULE,
4921595c568SIlija Hadzic 	.open = drm_open,
4931595c568SIlija Hadzic 	.release = drm_release,
494*d35a4bf6SHans de Goede 	.unlocked_ioctl = drm_ioctl,
4950a97c81aSPatrik Jakobsson 	.compat_ioctl = drm_compat_ioctl,
4961595c568SIlija Hadzic 	.mmap = drm_gem_mmap,
4971595c568SIlija Hadzic 	.poll = drm_poll,
4981595c568SIlija Hadzic 	.read = drm_read,
4991595c568SIlija Hadzic };
5001595c568SIlija Hadzic 
50170a59dd8SDaniel Vetter static const struct drm_driver driver = {
5021ff49481SDaniel Vetter 	.driver_features = DRIVER_MODESET | DRIVER_GEM,
503cdc13f68SNoralf Trønnes 	.lastclose = drm_fb_helper_lastclose,
5045c49fd3aSAlan Cox 
505f95aeb17SDamien Lespiau 	.num_ioctls = ARRAY_SIZE(psb_ioctls),
5065c49fd3aSAlan Cox 
5075c49fd3aSAlan Cox 	.dumb_create = psb_gem_dumb_create,
508f90cd811SArthur Borsboom 	.ioctls = psb_ioctls,
5091595c568SIlija Hadzic 	.fops = &psb_gem_fops,
5105c49fd3aSAlan Cox 	.name = DRIVER_NAME,
5115c49fd3aSAlan Cox 	.desc = DRIVER_DESC,
512f90cd811SArthur Borsboom 	.date = DRIVER_DATE,
513f90cd811SArthur Borsboom 	.major = DRIVER_MAJOR,
514f90cd811SArthur Borsboom 	.minor = DRIVER_MINOR,
515f90cd811SArthur Borsboom 	.patchlevel = DRIVER_PATCHLEVEL
5165c49fd3aSAlan Cox };
5175c49fd3aSAlan Cox 
5185c49fd3aSAlan Cox static struct pci_driver psb_pci_driver = {
5195c49fd3aSAlan Cox 	.name = DRIVER_NAME,
5205c49fd3aSAlan Cox 	.id_table = pciidlist,
521f90cd811SArthur Borsboom 	.probe = psb_pci_probe,
522f90cd811SArthur Borsboom 	.remove = psb_pci_remove,
523f90cd811SArthur Borsboom 	.driver.pm = &psb_pm_ops,
5245c49fd3aSAlan Cox };
5255c49fd3aSAlan Cox 
5265c49fd3aSAlan Cox static int __init psb_init(void)
5275c49fd3aSAlan Cox {
528ba4a28bbSJavier Martinez Canillas 	if (drm_firmware_drivers_only())
529ba4a28bbSJavier Martinez Canillas 		return -ENODEV;
530ba4a28bbSJavier Martinez Canillas 
53110631d72SDaniel Vetter 	return pci_register_driver(&psb_pci_driver);
5325c49fd3aSAlan Cox }
5335c49fd3aSAlan Cox 
5345c49fd3aSAlan Cox static void __exit psb_exit(void)
5355c49fd3aSAlan Cox {
53610631d72SDaniel Vetter 	pci_unregister_driver(&psb_pci_driver);
5375c49fd3aSAlan Cox }
5385c49fd3aSAlan Cox 
5395c49fd3aSAlan Cox late_initcall(psb_init);
5405c49fd3aSAlan Cox module_exit(psb_exit);
5415c49fd3aSAlan Cox 
542f90cd811SArthur Borsboom MODULE_AUTHOR(DRIVER_AUTHOR);
5435c49fd3aSAlan Cox MODULE_DESCRIPTION(DRIVER_DESC);
5440df12b3fSGreg Kroah-Hartman MODULE_LICENSE("GPL");
545