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_fb_helper.h>
25 #include <drm/drm_file.h>
26 #include <drm/drm_ioctl.h>
27 #include <drm/drm_pciids.h>
28 #include <drm/drm_vblank.h>
30 #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 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 if (dev_priv->backlight_device)
172 gma_backlight_exit(dev);
173 psb_modeset_cleanup(dev);
175 if (dev_priv->ops->chip_teardown)
176 dev_priv->ops->chip_teardown(dev);
178 psb_intel_opregion_fini(dev);
180 if (dev_priv->pf_pd) {
181 psb_mmu_free_pagedir(dev_priv->pf_pd);
182 dev_priv->pf_pd = NULL;
185 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);
194 psb_mmu_driver_takedown(dev_priv->mmu);
195 dev_priv->mmu = NULL;
197 psb_gtt_takedown(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;
238 struct drm_connector *connector;
239 struct gma_encoder *gma_encoder;
242 /* initializing driver private data */
244 dev_priv->ops = (struct psb_ops *)flags;
248 pci_set_master(pdev);
250 dev_priv->num_pipe = dev_priv->ops->pipes;
252 resource_start = pci_resource_start(pdev, PSB_MMIO_RESOURCE);
255 ioremap(resource_start + PSB_VDC_OFFSET, PSB_VDC_SIZE);
256 if (!dev_priv->vdc_reg)
259 dev_priv->sgx_reg = ioremap(resource_start + dev_priv->ops->sgx_offset,
261 if (!dev_priv->sgx_reg)
265 int domain = pci_domain_nr(pdev->bus);
268 pci_get_domain_bus_and_slot(domain, 0,
271 if (dev_priv->aux_pdev) {
272 resource_start = pci_resource_start(dev_priv->aux_pdev,
274 resource_len = pci_resource_len(dev_priv->aux_pdev,
276 dev_priv->aux_reg = ioremap(resource_start,
278 if (!dev_priv->aux_reg)
281 DRM_DEBUG_KMS("Found aux vdc");
283 /* Couldn't find the aux vdc so map to primary vdc */
284 dev_priv->aux_reg = dev_priv->vdc_reg;
285 DRM_DEBUG_KMS("Couldn't find aux pci device");
287 dev_priv->gmbus_reg = dev_priv->aux_reg;
290 pci_get_domain_bus_and_slot(domain, 0,
292 if (dev_priv->lpc_pdev) {
293 pci_read_config_word(dev_priv->lpc_pdev, PSB_LPC_GBA,
294 &dev_priv->lpc_gpio_base);
295 pci_write_config_dword(dev_priv->lpc_pdev, PSB_LPC_GBA,
296 (u32)dev_priv->lpc_gpio_base | (1L<<31));
297 pci_read_config_word(dev_priv->lpc_pdev, PSB_LPC_GBA,
298 &dev_priv->lpc_gpio_base);
299 dev_priv->lpc_gpio_base &= 0xffc0;
300 if (dev_priv->lpc_gpio_base)
301 DRM_DEBUG_KMS("Found LPC GPIO at 0x%04x\n",
302 dev_priv->lpc_gpio_base);
304 pci_dev_put(dev_priv->lpc_pdev);
305 dev_priv->lpc_pdev = NULL;
309 dev_priv->gmbus_reg = dev_priv->vdc_reg;
312 psb_intel_opregion_setup(dev);
314 ret = dev_priv->ops->chip_setup(dev);
318 /* Init OSPM support */
323 dev_priv->scratch_page = alloc_page(GFP_DMA32 | __GFP_ZERO);
324 if (!dev_priv->scratch_page)
327 set_pages_uc(dev_priv->scratch_page, 1);
329 ret = psb_gtt_init(dev, 0);
335 dev_priv->mmu = psb_mmu_driver_init(dev, 1, 0, NULL);
339 dev_priv->pf_pd = psb_mmu_alloc_pd(dev_priv->mmu, 1, 0);
340 if (!dev_priv->pf_pd)
343 ret = psb_do_init(dev);
347 /* Add stolen memory to SGX MMU */
349 ret = psb_mmu_insert_pfn_sequence(psb_mmu_get_default_pd(dev_priv->mmu),
350 dev_priv->stolen_base >> PAGE_SHIFT,
352 pg->stolen_size >> PAGE_SHIFT, 0);
355 psb_mmu_set_pd_context(psb_mmu_get_default_pd(dev_priv->mmu), 0);
356 psb_mmu_set_pd_context(dev_priv->pf_pd, 1);
358 PSB_WSGX32(0x20000000, PSB_CR_PDS_EXEC_BASE);
359 PSB_WSGX32(0x30000000, PSB_CR_BIF_3D_REQ_BASE);
361 acpi_video_register();
363 /* Setup vertical blanking handling */
364 ret = drm_vblank_init(dev, dev_priv->num_pipe);
369 * Install interrupt handlers prior to powering off SGX or else we will
372 dev_priv->vdc_irq_mask = 0;
373 dev_priv->pipestat[0] = 0;
374 dev_priv->pipestat[1] = 0;
375 dev_priv->pipestat[2] = 0;
376 spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
377 PSB_WVDC32(0xFFFFFFFF, PSB_HWSTAM);
378 PSB_WVDC32(0x00000000, PSB_INT_ENABLE_R);
379 PSB_WVDC32(0xFFFFFFFF, PSB_INT_MASK_R);
380 spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
382 psb_irq_install(dev, pdev->irq);
384 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
386 psb_modeset_init(dev);
388 drm_kms_helper_poll_init(dev);
390 /* Only add backlight support if we have LVDS output */
391 list_for_each_entry(connector, &dev->mode_config.connector_list,
393 gma_encoder = gma_attached_encoder(connector);
395 switch (gma_encoder->type) {
396 case INTEL_OUTPUT_LVDS:
397 case INTEL_OUTPUT_MIPI:
398 ret = gma_backlight_init(dev);
405 psb_intel_opregion_enable_asle(dev);
407 /* Enable runtime pm at last */
408 pm_runtime_enable(dev->dev);
409 pm_runtime_set_active(dev->dev);
412 return devm_add_action_or_reset(dev->dev, psb_device_release, dev);
415 psb_driver_unload(dev);
419 static inline void get_brightness(struct backlight_device *bd)
421 #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
423 bd->props.brightness = bd->ops->get_brightness(bd);
424 backlight_update_status(bd);
429 static long psb_unlocked_ioctl(struct file *filp, unsigned int cmd,
432 struct drm_file *file_priv = filp->private_data;
433 struct drm_device *dev = file_priv->minor->dev;
434 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
435 static unsigned int runtime_allowed;
437 if (runtime_allowed == 1 && dev_priv->is_lvds_on) {
439 pm_runtime_allow(dev->dev);
440 dev_priv->rpm_enabled = 1;
442 return drm_ioctl(filp, cmd, arg);
443 /* FIXME: do we need to wrap the other side of this */
446 static int psb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
448 struct drm_psb_private *dev_priv;
449 struct drm_device *dev;
453 * We cannot yet easily find the framebuffer's location in memory. So
454 * remove all framebuffers here.
456 * TODO: Refactor psb_driver_load() to map vdc_reg earlier. Then we
457 * might be able to read the framebuffer range from the device.
459 ret = drm_aperture_remove_framebuffers(true, &driver);
463 ret = pcim_enable_device(pdev);
467 dev_priv = devm_drm_dev_alloc(&pdev->dev, &driver, struct drm_psb_private, dev);
468 if (IS_ERR(dev_priv))
469 return PTR_ERR(dev_priv);
470 dev = &dev_priv->dev;
472 pci_set_drvdata(pdev, dev);
474 ret = psb_driver_load(dev, ent->driver_data);
478 ret = drm_dev_register(dev, ent->driver_data);
485 static void psb_pci_remove(struct pci_dev *pdev)
487 struct drm_device *dev = pci_get_drvdata(pdev);
489 drm_dev_unregister(dev);
492 static const struct dev_pm_ops psb_pm_ops = {
493 .resume = gma_power_resume,
494 .suspend = gma_power_suspend,
495 .thaw = gma_power_thaw,
496 .freeze = gma_power_freeze,
497 .restore = gma_power_restore,
498 .runtime_suspend = psb_runtime_suspend,
499 .runtime_resume = psb_runtime_resume,
500 .runtime_idle = psb_runtime_idle,
503 static const struct file_operations psb_gem_fops = {
504 .owner = THIS_MODULE,
506 .release = drm_release,
507 .unlocked_ioctl = psb_unlocked_ioctl,
508 .compat_ioctl = drm_compat_ioctl,
509 .mmap = drm_gem_mmap,
514 static const struct drm_driver driver = {
515 .driver_features = DRIVER_MODESET | DRIVER_GEM,
516 .lastclose = drm_fb_helper_lastclose,
518 .num_ioctls = ARRAY_SIZE(psb_ioctls),
520 .dumb_create = psb_gem_dumb_create,
521 .ioctls = psb_ioctls,
522 .fops = &psb_gem_fops,
526 .major = DRIVER_MAJOR,
527 .minor = DRIVER_MINOR,
528 .patchlevel = DRIVER_PATCHLEVEL
531 static struct pci_driver psb_pci_driver = {
533 .id_table = pciidlist,
534 .probe = psb_pci_probe,
535 .remove = psb_pci_remove,
536 .driver.pm = &psb_pm_ops,
539 static int __init psb_init(void)
541 return pci_register_driver(&psb_pci_driver);
544 static void __exit psb_exit(void)
546 pci_unregister_driver(&psb_pci_driver);
549 late_initcall(psb_init);
550 module_exit(psb_exit);
552 MODULE_AUTHOR(DRIVER_AUTHOR);
553 MODULE_DESCRIPTION(DRIVER_DESC);
554 MODULE_LICENSE("GPL");