blob: 99e8f78b8b17e60afdbde811398024a58426814e [file] [log] [blame]
Alan Cox5c49fd32011-11-03 18:22:04 +00001/**************************************************************************
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 <drm/drmP.h>
23#include <drm/drm.h>
David Howells760285e2012-10-02 18:01:07 +010024#include <drm/gma_drm.h>
Alan Cox5c49fd32011-11-03 18:22:04 +000025#include "psb_drv.h"
26#include "framebuffer.h"
27#include "psb_reg.h"
28#include "psb_intel_reg.h"
29#include "intel_bios.h"
30#include "mid_bios.h"
31#include <drm/drm_pciids.h>
32#include "power.h"
33#include <linux/cpu.h>
34#include <linux/notifier.h>
35#include <linux/spinlock.h>
36#include <linux/pm_runtime.h>
37#include <acpi/video.h>
Dave Airlieaf3a2cf2011-11-16 12:13:30 +000038#include <linux/module.h>
Alan Cox5c49fd32011-11-03 18:22:04 +000039
Alan Cox5c49fd32011-11-03 18:22:04 +000040static int psb_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
41
Alan Cox5c49fd32011-11-03 18:22:04 +000042static DEFINE_PCI_DEVICE_TABLE(pciidlist) = {
43 { 0x8086, 0x8108, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &psb_chip_ops },
44 { 0x8086, 0x8109, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &psb_chip_ops },
Alan Cox0cecdd82011-12-19 21:06:35 +000045#if defined(CONFIG_DRM_GMA600)
Alan Cox5c49fd32011-11-03 18:22:04 +000046 { 0x8086, 0x4100, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops},
47 { 0x8086, 0x4101, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops},
48 { 0x8086, 0x4102, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops},
49 { 0x8086, 0x4103, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops},
50 { 0x8086, 0x4104, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops},
51 { 0x8086, 0x4105, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops},
52 { 0x8086, 0x4106, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops},
53 { 0x8086, 0x4107, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops},
Alan Cox56125db2011-12-28 15:16:07 +000054 /* Atom E620 */
55 { 0x8086, 0x4108, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops},
Alan Cox5c49fd32011-11-03 18:22:04 +000056#endif
Kirill A. Shutemov026abc32012-03-08 16:02:20 +000057#if defined(CONFIG_DRM_MEDFIELD)
58 {0x8086, 0x0130, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &mdfld_chip_ops},
59 {0x8086, 0x0131, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &mdfld_chip_ops},
60 {0x8086, 0x0132, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &mdfld_chip_ops},
61 {0x8086, 0x0133, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &mdfld_chip_ops},
62 {0x8086, 0x0134, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &mdfld_chip_ops},
63 {0x8086, 0x0135, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &mdfld_chip_ops},
64 {0x8086, 0x0136, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &mdfld_chip_ops},
65 {0x8086, 0x0137, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &mdfld_chip_ops},
66#endif
Alan Coxb6195aa2011-12-29 14:37:03 +000067#if defined(CONFIG_DRM_GMA3600)
Alan Cox5c49fd32011-11-03 18:22:04 +000068 { 0x8086, 0x0be0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops},
69 { 0x8086, 0x0be1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops},
70 { 0x8086, 0x0be2, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops},
71 { 0x8086, 0x0be3, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops},
72 { 0x8086, 0x0be4, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops},
73 { 0x8086, 0x0be5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops},
74 { 0x8086, 0x0be6, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops},
75 { 0x8086, 0x0be7, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops},
Alan Cox9aa65a22012-05-11 11:32:46 +010076 { 0x8086, 0x0be8, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops},
77 { 0x8086, 0x0be9, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops},
78 { 0x8086, 0x0bea, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops},
79 { 0x8086, 0x0beb, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops},
80 { 0x8086, 0x0bec, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops},
81 { 0x8086, 0x0bed, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops},
82 { 0x8086, 0x0bee, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops},
83 { 0x8086, 0x0bef, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops},
Alan Cox5c49fd32011-11-03 18:22:04 +000084#endif
Kirill A. Shutemovaa53f532012-03-08 16:09:19 +000085 { 0, }
Alan Cox5c49fd32011-11-03 18:22:04 +000086};
87MODULE_DEVICE_TABLE(pci, pciidlist);
88
89/*
90 * Standard IOCTLs.
91 */
92
Kirill A. Shutemov670c0442012-03-08 16:04:32 +000093#define DRM_IOCTL_GMA_ADB \
Alan Cox770179d2011-11-16 22:39:18 +000094 DRM_IOWR(DRM_GMA_ADB + DRM_COMMAND_BASE, uint32_t)
Kirill A. Shutemov670c0442012-03-08 16:04:32 +000095#define DRM_IOCTL_GMA_MODE_OPERATION \
Alan Cox770179d2011-11-16 22:39:18 +000096 DRM_IOWR(DRM_GMA_MODE_OPERATION + DRM_COMMAND_BASE, \
Alan Cox5c49fd32011-11-03 18:22:04 +000097 struct drm_psb_mode_operation_arg)
Kirill A. Shutemov670c0442012-03-08 16:04:32 +000098#define DRM_IOCTL_GMA_STOLEN_MEMORY \
Alan Cox770179d2011-11-16 22:39:18 +000099 DRM_IOWR(DRM_GMA_STOLEN_MEMORY + DRM_COMMAND_BASE, \
Alan Cox5c49fd32011-11-03 18:22:04 +0000100 struct drm_psb_stolen_memory_arg)
Kirill A. Shutemov670c0442012-03-08 16:04:32 +0000101#define DRM_IOCTL_GMA_GAMMA \
Alan Cox770179d2011-11-16 22:39:18 +0000102 DRM_IOWR(DRM_GMA_GAMMA + DRM_COMMAND_BASE, \
Alan Cox5c49fd32011-11-03 18:22:04 +0000103 struct drm_psb_dpst_lut_arg)
Kirill A. Shutemov670c0442012-03-08 16:04:32 +0000104#define DRM_IOCTL_GMA_DPST_BL \
Alan Cox770179d2011-11-16 22:39:18 +0000105 DRM_IOWR(DRM_GMA_DPST_BL + DRM_COMMAND_BASE, \
Alan Cox5c49fd32011-11-03 18:22:04 +0000106 uint32_t)
Kirill A. Shutemov670c0442012-03-08 16:04:32 +0000107#define DRM_IOCTL_GMA_GET_PIPE_FROM_CRTC_ID \
Alan Cox770179d2011-11-16 22:39:18 +0000108 DRM_IOWR(DRM_GMA_GET_PIPE_FROM_CRTC_ID + DRM_COMMAND_BASE, \
Alan Cox5c49fd32011-11-03 18:22:04 +0000109 struct drm_psb_get_pipe_from_crtc_id_arg)
Kirill A. Shutemov670c0442012-03-08 16:04:32 +0000110#define DRM_IOCTL_GMA_GEM_CREATE \
Alan Cox770179d2011-11-16 22:39:18 +0000111 DRM_IOWR(DRM_GMA_GEM_CREATE + DRM_COMMAND_BASE, \
Alan Cox5c49fd32011-11-03 18:22:04 +0000112 struct drm_psb_gem_create)
Kirill A. Shutemov670c0442012-03-08 16:04:32 +0000113#define DRM_IOCTL_GMA_GEM_MMAP \
Alan Cox770179d2011-11-16 22:39:18 +0000114 DRM_IOWR(DRM_GMA_GEM_MMAP + DRM_COMMAND_BASE, \
Alan Cox5c49fd32011-11-03 18:22:04 +0000115 struct drm_psb_gem_mmap)
116
Alan Cox5c49fd32011-11-03 18:22:04 +0000117static int psb_adb_ioctl(struct drm_device *dev, void *data,
118 struct drm_file *file_priv);
119static int psb_mode_operation_ioctl(struct drm_device *dev, void *data,
120 struct drm_file *file_priv);
121static int psb_stolen_memory_ioctl(struct drm_device *dev, void *data,
122 struct drm_file *file_priv);
Alan Cox5c49fd32011-11-03 18:22:04 +0000123static int psb_gamma_ioctl(struct drm_device *dev, void *data,
124 struct drm_file *file_priv);
125static int psb_dpst_bl_ioctl(struct drm_device *dev, void *data,
126 struct drm_file *file_priv);
127
Rob Clarkbaa70942013-08-02 13:27:49 -0400128static const struct drm_ioctl_desc psb_ioctls[] = {
Kirill A. Shutemov670c0442012-03-08 16:04:32 +0000129 DRM_IOCTL_DEF_DRV(GMA_ADB, psb_adb_ioctl, DRM_AUTH),
130 DRM_IOCTL_DEF_DRV(GMA_MODE_OPERATION, psb_mode_operation_ioctl,
Alan Cox5c49fd32011-11-03 18:22:04 +0000131 DRM_AUTH),
Kirill A. Shutemov670c0442012-03-08 16:04:32 +0000132 DRM_IOCTL_DEF_DRV(GMA_STOLEN_MEMORY, psb_stolen_memory_ioctl,
Alan Cox5c49fd32011-11-03 18:22:04 +0000133 DRM_AUTH),
Kirill A. Shutemov670c0442012-03-08 16:04:32 +0000134 DRM_IOCTL_DEF_DRV(GMA_GAMMA, psb_gamma_ioctl, DRM_AUTH),
135 DRM_IOCTL_DEF_DRV(GMA_DPST_BL, psb_dpst_bl_ioctl, DRM_AUTH),
136 DRM_IOCTL_DEF_DRV(GMA_GET_PIPE_FROM_CRTC_ID,
Alan Cox5c49fd32011-11-03 18:22:04 +0000137 psb_intel_get_pipe_from_crtc_id, 0),
Kirill A. Shutemov670c0442012-03-08 16:04:32 +0000138 DRM_IOCTL_DEF_DRV(GMA_GEM_CREATE, psb_gem_create_ioctl,
Alan Cox5c49fd32011-11-03 18:22:04 +0000139 DRM_UNLOCKED | DRM_AUTH),
Kirill A. Shutemov670c0442012-03-08 16:04:32 +0000140 DRM_IOCTL_DEF_DRV(GMA_GEM_MMAP, psb_gem_mmap_ioctl,
Alan Cox5c49fd32011-11-03 18:22:04 +0000141 DRM_UNLOCKED | DRM_AUTH),
142};
143
144static void psb_lastclose(struct drm_device *dev)
145{
Daniel Vetter71475732012-12-02 21:55:41 +0100146 int ret;
147 struct drm_psb_private *dev_priv = dev->dev_private;
148 struct psb_fbdev *fbdev = dev_priv->fbdev;
149
Daniel Vetter0a819512012-12-02 01:33:17 +0100150 drm_modeset_lock_all(dev);
Daniel Vetter71475732012-12-02 21:55:41 +0100151 ret = drm_fb_helper_restore_fbdev_mode(&fbdev->psb_fb_helper);
152 if (ret)
153 DRM_DEBUG("failed to restore crtc mode\n");
Daniel Vetter0a819512012-12-02 01:33:17 +0100154 drm_modeset_unlock_all(dev);
Daniel Vetter71475732012-12-02 21:55:41 +0100155
Alan Cox5c49fd32011-11-03 18:22:04 +0000156 return;
157}
158
Alan Cox5c49fd32011-11-03 18:22:04 +0000159static int psb_do_init(struct drm_device *dev)
160{
161 struct drm_psb_private *dev_priv = dev->dev_private;
162 struct psb_gtt *pg = &dev_priv->gtt;
163
164 uint32_t stolen_gtt;
165
166 int ret = -ENOMEM;
167
168 if (pg->mmu_gatt_start & 0x0FFFFFFF) {
169 dev_err(dev->dev, "Gatt must be 256M aligned. This is a bug.\n");
170 ret = -EINVAL;
171 goto out_err;
172 }
173
174
175 stolen_gtt = (pg->stolen_size >> PAGE_SHIFT) * 4;
176 stolen_gtt = (stolen_gtt + PAGE_SIZE - 1) >> PAGE_SHIFT;
177 stolen_gtt =
178 (stolen_gtt < pg->gtt_pages) ? stolen_gtt : pg->gtt_pages;
179
180 dev_priv->gatt_free_offset = pg->mmu_gatt_start +
181 (stolen_gtt << PAGE_SHIFT) * 1024;
182
Alan Cox5c49fd32011-11-03 18:22:04 +0000183 spin_lock_init(&dev_priv->irqmask_lock);
Alan Cox9242fe22011-11-29 22:27:10 +0000184 spin_lock_init(&dev_priv->lock_2d);
Alan Cox5c49fd32011-11-03 18:22:04 +0000185
186 PSB_WSGX32(0x00000000, PSB_CR_BIF_BANK0);
187 PSB_WSGX32(0x00000000, PSB_CR_BIF_BANK1);
188 PSB_RSGX32(PSB_CR_BIF_BANK1);
Patrik Jakobssonae012bd2014-01-04 22:11:17 +0100189
190 /* Do not bypass any MMU access, let them pagefault instead */
191 PSB_WSGX32((PSB_RSGX32(PSB_CR_BIF_CTRL) & ~_PSB_MMU_ER_MASK),
192 PSB_CR_BIF_CTRL);
193 PSB_RSGX32(PSB_CR_BIF_CTRL);
194
Alan Cox5c49fd32011-11-03 18:22:04 +0000195 psb_spank(dev_priv);
196
197 /* mmu_gatt ?? */
198 PSB_WSGX32(pg->gatt_start, PSB_CR_BIF_TWOD_REQ_BASE);
Patrik Jakobssonae012bd2014-01-04 22:11:17 +0100199 PSB_RSGX32(PSB_CR_BIF_TWOD_REQ_BASE); /* Post */
200
Alan Cox5c49fd32011-11-03 18:22:04 +0000201 return 0;
202out_err:
Alan Cox5c49fd32011-11-03 18:22:04 +0000203 return ret;
204}
205
206static int psb_driver_unload(struct drm_device *dev)
207{
208 struct drm_psb_private *dev_priv = dev->dev_private;
209
210 /* Kill vblank etc here */
211
Alan Cox5c49fd32011-11-03 18:22:04 +0000212
213 if (dev_priv) {
Alan Cox4ab2c7f2012-05-14 12:04:00 +0100214 if (dev_priv->backlight_device)
215 gma_backlight_exit(dev);
216 psb_modeset_cleanup(dev);
Alan Cox5c49fd32011-11-03 18:22:04 +0000217
218 if (dev_priv->ops->chip_teardown)
219 dev_priv->ops->chip_teardown(dev);
Alan Cox6607e022012-05-14 12:03:34 +0100220
221 psb_intel_opregion_fini(dev);
Alan Cox5c49fd32011-11-03 18:22:04 +0000222
223 if (dev_priv->pf_pd) {
224 psb_mmu_free_pagedir(dev_priv->pf_pd);
225 dev_priv->pf_pd = NULL;
226 }
227 if (dev_priv->mmu) {
228 struct psb_gtt *pg = &dev_priv->gtt;
229
230 down_read(&pg->sem);
231 psb_mmu_remove_pfn_sequence(
232 psb_mmu_get_default_pd
233 (dev_priv->mmu),
234 pg->mmu_gatt_start,
235 dev_priv->vram_stolen_size >> PAGE_SHIFT);
236 up_read(&pg->sem);
237 psb_mmu_driver_takedown(dev_priv->mmu);
238 dev_priv->mmu = NULL;
239 }
240 psb_gtt_takedown(dev);
241 if (dev_priv->scratch_page) {
Alan Cox18963442012-04-25 14:36:13 +0100242 set_pages_wb(dev_priv->scratch_page, 1);
Alan Cox5c49fd32011-11-03 18:22:04 +0000243 __free_page(dev_priv->scratch_page);
244 dev_priv->scratch_page = NULL;
245 }
246 if (dev_priv->vdc_reg) {
247 iounmap(dev_priv->vdc_reg);
248 dev_priv->vdc_reg = NULL;
249 }
250 if (dev_priv->sgx_reg) {
251 iounmap(dev_priv->sgx_reg);
252 dev_priv->sgx_reg = NULL;
253 }
Patrik Jakobsson26579292013-09-16 18:02:40 +0200254 if (dev_priv->aux_reg) {
255 iounmap(dev_priv->aux_reg);
256 dev_priv->aux_reg = NULL;
257 }
258 if (dev_priv->aux_pdev)
259 pci_dev_put(dev_priv->aux_pdev);
Alan Cox5c49fd32011-11-03 18:22:04 +0000260
Alan Cox4ab2c7f2012-05-14 12:04:00 +0100261 /* Destroy VBT data */
262 psb_intel_destroy_bios(dev);
263
Alan Cox5c49fd32011-11-03 18:22:04 +0000264 kfree(dev_priv);
265 dev->dev_private = NULL;
Alan Cox5c49fd32011-11-03 18:22:04 +0000266 }
Alan Cox5c49fd32011-11-03 18:22:04 +0000267 gma_power_uninit(dev);
Alan Cox5c49fd32011-11-03 18:22:04 +0000268 return 0;
269}
270
271
272static int psb_driver_load(struct drm_device *dev, unsigned long chipset)
273{
274 struct drm_psb_private *dev_priv;
Patrik Jakobsson26579292013-09-16 18:02:40 +0200275 unsigned long resource_start, resource_len;
Alan Cox5c49fd32011-11-03 18:22:04 +0000276 unsigned long irqflags;
277 int ret = -ENOMEM;
Alan Cox5c49fd32011-11-03 18:22:04 +0000278 struct drm_connector *connector;
Patrik Jakobsson367e4402013-07-22 17:45:26 +0200279 struct gma_encoder *gma_encoder;
Patrik Jakobssonae012bd2014-01-04 22:11:17 +0100280 struct psb_gtt *pg;
Alan Cox5c49fd32011-11-03 18:22:04 +0000281
282 dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
283 if (dev_priv == NULL)
284 return -ENOMEM;
285
286 dev_priv->ops = (struct psb_ops *)chipset;
287 dev_priv->dev = dev;
288 dev->dev_private = (void *) dev_priv;
289
Patrik Jakobssonae012bd2014-01-04 22:11:17 +0100290 pg = &dev_priv->gtt;
291
Dave Airlie466e69b2011-12-19 11:15:29 +0000292 pci_set_master(dev->pdev);
293
Alan Cox5c49fd32011-11-03 18:22:04 +0000294 dev_priv->num_pipe = dev_priv->ops->pipes;
295
296 resource_start = pci_resource_start(dev->pdev, PSB_MMIO_RESOURCE);
297
298 dev_priv->vdc_reg =
299 ioremap(resource_start + PSB_VDC_OFFSET, PSB_VDC_SIZE);
300 if (!dev_priv->vdc_reg)
301 goto out_err;
302
303 dev_priv->sgx_reg = ioremap(resource_start + dev_priv->ops->sgx_offset,
304 PSB_SGX_SIZE);
305 if (!dev_priv->sgx_reg)
306 goto out_err;
307
Patrik Jakobsson26579292013-09-16 18:02:40 +0200308 if (IS_MRST(dev)) {
309 dev_priv->aux_pdev = pci_get_bus_and_slot(0, PCI_DEVFN(3, 0));
310
311 if (dev_priv->aux_pdev) {
312 resource_start = pci_resource_start(dev_priv->aux_pdev,
313 PSB_AUX_RESOURCE);
314 resource_len = pci_resource_len(dev_priv->aux_pdev,
315 PSB_AUX_RESOURCE);
316 dev_priv->aux_reg = ioremap_nocache(resource_start,
317 resource_len);
318 if (!dev_priv->aux_reg)
319 goto out_err;
320
321 DRM_DEBUG_KMS("Found aux vdc");
322 } else {
323 /* Couldn't find the aux vdc so map to primary vdc */
324 dev_priv->aux_reg = dev_priv->vdc_reg;
325 DRM_DEBUG_KMS("Couldn't find aux pci device");
326 }
327 dev_priv->gmbus_reg = dev_priv->aux_reg;
328 } else {
329 dev_priv->gmbus_reg = dev_priv->vdc_reg;
330 }
331
Alan Coxd839ede2012-05-03 15:06:18 +0100332 psb_intel_opregion_setup(dev);
333
Alan Cox5c49fd32011-11-03 18:22:04 +0000334 ret = dev_priv->ops->chip_setup(dev);
335 if (ret)
336 goto out_err;
337
338 /* Init OSPM support */
339 gma_power_init(dev);
340
341 ret = -ENOMEM;
342
343 dev_priv->scratch_page = alloc_page(GFP_DMA32 | __GFP_ZERO);
344 if (!dev_priv->scratch_page)
345 goto out_err;
346
347 set_pages_uc(dev_priv->scratch_page, 1);
348
349 ret = psb_gtt_init(dev, 0);
350 if (ret)
351 goto out_err;
352
Patrik Jakobsson8f394872014-01-05 00:27:51 +0100353 dev_priv->mmu = psb_mmu_driver_init(dev, 1, 0, 0);
Alan Cox5c49fd32011-11-03 18:22:04 +0000354 if (!dev_priv->mmu)
355 goto out_err;
356
Alan Cox5c49fd32011-11-03 18:22:04 +0000357 dev_priv->pf_pd = psb_mmu_alloc_pd(dev_priv->mmu, 1, 0);
358 if (!dev_priv->pf_pd)
359 goto out_err;
360
Alan Cox5c49fd32011-11-03 18:22:04 +0000361 ret = psb_do_init(dev);
362 if (ret)
363 return ret;
364
Patrik Jakobssonae012bd2014-01-04 22:11:17 +0100365 /* Add stolen memory to SGX MMU */
366 down_read(&pg->sem);
367 ret = psb_mmu_insert_pfn_sequence(psb_mmu_get_default_pd(dev_priv->mmu),
368 dev_priv->stolen_base >> PAGE_SHIFT,
369 pg->gatt_start,
370 pg->stolen_size >> PAGE_SHIFT, 0);
371 up_read(&pg->sem);
372
373 psb_mmu_set_pd_context(psb_mmu_get_default_pd(dev_priv->mmu), 0);
374 psb_mmu_set_pd_context(dev_priv->pf_pd, 1);
375
Alan Cox5c49fd32011-11-03 18:22:04 +0000376 PSB_WSGX32(0x20000000, PSB_CR_PDS_EXEC_BASE);
377 PSB_WSGX32(0x30000000, PSB_CR_BIF_3D_REQ_BASE);
378
Alan Coxd839ede2012-05-03 15:06:18 +0100379 acpi_video_register();
Alan Cox5c49fd32011-11-03 18:22:04 +0000380
381 ret = drm_vblank_init(dev, dev_priv->num_pipe);
382 if (ret)
383 goto out_err;
384
385 /*
386 * Install interrupt handlers prior to powering off SGX or else we will
387 * crash.
388 */
389 dev_priv->vdc_irq_mask = 0;
390 dev_priv->pipestat[0] = 0;
391 dev_priv->pipestat[1] = 0;
392 dev_priv->pipestat[2] = 0;
393 spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
394 PSB_WVDC32(0xFFFFFFFF, PSB_HWSTAM);
395 PSB_WVDC32(0x00000000, PSB_INT_ENABLE_R);
396 PSB_WVDC32(0xFFFFFFFF, PSB_INT_MASK_R);
397 spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
Alan Cox62363a42012-05-11 11:33:17 +0100398
399 drm_irq_install(dev);
Alan Cox5c49fd32011-11-03 18:22:04 +0000400
Ville Syrjäläba0bf122013-10-04 14:53:33 +0300401 dev->vblank_disable_allowed = true;
Alan Cox5c49fd32011-11-03 18:22:04 +0000402
403 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
404
405 dev->driver->get_vblank_counter = psb_get_vblank_counter;
406
Alan Coxf9f23a72011-12-29 14:38:07 +0000407 psb_modeset_init(dev);
408 psb_fbdev_init(dev);
409 drm_kms_helper_poll_init(dev);
Alan Cox5c49fd32011-11-03 18:22:04 +0000410
411 /* Only add backlight support if we have LVDS output */
412 list_for_each_entry(connector, &dev->mode_config.connector_list,
413 head) {
Patrik Jakobsson367e4402013-07-22 17:45:26 +0200414 gma_encoder = gma_attached_encoder(connector);
Alan Cox5c49fd32011-11-03 18:22:04 +0000415
Patrik Jakobsson367e4402013-07-22 17:45:26 +0200416 switch (gma_encoder->type) {
Alan Cox5c49fd32011-11-03 18:22:04 +0000417 case INTEL_OUTPUT_LVDS:
418 case INTEL_OUTPUT_MIPI:
419 ret = gma_backlight_init(dev);
420 break;
421 }
422 }
423
424 if (ret)
425 return ret;
Alan Cox166973e2012-07-16 17:52:45 +0100426 psb_intel_opregion_enable_asle(dev);
Alan Cox5c49fd32011-11-03 18:22:04 +0000427#if 0
428 /*enable runtime pm at last*/
429 pm_runtime_enable(&dev->pdev->dev);
430 pm_runtime_set_active(&dev->pdev->dev);
431#endif
432 /*Intel drm driver load is done, continue doing pvr load*/
433 return 0;
434out_err:
435 psb_driver_unload(dev);
436 return ret;
437}
438
Kirill A. Shutemov15308e22012-03-08 16:08:45 +0000439static int psb_driver_device_is_agp(struct drm_device *dev)
Alan Cox5c49fd32011-11-03 18:22:04 +0000440{
441 return 0;
442}
443
Alan Cox5c49fd32011-11-03 18:22:04 +0000444static inline void get_brightness(struct backlight_device *bd)
445{
446#ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
447 if (bd) {
448 bd->props.brightness = bd->ops->get_brightness(bd);
449 backlight_update_status(bd);
450 }
451#endif
452}
453
454static int psb_dpst_bl_ioctl(struct drm_device *dev, void *data,
455 struct drm_file *file_priv)
456{
457 struct drm_psb_private *dev_priv = psb_priv(dev);
458 uint32_t *arg = data;
459
460 dev_priv->blc_adj2 = *arg;
461 get_brightness(dev_priv->backlight_device);
462 return 0;
463}
464
465static int psb_adb_ioctl(struct drm_device *dev, void *data,
466 struct drm_file *file_priv)
467{
468 struct drm_psb_private *dev_priv = psb_priv(dev);
469 uint32_t *arg = data;
470
471 dev_priv->blc_adj1 = *arg;
472 get_brightness(dev_priv->backlight_device);
473 return 0;
474}
475
Alan Cox5c49fd32011-11-03 18:22:04 +0000476static int psb_gamma_ioctl(struct drm_device *dev, void *data,
477 struct drm_file *file_priv)
478{
479 struct drm_psb_dpst_lut_arg *lut_arg = data;
480 struct drm_mode_object *obj;
481 struct drm_crtc *crtc;
482 struct drm_connector *connector;
Patrik Jakobsson63068652013-07-22 01:31:23 +0200483 struct gma_crtc *gma_crtc;
Alan Cox5c49fd32011-11-03 18:22:04 +0000484 int i = 0;
485 int32_t obj_id;
486
487 obj_id = lut_arg->output_id;
488 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_CONNECTOR);
489 if (!obj) {
490 dev_dbg(dev->dev, "Invalid Connector object.\n");
Ville Syrjälä4f224662013-10-17 13:35:02 +0300491 return -ENOENT;
Alan Cox5c49fd32011-11-03 18:22:04 +0000492 }
493
494 connector = obj_to_connector(obj);
495 crtc = connector->encoder->crtc;
Patrik Jakobsson63068652013-07-22 01:31:23 +0200496 gma_crtc = to_gma_crtc(crtc);
Alan Cox5c49fd32011-11-03 18:22:04 +0000497
498 for (i = 0; i < 256; i++)
Patrik Jakobsson63068652013-07-22 01:31:23 +0200499 gma_crtc->lut_adj[i] = lut_arg->lut[i];
Alan Cox5c49fd32011-11-03 18:22:04 +0000500
Patrik Jakobsson6443ea12013-07-10 18:39:58 +0200501 gma_crtc_load_lut(crtc);
Alan Cox5c49fd32011-11-03 18:22:04 +0000502
503 return 0;
504}
505
506static int psb_mode_operation_ioctl(struct drm_device *dev, void *data,
507 struct drm_file *file_priv)
508{
509 uint32_t obj_id;
510 uint16_t op;
511 struct drm_mode_modeinfo *umode;
512 struct drm_display_mode *mode = NULL;
513 struct drm_psb_mode_operation_arg *arg;
514 struct drm_mode_object *obj;
515 struct drm_connector *connector;
Alan Cox5c49fd32011-11-03 18:22:04 +0000516 struct drm_connector_helper_funcs *connector_funcs;
517 int ret = 0;
518 int resp = MODE_OK;
Alan Cox5c49fd32011-11-03 18:22:04 +0000519
520 arg = (struct drm_psb_mode_operation_arg *)data;
521 obj_id = arg->obj_id;
522 op = arg->operation;
523
524 switch (op) {
Alan Cox5c49fd32011-11-03 18:22:04 +0000525 case PSB_MODE_OPERATION_MODE_VALID:
526 umode = &arg->mode;
527
Daniel Vetter0a819512012-12-02 01:33:17 +0100528 drm_modeset_lock_all(dev);
Alan Cox5c49fd32011-11-03 18:22:04 +0000529
530 obj = drm_mode_object_find(dev, obj_id,
531 DRM_MODE_OBJECT_CONNECTOR);
532 if (!obj) {
Ville Syrjälä4f224662013-10-17 13:35:02 +0300533 ret = -ENOENT;
Alan Cox5c49fd32011-11-03 18:22:04 +0000534 goto mode_op_out;
535 }
536
537 connector = obj_to_connector(obj);
538
539 mode = drm_mode_create(dev);
540 if (!mode) {
541 ret = -ENOMEM;
542 goto mode_op_out;
543 }
544
545 /* drm_crtc_convert_umode(mode, umode); */
546 {
547 mode->clock = umode->clock;
548 mode->hdisplay = umode->hdisplay;
549 mode->hsync_start = umode->hsync_start;
550 mode->hsync_end = umode->hsync_end;
551 mode->htotal = umode->htotal;
552 mode->hskew = umode->hskew;
553 mode->vdisplay = umode->vdisplay;
554 mode->vsync_start = umode->vsync_start;
555 mode->vsync_end = umode->vsync_end;
556 mode->vtotal = umode->vtotal;
557 mode->vscan = umode->vscan;
558 mode->vrefresh = umode->vrefresh;
559 mode->flags = umode->flags;
560 mode->type = umode->type;
561 strncpy(mode->name, umode->name, DRM_DISPLAY_MODE_LEN);
562 mode->name[DRM_DISPLAY_MODE_LEN-1] = 0;
563 }
564
565 connector_funcs = (struct drm_connector_helper_funcs *)
566 connector->helper_private;
567
568 if (connector_funcs->mode_valid) {
569 resp = connector_funcs->mode_valid(connector, mode);
Alan Cox61bedf72011-11-16 22:38:42 +0000570 arg->data = resp;
Alan Cox5c49fd32011-11-03 18:22:04 +0000571 }
572
573 /*do some clean up work*/
574 if (mode)
575 drm_mode_destroy(dev, mode);
576mode_op_out:
Daniel Vetter0a819512012-12-02 01:33:17 +0100577 drm_modeset_unlock_all(dev);
Alan Cox5c49fd32011-11-03 18:22:04 +0000578 return ret;
579
580 default:
581 dev_dbg(dev->dev, "Unsupported psb mode operation\n");
582 return -EOPNOTSUPP;
583 }
584
585 return 0;
586}
587
588static int psb_stolen_memory_ioctl(struct drm_device *dev, void *data,
589 struct drm_file *file_priv)
590{
591 struct drm_psb_private *dev_priv = psb_priv(dev);
592 struct drm_psb_stolen_memory_arg *arg = data;
593
594 arg->base = dev_priv->stolen_base;
595 arg->size = dev_priv->vram_stolen_size;
596
597 return 0;
598}
599
Alan Cox5c49fd32011-11-03 18:22:04 +0000600static int psb_driver_open(struct drm_device *dev, struct drm_file *priv)
601{
602 return 0;
603}
604
605static void psb_driver_close(struct drm_device *dev, struct drm_file *priv)
606{
607}
608
609static long psb_unlocked_ioctl(struct file *filp, unsigned int cmd,
610 unsigned long arg)
611{
612 struct drm_file *file_priv = filp->private_data;
613 struct drm_device *dev = file_priv->minor->dev;
614 struct drm_psb_private *dev_priv = dev->dev_private;
615 static unsigned int runtime_allowed;
616
617 if (runtime_allowed == 1 && dev_priv->is_lvds_on) {
618 runtime_allowed++;
619 pm_runtime_allow(&dev->pdev->dev);
620 dev_priv->rpm_enabled = 1;
621 }
622 return drm_ioctl(filp, cmd, arg);
623 /* FIXME: do we need to wrap the other side of this */
624}
625
626
627/* When a client dies:
628 * - Check for and clean up flipped page state
629 */
Kirill A. Shutemov15308e22012-03-08 16:08:45 +0000630static void psb_driver_preclose(struct drm_device *dev, struct drm_file *priv)
Alan Cox5c49fd32011-11-03 18:22:04 +0000631{
632}
633
634static void psb_remove(struct pci_dev *pdev)
635{
636 struct drm_device *dev = pci_get_drvdata(pdev);
637 drm_put_dev(dev);
638}
639
640static const struct dev_pm_ops psb_pm_ops = {
641 .resume = gma_power_resume,
642 .suspend = gma_power_suspend,
Patrik Jakobsson888eef22013-04-06 00:45:22 +0200643 .thaw = gma_power_thaw,
644 .freeze = gma_power_freeze,
645 .restore = gma_power_restore,
Alan Cox5c49fd32011-11-03 18:22:04 +0000646 .runtime_suspend = psb_runtime_suspend,
647 .runtime_resume = psb_runtime_resume,
648 .runtime_idle = psb_runtime_idle,
649};
650
Laurent Pinchart78b68552012-05-17 13:27:22 +0200651static const struct vm_operations_struct psb_gem_vm_ops = {
Alan Cox5c49fd32011-11-03 18:22:04 +0000652 .fault = psb_gem_fault,
653 .open = drm_gem_vm_open,
654 .close = drm_gem_vm_close,
655};
656
Ilija Hadzic1595c562011-11-27 11:08:33 -0500657static const struct file_operations psb_gem_fops = {
658 .owner = THIS_MODULE,
659 .open = drm_open,
660 .release = drm_release,
661 .unlocked_ioctl = psb_unlocked_ioctl,
662 .mmap = drm_gem_mmap,
663 .poll = drm_poll,
Ilija Hadzic1595c562011-11-27 11:08:33 -0500664 .read = drm_read,
665};
666
Alan Cox5c49fd32011-11-03 18:22:04 +0000667static struct drm_driver driver = {
668 .driver_features = DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | \
Daniel Vetter74867e32013-08-08 15:41:25 +0200669 DRIVER_MODESET | DRIVER_GEM ,
Alan Cox5c49fd32011-11-03 18:22:04 +0000670 .load = psb_driver_load,
671 .unload = psb_driver_unload,
672
673 .ioctls = psb_ioctls,
674 .num_ioctls = DRM_ARRAY_SIZE(psb_ioctls),
675 .device_is_agp = psb_driver_device_is_agp,
676 .irq_preinstall = psb_irq_preinstall,
677 .irq_postinstall = psb_irq_postinstall,
678 .irq_uninstall = psb_irq_uninstall,
679 .irq_handler = psb_irq_handler,
680 .enable_vblank = psb_enable_vblank,
681 .disable_vblank = psb_disable_vblank,
682 .get_vblank_counter = psb_get_vblank_counter,
683 .lastclose = psb_lastclose,
684 .open = psb_driver_open,
685 .preclose = psb_driver_preclose,
686 .postclose = psb_driver_close,
Alan Cox5c49fd32011-11-03 18:22:04 +0000687
Alan Cox5c49fd32011-11-03 18:22:04 +0000688 .gem_free_object = psb_gem_free_object,
689 .gem_vm_ops = &psb_gem_vm_ops,
690 .dumb_create = psb_gem_dumb_create,
691 .dumb_map_offset = psb_gem_dumb_map_gtt,
Daniel Vetter43387b32013-07-16 09:12:04 +0200692 .dumb_destroy = drm_gem_dumb_destroy,
Ilija Hadzic1595c562011-11-27 11:08:33 -0500693 .fops = &psb_gem_fops,
Alan Cox5c49fd32011-11-03 18:22:04 +0000694 .name = DRIVER_NAME,
695 .desc = DRIVER_DESC,
696 .date = PSB_DRM_DRIVER_DATE,
697 .major = PSB_DRM_DRIVER_MAJOR,
698 .minor = PSB_DRM_DRIVER_MINOR,
699 .patchlevel = PSB_DRM_DRIVER_PATCHLEVEL
700};
701
702static struct pci_driver psb_pci_driver = {
703 .name = DRIVER_NAME,
704 .id_table = pciidlist,
705 .probe = psb_probe,
706 .remove = psb_remove,
Kirill A. Shutemovaa53f532012-03-08 16:09:19 +0000707 .driver = {
708 .pm = &psb_pm_ops,
709 }
Alan Cox5c49fd32011-11-03 18:22:04 +0000710};
711
712static int psb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
713{
Alan Cox5c49fd32011-11-03 18:22:04 +0000714 return drm_get_pci_dev(pdev, ent, &driver);
715}
716
717static int __init psb_init(void)
718{
719 return drm_pci_init(&driver, &psb_pci_driver);
720}
721
722static void __exit psb_exit(void)
723{
724 drm_pci_exit(&driver, &psb_pci_driver);
725}
726
727late_initcall(psb_init);
728module_exit(psb_exit);
729
730MODULE_AUTHOR("Alan Cox <alan@linux.intel.com> and others");
731MODULE_DESCRIPTION(DRIVER_DESC);
732MODULE_LICENSE("GPL");