1 // SPDX-License-Identifier: GPL-2.0-only
2 /**************************************************************************
3 * Copyright (c) 2007-2011, Intel Corporation.
5 * Copyright (c) 2008, Tungsten Graphics, Inc. Cedar Park, TX., USA.
8 **************************************************************************/
10 #include <linux/cpu.h>
11 #include <linux/module.h>
12 #include <linux/notifier.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/spinlock.h>
15 #include <linux/delay.h>
17 #include <asm/set_memory.h>
19 #include <acpi/video.h>
22 #include <drm/drm_aperture.h>
23 #include <drm/drm_drv.h>
24 #include <drm/drm_file.h>
25 #include <drm/drm_ioctl.h>
26 #include <drm/drm_pciids.h>
27 #include <drm/drm_vblank.h>
29 #include "framebuffer.h"
31 #include "intel_bios.h"
35 #include "psb_intel_reg.h"
39 static const struct drm_driver driver;
40 static int psb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
43 * The table below contains a mapping of the PCI vendor ID and the PCI Device ID
44 * to the different groups of PowerVR 5-series chip designs
46 * 0x8086 = Intel Corporation
48 * PowerVR SGX535 - Poulsbo - Intel GMA 500, Intel Atom Z5xx
49 * PowerVR SGX535 - Moorestown - Intel GMA 600
50 * PowerVR SGX535 - Oaktrail - Intel GMA 600, Intel Atom Z6xx, E6xx
51 * PowerVR SGX545 - Cedartrail - Intel GMA 3600, Intel Atom D2500, N2600
52 * PowerVR SGX545 - Cedartrail - Intel GMA 3650, Intel Atom D2550, D2700,
55 static const struct pci_device_id pciidlist[] = {
57 { 0x8086, 0x8108, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &psb_chip_ops },
58 { 0x8086, 0x8109, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &psb_chip_ops },
60 { 0x8086, 0x4100, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops },
61 { 0x8086, 0x4101, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops },
62 { 0x8086, 0x4102, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops },
63 { 0x8086, 0x4103, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops },
64 { 0x8086, 0x4104, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops },
65 { 0x8086, 0x4105, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops },
66 { 0x8086, 0x4106, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops },
67 { 0x8086, 0x4107, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops },
68 { 0x8086, 0x4108, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops },
70 { 0x8086, 0x0be0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
71 { 0x8086, 0x0be1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
72 { 0x8086, 0x0be2, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
73 { 0x8086, 0x0be3, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
74 { 0x8086, 0x0be4, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
75 { 0x8086, 0x0be5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
76 { 0x8086, 0x0be6, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
77 { 0x8086, 0x0be7, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
78 { 0x8086, 0x0be8, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
79 { 0x8086, 0x0be9, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
80 { 0x8086, 0x0bea, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
81 { 0x8086, 0x0beb, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
82 { 0x8086, 0x0bec, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
83 { 0x8086, 0x0bed, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
84 { 0x8086, 0x0bee, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
85 { 0x8086, 0x0bef, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops },
88 MODULE_DEVICE_TABLE(pci, pciidlist);
93 static const struct drm_ioctl_desc psb_ioctls[] = {
97 * psb_spank - reset the 2D engine
98 * @dev_priv: our PSB DRM device
100 * Soft reset the graphics engine and then reload the necessary registers.
102 static void psb_spank(struct drm_psb_private *dev_priv)
104 PSB_WSGX32(_PSB_CS_RESET_BIF_RESET | _PSB_CS_RESET_DPM_RESET |
105 _PSB_CS_RESET_TA_RESET | _PSB_CS_RESET_USE_RESET |
106 _PSB_CS_RESET_ISP_RESET | _PSB_CS_RESET_TSP_RESET |
107 _PSB_CS_RESET_TWOD_RESET, PSB_CR_SOFT_RESET);
108 PSB_RSGX32(PSB_CR_SOFT_RESET);
112 PSB_WSGX32(0, PSB_CR_SOFT_RESET);
114 PSB_WSGX32(PSB_RSGX32(PSB_CR_BIF_CTRL) | _PSB_CB_CTRL_CLEAR_FAULT,
117 (void) PSB_RSGX32(PSB_CR_BIF_CTRL);
120 PSB_WSGX32(PSB_RSGX32(PSB_CR_BIF_CTRL) & ~_PSB_CB_CTRL_CLEAR_FAULT,
122 (void) PSB_RSGX32(PSB_CR_BIF_CTRL);
123 PSB_WSGX32(dev_priv->gtt.gatt_start, PSB_CR_BIF_TWOD_REQ_BASE);
126 static int psb_do_init(struct drm_device *dev)
128 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
129 struct psb_gtt *pg = &dev_priv->gtt;
133 if (pg->mmu_gatt_start & 0x0FFFFFFF) {
134 dev_err(dev->dev, "Gatt must be 256M aligned. This is a bug.\n");
138 stolen_gtt = (pg->stolen_size >> PAGE_SHIFT) * 4;
139 stolen_gtt = (stolen_gtt + PAGE_SIZE - 1) >> PAGE_SHIFT;
140 stolen_gtt = (stolen_gtt < pg->gtt_pages) ? stolen_gtt : pg->gtt_pages;
142 dev_priv->gatt_free_offset = pg->mmu_gatt_start +
143 (stolen_gtt << PAGE_SHIFT) * 1024;
145 spin_lock_init(&dev_priv->irqmask_lock);
147 PSB_WSGX32(0x00000000, PSB_CR_BIF_BANK0);
148 PSB_WSGX32(0x00000000, PSB_CR_BIF_BANK1);
149 PSB_RSGX32(PSB_CR_BIF_BANK1);
151 /* Do not bypass any MMU access, let them pagefault instead */
152 PSB_WSGX32((PSB_RSGX32(PSB_CR_BIF_CTRL) & ~_PSB_MMU_ER_MASK),
154 PSB_RSGX32(PSB_CR_BIF_CTRL);
159 PSB_WSGX32(pg->gatt_start, PSB_CR_BIF_TWOD_REQ_BASE);
160 PSB_RSGX32(PSB_CR_BIF_TWOD_REQ_BASE); /* Post */
165 static void psb_driver_unload(struct drm_device *dev)
167 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
169 /* TODO: Kill vblank etc here */
171 gma_backlight_exit(dev);
172 psb_modeset_cleanup(dev);
174 gma_irq_uninstall(dev);
176 if (dev_priv->ops->chip_teardown)
177 dev_priv->ops->chip_teardown(dev);
179 psb_intel_opregion_fini(dev);
181 if (dev_priv->pf_pd) {
182 psb_mmu_free_pagedir(dev_priv->pf_pd);
183 dev_priv->pf_pd = NULL;
186 struct psb_gtt *pg = &dev_priv->gtt;
188 psb_mmu_remove_pfn_sequence(
189 psb_mmu_get_default_pd
192 dev_priv->vram_stolen_size >> PAGE_SHIFT);
193 psb_mmu_driver_takedown(dev_priv->mmu);
194 dev_priv->mmu = NULL;
196 psb_gem_mm_fini(dev);
198 if (dev_priv->scratch_page) {
199 set_pages_wb(dev_priv->scratch_page, 1);
200 __free_page(dev_priv->scratch_page);
201 dev_priv->scratch_page = NULL;
203 if (dev_priv->vdc_reg) {
204 iounmap(dev_priv->vdc_reg);
205 dev_priv->vdc_reg = NULL;
207 if (dev_priv->sgx_reg) {
208 iounmap(dev_priv->sgx_reg);
209 dev_priv->sgx_reg = NULL;
211 if (dev_priv->aux_reg) {
212 iounmap(dev_priv->aux_reg);
213 dev_priv->aux_reg = NULL;
215 pci_dev_put(dev_priv->aux_pdev);
216 pci_dev_put(dev_priv->lpc_pdev);
218 /* Destroy VBT data */
219 psb_intel_destroy_bios(dev);
221 gma_power_uninit(dev);
224 static void psb_device_release(void *data)
226 struct drm_device *dev = data;
228 psb_driver_unload(dev);
231 static int psb_driver_load(struct drm_device *dev, unsigned long flags)
233 struct pci_dev *pdev = to_pci_dev(dev->dev);
234 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
235 unsigned long resource_start, resource_len;
236 unsigned long irqflags;
237 struct drm_connector_list_iter conn_iter;
238 struct drm_connector *connector;
239 struct gma_encoder *gma_encoder;
243 /* initializing driver private data */
245 dev_priv->ops = (struct psb_ops *)flags;
249 pci_set_master(pdev);
251 dev_priv->num_pipe = dev_priv->ops->pipes;
253 resource_start = pci_resource_start(pdev, PSB_MMIO_RESOURCE);
256 ioremap(resource_start + PSB_VDC_OFFSET, PSB_VDC_SIZE);
257 if (!dev_priv->vdc_reg)
260 dev_priv->sgx_reg = ioremap(resource_start + dev_priv->ops->sgx_offset,
262 if (!dev_priv->sgx_reg)
266 int domain = pci_domain_nr(pdev->bus);
269 pci_get_domain_bus_and_slot(domain, 0,
272 if (dev_priv->aux_pdev) {
273 resource_start = pci_resource_start(dev_priv->aux_pdev,
275 resource_len = pci_resource_len(dev_priv->aux_pdev,
277 dev_priv->aux_reg = ioremap(resource_start,
279 if (!dev_priv->aux_reg)
282 DRM_DEBUG_KMS("Found aux vdc");
284 /* Couldn't find the aux vdc so map to primary vdc */
285 dev_priv->aux_reg = dev_priv->vdc_reg;
286 DRM_DEBUG_KMS("Couldn't find aux pci device");
288 dev_priv->gmbus_reg = dev_priv->aux_reg;
291 pci_get_domain_bus_and_slot(domain, 0,
293 if (dev_priv->lpc_pdev) {
294 pci_read_config_word(dev_priv->lpc_pdev, PSB_LPC_GBA,
295 &dev_priv->lpc_gpio_base);
296 pci_write_config_dword(dev_priv->lpc_pdev, PSB_LPC_GBA,
297 (u32)dev_priv->lpc_gpio_base | (1L<<31));
298 pci_read_config_word(dev_priv->lpc_pdev, PSB_LPC_GBA,
299 &dev_priv->lpc_gpio_base);
300 dev_priv->lpc_gpio_base &= 0xffc0;
301 if (dev_priv->lpc_gpio_base)
302 DRM_DEBUG_KMS("Found LPC GPIO at 0x%04x\n",
303 dev_priv->lpc_gpio_base);
305 pci_dev_put(dev_priv->lpc_pdev);
306 dev_priv->lpc_pdev = NULL;
310 dev_priv->gmbus_reg = dev_priv->vdc_reg;
313 psb_intel_opregion_setup(dev);
315 ret = dev_priv->ops->chip_setup(dev);
319 /* Init OSPM support */
324 dev_priv->scratch_page = alloc_page(GFP_DMA32 | __GFP_ZERO);
325 if (!dev_priv->scratch_page)
328 set_pages_uc(dev_priv->scratch_page, 1);
330 ret = psb_gtt_init(dev);
333 ret = psb_gem_mm_init(dev);
339 dev_priv->mmu = psb_mmu_driver_init(dev, 1, 0, NULL);
343 dev_priv->pf_pd = psb_mmu_alloc_pd(dev_priv->mmu, 1, 0);
344 if (!dev_priv->pf_pd)
347 ret = psb_do_init(dev);
351 /* Add stolen memory to SGX MMU */
352 ret = psb_mmu_insert_pfn_sequence(psb_mmu_get_default_pd(dev_priv->mmu),
353 dev_priv->stolen_base >> PAGE_SHIFT,
355 pg->stolen_size >> PAGE_SHIFT, 0);
357 psb_mmu_set_pd_context(psb_mmu_get_default_pd(dev_priv->mmu), 0);
358 psb_mmu_set_pd_context(dev_priv->pf_pd, 1);
360 PSB_WSGX32(0x20000000, PSB_CR_PDS_EXEC_BASE);
361 PSB_WSGX32(0x30000000, PSB_CR_BIF_3D_REQ_BASE);
363 acpi_video_register();
365 /* Setup vertical blanking handling */
366 ret = drm_vblank_init(dev, dev_priv->num_pipe);
371 * Install interrupt handlers prior to powering off SGX or else we will
374 dev_priv->vdc_irq_mask = 0;
375 dev_priv->pipestat[0] = 0;
376 dev_priv->pipestat[1] = 0;
377 dev_priv->pipestat[2] = 0;
378 spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
379 PSB_WVDC32(0xFFFFFFFF, PSB_HWSTAM);
380 PSB_WVDC32(0x00000000, PSB_INT_ENABLE_R);
381 PSB_WVDC32(0xFFFFFFFF, PSB_INT_MASK_R);
382 spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
384 gma_irq_install(dev);
386 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
388 psb_modeset_init(dev);
389 drm_kms_helper_poll_init(dev);
391 /* Only add backlight support if we have LVDS or MIPI output */
392 drm_connector_list_iter_begin(dev, &conn_iter);
393 drm_for_each_connector_iter(connector, &conn_iter) {
394 gma_encoder = gma_attached_encoder(connector);
396 if (gma_encoder->type == INTEL_OUTPUT_LVDS ||
397 gma_encoder->type == INTEL_OUTPUT_MIPI) {
398 ret = gma_backlight_init(dev);
400 acpi_video_register_backlight();
404 drm_connector_list_iter_end(&conn_iter);
408 psb_intel_opregion_enable_asle(dev);
410 return devm_add_action_or_reset(dev->dev, psb_device_release, dev);
413 psb_driver_unload(dev);
417 static int psb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
419 struct drm_psb_private *dev_priv;
420 struct drm_device *dev;
424 * We cannot yet easily find the framebuffer's location in memory. So
425 * remove all framebuffers here. Note that we still want the pci special
426 * handling to kick out vgacon.
428 * TODO: Refactor psb_driver_load() to map vdc_reg earlier. Then we
429 * might be able to read the framebuffer range from the device.
431 ret = drm_aperture_remove_framebuffers(&driver);
435 ret = drm_aperture_remove_conflicting_pci_framebuffers(pdev, &driver);
439 ret = pcim_enable_device(pdev);
443 dev_priv = devm_drm_dev_alloc(&pdev->dev, &driver, struct drm_psb_private, dev);
444 if (IS_ERR(dev_priv))
445 return PTR_ERR(dev_priv);
446 dev = &dev_priv->dev;
448 pci_set_drvdata(pdev, dev);
450 ret = psb_driver_load(dev, ent->driver_data);
454 ret = drm_dev_register(dev, ent->driver_data);
458 psb_fbdev_setup(dev_priv);
463 static void psb_pci_remove(struct pci_dev *pdev)
465 struct drm_device *dev = pci_get_drvdata(pdev);
467 drm_dev_unregister(dev);
470 static DEFINE_RUNTIME_DEV_PM_OPS(psb_pm_ops, gma_power_suspend, gma_power_resume, NULL);
472 static const struct file_operations psb_gem_fops = {
473 .owner = THIS_MODULE,
475 .release = drm_release,
476 .unlocked_ioctl = drm_ioctl,
477 .compat_ioctl = drm_compat_ioctl,
478 .mmap = drm_gem_mmap,
483 static const struct drm_driver driver = {
484 .driver_features = DRIVER_MODESET | DRIVER_GEM,
486 .num_ioctls = ARRAY_SIZE(psb_ioctls),
488 .dumb_create = psb_gem_dumb_create,
489 .ioctls = psb_ioctls,
490 .fops = &psb_gem_fops,
494 .major = DRIVER_MAJOR,
495 .minor = DRIVER_MINOR,
496 .patchlevel = DRIVER_PATCHLEVEL
499 static struct pci_driver psb_pci_driver = {
501 .id_table = pciidlist,
502 .probe = psb_pci_probe,
503 .remove = psb_pci_remove,
504 .driver.pm = &psb_pm_ops,
507 static int __init psb_init(void)
509 if (drm_firmware_drivers_only())
512 return pci_register_driver(&psb_pci_driver);
515 static void __exit psb_exit(void)
517 pci_unregister_driver(&psb_pci_driver);
520 late_initcall(psb_init);
521 module_exit(psb_exit);
523 MODULE_AUTHOR(DRIVER_AUTHOR);
524 MODULE_DESCRIPTION(DRIVER_DESC);
525 MODULE_LICENSE("GPL");