1 // SPDX-License-Identifier: GPL-2.0-only 2 /************************************************************************** 3 * Copyright (c) 2007-2011, Intel Corporation. 4 * All Rights Reserved. 5 * Copyright (c) 2008, Tungsten Graphics, Inc. Cedar Park, TX., USA. 6 * All Rights Reserved. 7 * 8 **************************************************************************/ 9 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> 16 17 #include <asm/set_memory.h> 18 19 #include <acpi/video.h> 20 21 #include <drm/drm.h> 22 #include <drm/drm_drv.h> 23 #include <drm/drm_fb_helper.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> 28 29 #include "framebuffer.h" 30 #include "intel_bios.h" 31 #include "mid_bios.h" 32 #include "power.h" 33 #include "psb_drv.h" 34 #include "psb_intel_reg.h" 35 #include "psb_irq.h" 36 #include "psb_reg.h" 37 38 static const struct drm_driver driver; 39 static int psb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent); 40 41 /* 42 * The table below contains a mapping of the PCI vendor ID and the PCI Device ID 43 * to the different groups of PowerVR 5-series chip designs 44 * 45 * 0x8086 = Intel Corporation 46 * 47 * PowerVR SGX535 - Poulsbo - Intel GMA 500, Intel Atom Z5xx 48 * PowerVR SGX535 - Moorestown - Intel GMA 600 49 * PowerVR SGX535 - Oaktrail - Intel GMA 600, Intel Atom Z6xx, E6xx 50 * PowerVR SGX545 - Cedartrail - Intel GMA 3600, Intel Atom D2500, N2600 51 * PowerVR SGX545 - Cedartrail - Intel GMA 3650, Intel Atom D2550, D2700, 52 * N2800 53 */ 54 static const struct pci_device_id pciidlist[] = { 55 /* Poulsbo */ 56 { 0x8086, 0x8108, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &psb_chip_ops }, 57 { 0x8086, 0x8109, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &psb_chip_ops }, 58 /* Oak Trail */ 59 { 0x8086, 0x4100, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops }, 60 { 0x8086, 0x4101, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops }, 61 { 0x8086, 0x4102, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops }, 62 { 0x8086, 0x4103, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops }, 63 { 0x8086, 0x4104, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops }, 64 { 0x8086, 0x4105, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops }, 65 { 0x8086, 0x4106, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops }, 66 { 0x8086, 0x4107, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops }, 67 { 0x8086, 0x4108, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops }, 68 /* Cedar Trail */ 69 { 0x8086, 0x0be0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops }, 70 { 0x8086, 0x0be1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops }, 71 { 0x8086, 0x0be2, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops }, 72 { 0x8086, 0x0be3, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops }, 73 { 0x8086, 0x0be4, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops }, 74 { 0x8086, 0x0be5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops }, 75 { 0x8086, 0x0be6, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops }, 76 { 0x8086, 0x0be7, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops }, 77 { 0x8086, 0x0be8, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops }, 78 { 0x8086, 0x0be9, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops }, 79 { 0x8086, 0x0bea, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops }, 80 { 0x8086, 0x0beb, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops }, 81 { 0x8086, 0x0bec, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops }, 82 { 0x8086, 0x0bed, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops }, 83 { 0x8086, 0x0bee, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops }, 84 { 0x8086, 0x0bef, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops }, 85 { 0, } 86 }; 87 MODULE_DEVICE_TABLE(pci, pciidlist); 88 89 /* 90 * Standard IOCTLs. 91 */ 92 static const struct drm_ioctl_desc psb_ioctls[] = { 93 }; 94 95 /** 96 * psb_spank - reset the 2D engine 97 * @dev_priv: our PSB DRM device 98 * 99 * Soft reset the graphics engine and then reload the necessary registers. 100 */ 101 void psb_spank(struct drm_psb_private *dev_priv) 102 { 103 PSB_WSGX32(_PSB_CS_RESET_BIF_RESET | _PSB_CS_RESET_DPM_RESET | 104 _PSB_CS_RESET_TA_RESET | _PSB_CS_RESET_USE_RESET | 105 _PSB_CS_RESET_ISP_RESET | _PSB_CS_RESET_TSP_RESET | 106 _PSB_CS_RESET_TWOD_RESET, PSB_CR_SOFT_RESET); 107 PSB_RSGX32(PSB_CR_SOFT_RESET); 108 109 msleep(1); 110 111 PSB_WSGX32(0, PSB_CR_SOFT_RESET); 112 wmb(); 113 PSB_WSGX32(PSB_RSGX32(PSB_CR_BIF_CTRL) | _PSB_CB_CTRL_CLEAR_FAULT, 114 PSB_CR_BIF_CTRL); 115 wmb(); 116 (void) PSB_RSGX32(PSB_CR_BIF_CTRL); 117 118 msleep(1); 119 PSB_WSGX32(PSB_RSGX32(PSB_CR_BIF_CTRL) & ~_PSB_CB_CTRL_CLEAR_FAULT, 120 PSB_CR_BIF_CTRL); 121 (void) PSB_RSGX32(PSB_CR_BIF_CTRL); 122 PSB_WSGX32(dev_priv->gtt.gatt_start, PSB_CR_BIF_TWOD_REQ_BASE); 123 } 124 125 static int psb_do_init(struct drm_device *dev) 126 { 127 struct drm_psb_private *dev_priv = to_drm_psb_private(dev); 128 struct psb_gtt *pg = &dev_priv->gtt; 129 130 uint32_t stolen_gtt; 131 132 if (pg->mmu_gatt_start & 0x0FFFFFFF) { 133 dev_err(dev->dev, "Gatt must be 256M aligned. This is a bug.\n"); 134 return -EINVAL; 135 } 136 137 stolen_gtt = (pg->stolen_size >> PAGE_SHIFT) * 4; 138 stolen_gtt = (stolen_gtt + PAGE_SIZE - 1) >> PAGE_SHIFT; 139 stolen_gtt = (stolen_gtt < pg->gtt_pages) ? stolen_gtt : pg->gtt_pages; 140 141 dev_priv->gatt_free_offset = pg->mmu_gatt_start + 142 (stolen_gtt << PAGE_SHIFT) * 1024; 143 144 spin_lock_init(&dev_priv->irqmask_lock); 145 146 PSB_WSGX32(0x00000000, PSB_CR_BIF_BANK0); 147 PSB_WSGX32(0x00000000, PSB_CR_BIF_BANK1); 148 PSB_RSGX32(PSB_CR_BIF_BANK1); 149 150 /* Do not bypass any MMU access, let them pagefault instead */ 151 PSB_WSGX32((PSB_RSGX32(PSB_CR_BIF_CTRL) & ~_PSB_MMU_ER_MASK), 152 PSB_CR_BIF_CTRL); 153 PSB_RSGX32(PSB_CR_BIF_CTRL); 154 155 psb_spank(dev_priv); 156 157 /* mmu_gatt ?? */ 158 PSB_WSGX32(pg->gatt_start, PSB_CR_BIF_TWOD_REQ_BASE); 159 PSB_RSGX32(PSB_CR_BIF_TWOD_REQ_BASE); /* Post */ 160 161 return 0; 162 } 163 164 static void psb_driver_unload(struct drm_device *dev) 165 { 166 struct drm_psb_private *dev_priv = to_drm_psb_private(dev); 167 168 /* TODO: Kill vblank etc here */ 169 170 if (dev_priv->backlight_device) 171 gma_backlight_exit(dev); 172 psb_modeset_cleanup(dev); 173 174 if (dev_priv->ops->chip_teardown) 175 dev_priv->ops->chip_teardown(dev); 176 177 psb_intel_opregion_fini(dev); 178 179 if (dev_priv->pf_pd) { 180 psb_mmu_free_pagedir(dev_priv->pf_pd); 181 dev_priv->pf_pd = NULL; 182 } 183 if (dev_priv->mmu) { 184 struct psb_gtt *pg = &dev_priv->gtt; 185 186 down_read(&pg->sem); 187 psb_mmu_remove_pfn_sequence( 188 psb_mmu_get_default_pd 189 (dev_priv->mmu), 190 pg->mmu_gatt_start, 191 dev_priv->vram_stolen_size >> PAGE_SHIFT); 192 up_read(&pg->sem); 193 psb_mmu_driver_takedown(dev_priv->mmu); 194 dev_priv->mmu = NULL; 195 } 196 psb_gtt_takedown(dev); 197 if (dev_priv->scratch_page) { 198 set_pages_wb(dev_priv->scratch_page, 1); 199 __free_page(dev_priv->scratch_page); 200 dev_priv->scratch_page = NULL; 201 } 202 if (dev_priv->vdc_reg) { 203 iounmap(dev_priv->vdc_reg); 204 dev_priv->vdc_reg = NULL; 205 } 206 if (dev_priv->sgx_reg) { 207 iounmap(dev_priv->sgx_reg); 208 dev_priv->sgx_reg = NULL; 209 } 210 if (dev_priv->aux_reg) { 211 iounmap(dev_priv->aux_reg); 212 dev_priv->aux_reg = NULL; 213 } 214 pci_dev_put(dev_priv->aux_pdev); 215 pci_dev_put(dev_priv->lpc_pdev); 216 217 /* Destroy VBT data */ 218 psb_intel_destroy_bios(dev); 219 220 gma_power_uninit(dev); 221 } 222 223 static void psb_device_release(void *data) 224 { 225 struct drm_device *dev = data; 226 227 psb_driver_unload(dev); 228 } 229 230 static int psb_driver_load(struct drm_device *dev, unsigned long flags) 231 { 232 struct pci_dev *pdev = to_pci_dev(dev->dev); 233 struct drm_psb_private *dev_priv = to_drm_psb_private(dev); 234 unsigned long resource_start, resource_len; 235 unsigned long irqflags; 236 int ret = -ENOMEM; 237 struct drm_connector *connector; 238 struct gma_encoder *gma_encoder; 239 struct psb_gtt *pg; 240 241 /* initializing driver private data */ 242 243 dev_priv->ops = (struct psb_ops *)flags; 244 245 pg = &dev_priv->gtt; 246 247 pci_set_master(pdev); 248 249 dev_priv->num_pipe = dev_priv->ops->pipes; 250 251 resource_start = pci_resource_start(pdev, PSB_MMIO_RESOURCE); 252 253 dev_priv->vdc_reg = 254 ioremap(resource_start + PSB_VDC_OFFSET, PSB_VDC_SIZE); 255 if (!dev_priv->vdc_reg) 256 goto out_err; 257 258 dev_priv->sgx_reg = ioremap(resource_start + dev_priv->ops->sgx_offset, 259 PSB_SGX_SIZE); 260 if (!dev_priv->sgx_reg) 261 goto out_err; 262 263 if (IS_MRST(dev)) { 264 int domain = pci_domain_nr(pdev->bus); 265 266 dev_priv->aux_pdev = 267 pci_get_domain_bus_and_slot(domain, 0, 268 PCI_DEVFN(3, 0)); 269 270 if (dev_priv->aux_pdev) { 271 resource_start = pci_resource_start(dev_priv->aux_pdev, 272 PSB_AUX_RESOURCE); 273 resource_len = pci_resource_len(dev_priv->aux_pdev, 274 PSB_AUX_RESOURCE); 275 dev_priv->aux_reg = ioremap(resource_start, 276 resource_len); 277 if (!dev_priv->aux_reg) 278 goto out_err; 279 280 DRM_DEBUG_KMS("Found aux vdc"); 281 } else { 282 /* Couldn't find the aux vdc so map to primary vdc */ 283 dev_priv->aux_reg = dev_priv->vdc_reg; 284 DRM_DEBUG_KMS("Couldn't find aux pci device"); 285 } 286 dev_priv->gmbus_reg = dev_priv->aux_reg; 287 288 dev_priv->lpc_pdev = 289 pci_get_domain_bus_and_slot(domain, 0, 290 PCI_DEVFN(31, 0)); 291 if (dev_priv->lpc_pdev) { 292 pci_read_config_word(dev_priv->lpc_pdev, PSB_LPC_GBA, 293 &dev_priv->lpc_gpio_base); 294 pci_write_config_dword(dev_priv->lpc_pdev, PSB_LPC_GBA, 295 (u32)dev_priv->lpc_gpio_base | (1L<<31)); 296 pci_read_config_word(dev_priv->lpc_pdev, PSB_LPC_GBA, 297 &dev_priv->lpc_gpio_base); 298 dev_priv->lpc_gpio_base &= 0xffc0; 299 if (dev_priv->lpc_gpio_base) 300 DRM_DEBUG_KMS("Found LPC GPIO at 0x%04x\n", 301 dev_priv->lpc_gpio_base); 302 else { 303 pci_dev_put(dev_priv->lpc_pdev); 304 dev_priv->lpc_pdev = NULL; 305 } 306 } 307 } else { 308 dev_priv->gmbus_reg = dev_priv->vdc_reg; 309 } 310 311 psb_intel_opregion_setup(dev); 312 313 ret = dev_priv->ops->chip_setup(dev); 314 if (ret) 315 goto out_err; 316 317 /* Init OSPM support */ 318 gma_power_init(dev); 319 320 ret = -ENOMEM; 321 322 dev_priv->scratch_page = alloc_page(GFP_DMA32 | __GFP_ZERO); 323 if (!dev_priv->scratch_page) 324 goto out_err; 325 326 set_pages_uc(dev_priv->scratch_page, 1); 327 328 ret = psb_gtt_init(dev, 0); 329 if (ret) 330 goto out_err; 331 332 ret = -ENOMEM; 333 334 dev_priv->mmu = psb_mmu_driver_init(dev, 1, 0, NULL); 335 if (!dev_priv->mmu) 336 goto out_err; 337 338 dev_priv->pf_pd = psb_mmu_alloc_pd(dev_priv->mmu, 1, 0); 339 if (!dev_priv->pf_pd) 340 goto out_err; 341 342 ret = psb_do_init(dev); 343 if (ret) 344 return ret; 345 346 /* Add stolen memory to SGX MMU */ 347 down_read(&pg->sem); 348 ret = psb_mmu_insert_pfn_sequence(psb_mmu_get_default_pd(dev_priv->mmu), 349 dev_priv->stolen_base >> PAGE_SHIFT, 350 pg->gatt_start, 351 pg->stolen_size >> PAGE_SHIFT, 0); 352 up_read(&pg->sem); 353 354 psb_mmu_set_pd_context(psb_mmu_get_default_pd(dev_priv->mmu), 0); 355 psb_mmu_set_pd_context(dev_priv->pf_pd, 1); 356 357 PSB_WSGX32(0x20000000, PSB_CR_PDS_EXEC_BASE); 358 PSB_WSGX32(0x30000000, PSB_CR_BIF_3D_REQ_BASE); 359 360 acpi_video_register(); 361 362 /* Setup vertical blanking handling */ 363 ret = drm_vblank_init(dev, dev_priv->num_pipe); 364 if (ret) 365 goto out_err; 366 367 /* 368 * Install interrupt handlers prior to powering off SGX or else we will 369 * crash. 370 */ 371 dev_priv->vdc_irq_mask = 0; 372 dev_priv->pipestat[0] = 0; 373 dev_priv->pipestat[1] = 0; 374 dev_priv->pipestat[2] = 0; 375 spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags); 376 PSB_WVDC32(0xFFFFFFFF, PSB_HWSTAM); 377 PSB_WVDC32(0x00000000, PSB_INT_ENABLE_R); 378 PSB_WVDC32(0xFFFFFFFF, PSB_INT_MASK_R); 379 spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags); 380 381 psb_irq_install(dev, pdev->irq); 382 383 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */ 384 385 psb_modeset_init(dev); 386 psb_fbdev_init(dev); 387 drm_kms_helper_poll_init(dev); 388 389 /* Only add backlight support if we have LVDS output */ 390 list_for_each_entry(connector, &dev->mode_config.connector_list, 391 head) { 392 gma_encoder = gma_attached_encoder(connector); 393 394 switch (gma_encoder->type) { 395 case INTEL_OUTPUT_LVDS: 396 case INTEL_OUTPUT_MIPI: 397 ret = gma_backlight_init(dev); 398 break; 399 } 400 } 401 402 if (ret) 403 return ret; 404 psb_intel_opregion_enable_asle(dev); 405 #if 0 406 /* Enable runtime pm at last */ 407 pm_runtime_enable(dev->dev); 408 pm_runtime_set_active(dev->dev); 409 #endif 410 411 return devm_add_action_or_reset(dev->dev, psb_device_release, dev); 412 413 out_err: 414 psb_driver_unload(dev); 415 return ret; 416 } 417 418 static inline void get_brightness(struct backlight_device *bd) 419 { 420 #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE 421 if (bd) { 422 bd->props.brightness = bd->ops->get_brightness(bd); 423 backlight_update_status(bd); 424 } 425 #endif 426 } 427 428 static long psb_unlocked_ioctl(struct file *filp, unsigned int cmd, 429 unsigned long arg) 430 { 431 struct drm_file *file_priv = filp->private_data; 432 struct drm_device *dev = file_priv->minor->dev; 433 struct drm_psb_private *dev_priv = to_drm_psb_private(dev); 434 static unsigned int runtime_allowed; 435 436 if (runtime_allowed == 1 && dev_priv->is_lvds_on) { 437 runtime_allowed++; 438 pm_runtime_allow(dev->dev); 439 dev_priv->rpm_enabled = 1; 440 } 441 return drm_ioctl(filp, cmd, arg); 442 /* FIXME: do we need to wrap the other side of this */ 443 } 444 445 static int psb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 446 { 447 struct drm_psb_private *dev_priv; 448 struct drm_device *dev; 449 int ret; 450 451 ret = pcim_enable_device(pdev); 452 if (ret) 453 return ret; 454 455 dev_priv = devm_drm_dev_alloc(&pdev->dev, &driver, struct drm_psb_private, dev); 456 if (IS_ERR(dev_priv)) 457 return PTR_ERR(dev_priv); 458 dev = &dev_priv->dev; 459 460 pci_set_drvdata(pdev, dev); 461 462 ret = psb_driver_load(dev, ent->driver_data); 463 if (ret) 464 return ret; 465 466 ret = drm_dev_register(dev, ent->driver_data); 467 if (ret) 468 return ret; 469 470 return 0; 471 } 472 473 static void psb_pci_remove(struct pci_dev *pdev) 474 { 475 struct drm_device *dev = pci_get_drvdata(pdev); 476 477 drm_dev_unregister(dev); 478 } 479 480 static const struct dev_pm_ops psb_pm_ops = { 481 .resume = gma_power_resume, 482 .suspend = gma_power_suspend, 483 .thaw = gma_power_thaw, 484 .freeze = gma_power_freeze, 485 .restore = gma_power_restore, 486 .runtime_suspend = psb_runtime_suspend, 487 .runtime_resume = psb_runtime_resume, 488 .runtime_idle = psb_runtime_idle, 489 }; 490 491 static const struct file_operations psb_gem_fops = { 492 .owner = THIS_MODULE, 493 .open = drm_open, 494 .release = drm_release, 495 .unlocked_ioctl = psb_unlocked_ioctl, 496 .compat_ioctl = drm_compat_ioctl, 497 .mmap = drm_gem_mmap, 498 .poll = drm_poll, 499 .read = drm_read, 500 }; 501 502 static const struct drm_driver driver = { 503 .driver_features = DRIVER_MODESET | DRIVER_GEM, 504 .lastclose = drm_fb_helper_lastclose, 505 506 .num_ioctls = ARRAY_SIZE(psb_ioctls), 507 508 .dumb_create = psb_gem_dumb_create, 509 .ioctls = psb_ioctls, 510 .fops = &psb_gem_fops, 511 .name = DRIVER_NAME, 512 .desc = DRIVER_DESC, 513 .date = DRIVER_DATE, 514 .major = DRIVER_MAJOR, 515 .minor = DRIVER_MINOR, 516 .patchlevel = DRIVER_PATCHLEVEL 517 }; 518 519 static struct pci_driver psb_pci_driver = { 520 .name = DRIVER_NAME, 521 .id_table = pciidlist, 522 .probe = psb_pci_probe, 523 .remove = psb_pci_remove, 524 .driver.pm = &psb_pm_ops, 525 }; 526 527 static int __init psb_init(void) 528 { 529 return pci_register_driver(&psb_pci_driver); 530 } 531 532 static void __exit psb_exit(void) 533 { 534 pci_unregister_driver(&psb_pci_driver); 535 } 536 537 late_initcall(psb_init); 538 module_exit(psb_exit); 539 540 MODULE_AUTHOR(DRIVER_AUTHOR); 541 MODULE_DESCRIPTION(DRIVER_DESC); 542 MODULE_LICENSE("GPL"); 543