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