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>
16 #include <asm/set_memory.h>
18 #include <acpi/video.h>
21 #include <drm/drm_drv.h>
22 #include <drm/drm_fb_helper.h>
23 #include <drm/drm_file.h>
24 #include <drm/drm_ioctl.h>
25 #include <drm/drm_irq.h>
26 #include <drm/drm_pciids.h>
27 #include <drm/drm_vblank.h>
29 #include "framebuffer.h"
30 #include "intel_bios.h"
34 #include "psb_intel_reg.h"
37 static const struct drm_driver driver
;
38 static int psb_pci_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
);
41 * The table below contains a mapping of the PCI vendor ID and the PCI Device ID
42 * to the different groups of PowerVR 5-series chip designs
44 * 0x8086 = Intel Corporation
46 * PowerVR SGX535 - Poulsbo - Intel GMA 500, Intel Atom Z5xx
47 * PowerVR SGX535 - Moorestown - Intel GMA 600
48 * PowerVR SGX535 - Oaktrail - Intel GMA 600, Intel Atom Z6xx, E6xx
49 * PowerVR SGX540 - Medfield - Intel Atom Z2460
50 * PowerVR SGX544MP2 - Medfield -
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
[] = {
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 #if defined(CONFIG_DRM_GMA600)
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
},
69 #if defined(CONFIG_DRM_MEDFIELD)
70 { 0x8086, 0x0130, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (long) &mdfld_chip_ops
},
71 { 0x8086, 0x0131, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (long) &mdfld_chip_ops
},
72 { 0x8086, 0x0132, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (long) &mdfld_chip_ops
},
73 { 0x8086, 0x0133, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (long) &mdfld_chip_ops
},
74 { 0x8086, 0x0134, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (long) &mdfld_chip_ops
},
75 { 0x8086, 0x0135, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (long) &mdfld_chip_ops
},
76 { 0x8086, 0x0136, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (long) &mdfld_chip_ops
},
77 { 0x8086, 0x0137, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (long) &mdfld_chip_ops
},
79 #if defined(CONFIG_DRM_GMA3600)
80 { 0x8086, 0x0be0, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (long) &cdv_chip_ops
},
81 { 0x8086, 0x0be1, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (long) &cdv_chip_ops
},
82 { 0x8086, 0x0be2, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (long) &cdv_chip_ops
},
83 { 0x8086, 0x0be3, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (long) &cdv_chip_ops
},
84 { 0x8086, 0x0be4, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (long) &cdv_chip_ops
},
85 { 0x8086, 0x0be5, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (long) &cdv_chip_ops
},
86 { 0x8086, 0x0be6, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (long) &cdv_chip_ops
},
87 { 0x8086, 0x0be7, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (long) &cdv_chip_ops
},
88 { 0x8086, 0x0be8, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (long) &cdv_chip_ops
},
89 { 0x8086, 0x0be9, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (long) &cdv_chip_ops
},
90 { 0x8086, 0x0bea, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (long) &cdv_chip_ops
},
91 { 0x8086, 0x0beb, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (long) &cdv_chip_ops
},
92 { 0x8086, 0x0bec, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (long) &cdv_chip_ops
},
93 { 0x8086, 0x0bed, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (long) &cdv_chip_ops
},
94 { 0x8086, 0x0bee, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (long) &cdv_chip_ops
},
95 { 0x8086, 0x0bef, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (long) &cdv_chip_ops
},
99 MODULE_DEVICE_TABLE(pci
, pciidlist
);
104 static const struct drm_ioctl_desc psb_ioctls
[] = {
107 static int psb_do_init(struct drm_device
*dev
)
109 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
110 struct psb_gtt
*pg
= &dev_priv
->gtt
;
114 if (pg
->mmu_gatt_start
& 0x0FFFFFFF) {
115 dev_err(dev
->dev
, "Gatt must be 256M aligned. This is a bug.\n");
119 stolen_gtt
= (pg
->stolen_size
>> PAGE_SHIFT
) * 4;
120 stolen_gtt
= (stolen_gtt
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
121 stolen_gtt
= (stolen_gtt
< pg
->gtt_pages
) ? stolen_gtt
: pg
->gtt_pages
;
123 dev_priv
->gatt_free_offset
= pg
->mmu_gatt_start
+
124 (stolen_gtt
<< PAGE_SHIFT
) * 1024;
126 spin_lock_init(&dev_priv
->irqmask_lock
);
128 PSB_WSGX32(0x00000000, PSB_CR_BIF_BANK0
);
129 PSB_WSGX32(0x00000000, PSB_CR_BIF_BANK1
);
130 PSB_RSGX32(PSB_CR_BIF_BANK1
);
132 /* Do not bypass any MMU access, let them pagefault instead */
133 PSB_WSGX32((PSB_RSGX32(PSB_CR_BIF_CTRL
) & ~_PSB_MMU_ER_MASK
),
135 PSB_RSGX32(PSB_CR_BIF_CTRL
);
140 PSB_WSGX32(pg
->gatt_start
, PSB_CR_BIF_TWOD_REQ_BASE
);
141 PSB_RSGX32(PSB_CR_BIF_TWOD_REQ_BASE
); /* Post */
146 static void psb_driver_unload(struct drm_device
*dev
)
148 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
150 /* TODO: Kill vblank etc here */
153 if (dev_priv
->backlight_device
)
154 gma_backlight_exit(dev
);
155 psb_modeset_cleanup(dev
);
157 if (dev_priv
->ops
->chip_teardown
)
158 dev_priv
->ops
->chip_teardown(dev
);
160 psb_intel_opregion_fini(dev
);
162 if (dev_priv
->pf_pd
) {
163 psb_mmu_free_pagedir(dev_priv
->pf_pd
);
164 dev_priv
->pf_pd
= NULL
;
167 struct psb_gtt
*pg
= &dev_priv
->gtt
;
170 psb_mmu_remove_pfn_sequence(
171 psb_mmu_get_default_pd
174 dev_priv
->vram_stolen_size
>> PAGE_SHIFT
);
176 psb_mmu_driver_takedown(dev_priv
->mmu
);
177 dev_priv
->mmu
= NULL
;
179 psb_gtt_takedown(dev
);
180 if (dev_priv
->scratch_page
) {
181 set_pages_wb(dev_priv
->scratch_page
, 1);
182 __free_page(dev_priv
->scratch_page
);
183 dev_priv
->scratch_page
= NULL
;
185 if (dev_priv
->vdc_reg
) {
186 iounmap(dev_priv
->vdc_reg
);
187 dev_priv
->vdc_reg
= NULL
;
189 if (dev_priv
->sgx_reg
) {
190 iounmap(dev_priv
->sgx_reg
);
191 dev_priv
->sgx_reg
= NULL
;
193 if (dev_priv
->aux_reg
) {
194 iounmap(dev_priv
->aux_reg
);
195 dev_priv
->aux_reg
= NULL
;
197 pci_dev_put(dev_priv
->aux_pdev
);
198 pci_dev_put(dev_priv
->lpc_pdev
);
200 /* Destroy VBT data */
201 psb_intel_destroy_bios(dev
);
204 dev
->dev_private
= NULL
;
206 gma_power_uninit(dev
);
209 static int psb_driver_load(struct drm_device
*dev
, unsigned long flags
)
211 struct drm_psb_private
*dev_priv
;
212 unsigned long resource_start
, resource_len
;
213 unsigned long irqflags
;
215 struct drm_connector
*connector
;
216 struct gma_encoder
*gma_encoder
;
219 /* allocating and initializing driver private data */
220 dev_priv
= kzalloc(sizeof(*dev_priv
), GFP_KERNEL
);
221 if (dev_priv
== NULL
)
224 dev_priv
->ops
= (struct psb_ops
*)flags
;
226 dev
->dev_private
= (void *) dev_priv
;
230 pci_set_master(dev
->pdev
);
232 dev_priv
->num_pipe
= dev_priv
->ops
->pipes
;
234 resource_start
= pci_resource_start(dev
->pdev
, PSB_MMIO_RESOURCE
);
237 ioremap(resource_start
+ PSB_VDC_OFFSET
, PSB_VDC_SIZE
);
238 if (!dev_priv
->vdc_reg
)
241 dev_priv
->sgx_reg
= ioremap(resource_start
+ dev_priv
->ops
->sgx_offset
,
243 if (!dev_priv
->sgx_reg
)
247 int domain
= pci_domain_nr(dev
->pdev
->bus
);
250 pci_get_domain_bus_and_slot(domain
, 0,
253 if (dev_priv
->aux_pdev
) {
254 resource_start
= pci_resource_start(dev_priv
->aux_pdev
,
256 resource_len
= pci_resource_len(dev_priv
->aux_pdev
,
258 dev_priv
->aux_reg
= ioremap(resource_start
,
260 if (!dev_priv
->aux_reg
)
263 DRM_DEBUG_KMS("Found aux vdc");
265 /* Couldn't find the aux vdc so map to primary vdc */
266 dev_priv
->aux_reg
= dev_priv
->vdc_reg
;
267 DRM_DEBUG_KMS("Couldn't find aux pci device");
269 dev_priv
->gmbus_reg
= dev_priv
->aux_reg
;
272 pci_get_domain_bus_and_slot(domain
, 0,
274 if (dev_priv
->lpc_pdev
) {
275 pci_read_config_word(dev_priv
->lpc_pdev
, PSB_LPC_GBA
,
276 &dev_priv
->lpc_gpio_base
);
277 pci_write_config_dword(dev_priv
->lpc_pdev
, PSB_LPC_GBA
,
278 (u32
)dev_priv
->lpc_gpio_base
| (1L<<31));
279 pci_read_config_word(dev_priv
->lpc_pdev
, PSB_LPC_GBA
,
280 &dev_priv
->lpc_gpio_base
);
281 dev_priv
->lpc_gpio_base
&= 0xffc0;
282 if (dev_priv
->lpc_gpio_base
)
283 DRM_DEBUG_KMS("Found LPC GPIO at 0x%04x\n",
284 dev_priv
->lpc_gpio_base
);
286 pci_dev_put(dev_priv
->lpc_pdev
);
287 dev_priv
->lpc_pdev
= NULL
;
291 dev_priv
->gmbus_reg
= dev_priv
->vdc_reg
;
294 psb_intel_opregion_setup(dev
);
296 ret
= dev_priv
->ops
->chip_setup(dev
);
300 /* Init OSPM support */
305 dev_priv
->scratch_page
= alloc_page(GFP_DMA32
| __GFP_ZERO
);
306 if (!dev_priv
->scratch_page
)
309 set_pages_uc(dev_priv
->scratch_page
, 1);
311 ret
= psb_gtt_init(dev
, 0);
315 dev_priv
->mmu
= psb_mmu_driver_init(dev
, 1, 0, 0);
319 dev_priv
->pf_pd
= psb_mmu_alloc_pd(dev_priv
->mmu
, 1, 0);
320 if (!dev_priv
->pf_pd
)
323 ret
= psb_do_init(dev
);
327 /* Add stolen memory to SGX MMU */
329 ret
= psb_mmu_insert_pfn_sequence(psb_mmu_get_default_pd(dev_priv
->mmu
),
330 dev_priv
->stolen_base
>> PAGE_SHIFT
,
332 pg
->stolen_size
>> PAGE_SHIFT
, 0);
335 psb_mmu_set_pd_context(psb_mmu_get_default_pd(dev_priv
->mmu
), 0);
336 psb_mmu_set_pd_context(dev_priv
->pf_pd
, 1);
338 PSB_WSGX32(0x20000000, PSB_CR_PDS_EXEC_BASE
);
339 PSB_WSGX32(0x30000000, PSB_CR_BIF_3D_REQ_BASE
);
341 acpi_video_register();
343 /* Setup vertical blanking handling */
344 ret
= drm_vblank_init(dev
, dev_priv
->num_pipe
);
349 * Install interrupt handlers prior to powering off SGX or else we will
352 dev_priv
->vdc_irq_mask
= 0;
353 dev_priv
->pipestat
[0] = 0;
354 dev_priv
->pipestat
[1] = 0;
355 dev_priv
->pipestat
[2] = 0;
356 spin_lock_irqsave(&dev_priv
->irqmask_lock
, irqflags
);
357 PSB_WVDC32(0xFFFFFFFF, PSB_HWSTAM
);
358 PSB_WVDC32(0x00000000, PSB_INT_ENABLE_R
);
359 PSB_WVDC32(0xFFFFFFFF, PSB_INT_MASK_R
);
360 spin_unlock_irqrestore(&dev_priv
->irqmask_lock
, irqflags
);
362 drm_irq_install(dev
, dev
->pdev
->irq
);
364 dev
->max_vblank_count
= 0xffffff; /* only 24 bits of frame count */
366 psb_modeset_init(dev
);
368 drm_kms_helper_poll_init(dev
);
370 /* Only add backlight support if we have LVDS output */
371 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
,
373 gma_encoder
= gma_attached_encoder(connector
);
375 switch (gma_encoder
->type
) {
376 case INTEL_OUTPUT_LVDS
:
377 case INTEL_OUTPUT_MIPI
:
378 ret
= gma_backlight_init(dev
);
385 psb_intel_opregion_enable_asle(dev
);
387 /* Enable runtime pm at last */
388 pm_runtime_enable(&dev
->pdev
->dev
);
389 pm_runtime_set_active(&dev
->pdev
->dev
);
391 /* Intel drm driver load is done, continue doing pvr load */
394 psb_driver_unload(dev
);
398 static inline void get_brightness(struct backlight_device
*bd
)
400 #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
402 bd
->props
.brightness
= bd
->ops
->get_brightness(bd
);
403 backlight_update_status(bd
);
408 static long psb_unlocked_ioctl(struct file
*filp
, unsigned int cmd
,
411 struct drm_file
*file_priv
= filp
->private_data
;
412 struct drm_device
*dev
= file_priv
->minor
->dev
;
413 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
414 static unsigned int runtime_allowed
;
416 if (runtime_allowed
== 1 && dev_priv
->is_lvds_on
) {
418 pm_runtime_allow(&dev
->pdev
->dev
);
419 dev_priv
->rpm_enabled
= 1;
421 return drm_ioctl(filp
, cmd
, arg
);
422 /* FIXME: do we need to wrap the other side of this */
425 static int psb_pci_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
427 struct drm_device
*dev
;
430 ret
= pci_enable_device(pdev
);
434 dev
= drm_dev_alloc(&driver
, &pdev
->dev
);
437 goto err_pci_disable_device
;
441 pci_set_drvdata(pdev
, dev
);
443 ret
= psb_driver_load(dev
, ent
->driver_data
);
445 goto err_drm_dev_put
;
447 ret
= drm_dev_register(dev
, ent
->driver_data
);
449 goto err_psb_driver_unload
;
453 err_psb_driver_unload
:
454 psb_driver_unload(dev
);
457 err_pci_disable_device
:
458 pci_disable_device(pdev
);
462 static void psb_pci_remove(struct pci_dev
*pdev
)
464 struct drm_device
*dev
= pci_get_drvdata(pdev
);
466 drm_dev_unregister(dev
);
467 psb_driver_unload(dev
);
471 static const struct dev_pm_ops psb_pm_ops
= {
472 .resume
= gma_power_resume
,
473 .suspend
= gma_power_suspend
,
474 .thaw
= gma_power_thaw
,
475 .freeze
= gma_power_freeze
,
476 .restore
= gma_power_restore
,
477 .runtime_suspend
= psb_runtime_suspend
,
478 .runtime_resume
= psb_runtime_resume
,
479 .runtime_idle
= psb_runtime_idle
,
482 static const struct file_operations psb_gem_fops
= {
483 .owner
= THIS_MODULE
,
485 .release
= drm_release
,
486 .unlocked_ioctl
= psb_unlocked_ioctl
,
487 .compat_ioctl
= drm_compat_ioctl
,
488 .mmap
= drm_gem_mmap
,
493 static const struct drm_driver driver
= {
494 .driver_features
= DRIVER_MODESET
| DRIVER_GEM
,
495 .lastclose
= drm_fb_helper_lastclose
,
497 .num_ioctls
= ARRAY_SIZE(psb_ioctls
),
498 .irq_preinstall
= psb_irq_preinstall
,
499 .irq_postinstall
= psb_irq_postinstall
,
500 .irq_uninstall
= psb_irq_uninstall
,
501 .irq_handler
= psb_irq_handler
,
503 .dumb_create
= psb_gem_dumb_create
,
504 .ioctls
= psb_ioctls
,
505 .fops
= &psb_gem_fops
,
509 .major
= DRIVER_MAJOR
,
510 .minor
= DRIVER_MINOR
,
511 .patchlevel
= DRIVER_PATCHLEVEL
514 static struct pci_driver psb_pci_driver
= {
516 .id_table
= pciidlist
,
517 .probe
= psb_pci_probe
,
518 .remove
= psb_pci_remove
,
519 .driver
.pm
= &psb_pm_ops
,
522 static int __init
psb_init(void)
524 return pci_register_driver(&psb_pci_driver
);
527 static void __exit
psb_exit(void)
529 pci_unregister_driver(&psb_pci_driver
);
532 late_initcall(psb_init
);
533 module_exit(psb_exit
);
535 MODULE_AUTHOR(DRIVER_AUTHOR
);
536 MODULE_DESCRIPTION(DRIVER_DESC
);
537 MODULE_LICENSE("GPL");