f6d5f3ce755d882949705791371099d03d504a68
[linux-2.6.git] / drivers / gpu / drm / gma500 / psb_drv.c
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 <drm/drmP.h>
23 #include <drm/drm.h>
24 #include "gma_drm.h"
25 #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>
38 #include <linux/module.h>
39
40 static int drm_psb_trap_pagefaults;
41
42 int drm_psb_no_fb;
43
44 static int psb_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
45
46 MODULE_PARM_DESC(no_fb, "Disable FBdev");
47 MODULE_PARM_DESC(trap_pagefaults, "Error and reset on MMU pagefaults");
48 module_param_named(no_fb, drm_psb_no_fb, int, 0600);
49 module_param_named(trap_pagefaults, drm_psb_trap_pagefaults, int, 0600);
50
51
52 static DEFINE_PCI_DEVICE_TABLE(pciidlist) = {
53         { 0x8086, 0x8108, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &psb_chip_ops },
54         { 0x8086, 0x8109, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &psb_chip_ops },
55 #if defined(CONFIG_DRM_OAKTRAIL)
56         { 0x8086, 0x4100, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops},
57         { 0x8086, 0x4101, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops},
58         { 0x8086, 0x4102, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops},
59         { 0x8086, 0x4103, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops},
60         { 0x8086, 0x4104, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops},
61         { 0x8086, 0x4105, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops},
62         { 0x8086, 0x4106, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops},
63         { 0x8086, 0x4107, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &oaktrail_chip_ops},
64 #endif
65 #if defined(CONFIG_DRM_CDV)
66         { 0x8086, 0x0be0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops},
67         { 0x8086, 0x0be1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops},
68         { 0x8086, 0x0be2, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops},
69         { 0x8086, 0x0be3, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops},
70         { 0x8086, 0x0be4, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops},
71         { 0x8086, 0x0be5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops},
72         { 0x8086, 0x0be6, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops},
73         { 0x8086, 0x0be7, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (long) &cdv_chip_ops},
74 #endif
75         { 0, 0, 0}
76 };
77 MODULE_DEVICE_TABLE(pci, pciidlist);
78
79 /*
80  * Standard IOCTLs.
81  */
82
83 #define DRM_IOCTL_PSB_ADB       \
84                 DRM_IOWR(DRM_GMA_ADB + DRM_COMMAND_BASE, uint32_t)
85 #define DRM_IOCTL_PSB_MODE_OPERATION    \
86                 DRM_IOWR(DRM_GMA_MODE_OPERATION + DRM_COMMAND_BASE, \
87                          struct drm_psb_mode_operation_arg)
88 #define DRM_IOCTL_PSB_STOLEN_MEMORY     \
89                 DRM_IOWR(DRM_GMA_STOLEN_MEMORY + DRM_COMMAND_BASE, \
90                          struct drm_psb_stolen_memory_arg)
91 #define DRM_IOCTL_PSB_GAMMA     \
92                 DRM_IOWR(DRM_GMA_GAMMA + DRM_COMMAND_BASE, \
93                          struct drm_psb_dpst_lut_arg)
94 #define DRM_IOCTL_PSB_DPST_BL   \
95                 DRM_IOWR(DRM_GMA_DPST_BL + DRM_COMMAND_BASE, \
96                          uint32_t)
97 #define DRM_IOCTL_PSB_GET_PIPE_FROM_CRTC_ID     \
98                 DRM_IOWR(DRM_GMA_GET_PIPE_FROM_CRTC_ID + DRM_COMMAND_BASE, \
99                          struct drm_psb_get_pipe_from_crtc_id_arg)
100 #define DRM_IOCTL_PSB_GEM_CREATE        \
101                 DRM_IOWR(DRM_GMA_GEM_CREATE + DRM_COMMAND_BASE, \
102                          struct drm_psb_gem_create)
103 #define DRM_IOCTL_PSB_GEM_MMAP  \
104                 DRM_IOWR(DRM_GMA_GEM_MMAP + DRM_COMMAND_BASE, \
105                          struct drm_psb_gem_mmap)
106
107 static int psb_adb_ioctl(struct drm_device *dev, void *data,
108                          struct drm_file *file_priv);
109 static int psb_mode_operation_ioctl(struct drm_device *dev, void *data,
110                                     struct drm_file *file_priv);
111 static int psb_stolen_memory_ioctl(struct drm_device *dev, void *data,
112                                    struct drm_file *file_priv);
113 static int psb_gamma_ioctl(struct drm_device *dev, void *data,
114                            struct drm_file *file_priv);
115 static int psb_dpst_bl_ioctl(struct drm_device *dev, void *data,
116                              struct drm_file *file_priv);
117
118 #define PSB_IOCTL_DEF(ioctl, func, flags) \
119         [DRM_IOCTL_NR(ioctl) - DRM_COMMAND_BASE] = {ioctl, flags, func}
120
121 static struct drm_ioctl_desc psb_ioctls[] = {
122         PSB_IOCTL_DEF(DRM_IOCTL_PSB_ADB, psb_adb_ioctl, DRM_AUTH),
123         PSB_IOCTL_DEF(DRM_IOCTL_PSB_MODE_OPERATION, psb_mode_operation_ioctl,
124                       DRM_AUTH),
125         PSB_IOCTL_DEF(DRM_IOCTL_PSB_STOLEN_MEMORY, psb_stolen_memory_ioctl,
126                       DRM_AUTH),
127         PSB_IOCTL_DEF(DRM_IOCTL_PSB_GAMMA, psb_gamma_ioctl, DRM_AUTH),
128         PSB_IOCTL_DEF(DRM_IOCTL_PSB_DPST_BL, psb_dpst_bl_ioctl, DRM_AUTH),
129         PSB_IOCTL_DEF(DRM_IOCTL_PSB_GET_PIPE_FROM_CRTC_ID,
130                                         psb_intel_get_pipe_from_crtc_id, 0),
131         PSB_IOCTL_DEF(DRM_IOCTL_PSB_GEM_CREATE, psb_gem_create_ioctl,
132                                                 DRM_UNLOCKED | DRM_AUTH),
133         PSB_IOCTL_DEF(DRM_IOCTL_PSB_GEM_MMAP, psb_gem_mmap_ioctl,
134                                                 DRM_UNLOCKED | DRM_AUTH),
135 };
136
137 static void psb_lastclose(struct drm_device *dev)
138 {
139         return;
140 }
141
142 static void psb_do_takedown(struct drm_device *dev)
143 {
144         /* FIXME: do we need to clean up the gtt here ? */
145 }
146
147 static int psb_do_init(struct drm_device *dev)
148 {
149         struct drm_psb_private *dev_priv = dev->dev_private;
150         struct psb_gtt *pg = &dev_priv->gtt;
151
152         uint32_t stolen_gtt;
153
154         int ret = -ENOMEM;
155
156         if (pg->mmu_gatt_start & 0x0FFFFFFF) {
157                 dev_err(dev->dev, "Gatt must be 256M aligned. This is a bug.\n");
158                 ret = -EINVAL;
159                 goto out_err;
160         }
161
162
163         stolen_gtt = (pg->stolen_size >> PAGE_SHIFT) * 4;
164         stolen_gtt = (stolen_gtt + PAGE_SIZE - 1) >> PAGE_SHIFT;
165         stolen_gtt =
166             (stolen_gtt < pg->gtt_pages) ? stolen_gtt : pg->gtt_pages;
167
168         dev_priv->gatt_free_offset = pg->mmu_gatt_start +
169             (stolen_gtt << PAGE_SHIFT) * 1024;
170
171         if (1 || drm_debug) {
172                 uint32_t core_id = PSB_RSGX32(PSB_CR_CORE_ID);
173                 uint32_t core_rev = PSB_RSGX32(PSB_CR_CORE_REVISION);
174                 DRM_INFO("SGX core id = 0x%08x\n", core_id);
175                 DRM_INFO("SGX core rev major = 0x%02x, minor = 0x%02x\n",
176                          (core_rev & _PSB_CC_REVISION_MAJOR_MASK) >>
177                          _PSB_CC_REVISION_MAJOR_SHIFT,
178                          (core_rev & _PSB_CC_REVISION_MINOR_MASK) >>
179                          _PSB_CC_REVISION_MINOR_SHIFT);
180                 DRM_INFO
181                     ("SGX core rev maintenance = 0x%02x, designer = 0x%02x\n",
182                      (core_rev & _PSB_CC_REVISION_MAINTENANCE_MASK) >>
183                      _PSB_CC_REVISION_MAINTENANCE_SHIFT,
184                      (core_rev & _PSB_CC_REVISION_DESIGNER_MASK) >>
185                      _PSB_CC_REVISION_DESIGNER_SHIFT);
186         }
187
188
189         spin_lock_init(&dev_priv->irqmask_lock);
190         mutex_init(&dev_priv->mutex_2d);
191
192         PSB_WSGX32(0x00000000, PSB_CR_BIF_BANK0);
193         PSB_WSGX32(0x00000000, PSB_CR_BIF_BANK1);
194         PSB_RSGX32(PSB_CR_BIF_BANK1);
195         PSB_WSGX32(PSB_RSGX32(PSB_CR_BIF_CTRL) | _PSB_MMU_ER_MASK,
196                                                         PSB_CR_BIF_CTRL);
197         psb_spank(dev_priv);
198
199         /* mmu_gatt ?? */
200         PSB_WSGX32(pg->gatt_start, PSB_CR_BIF_TWOD_REQ_BASE);
201         return 0;
202 out_err:
203         psb_do_takedown(dev);
204         return ret;
205 }
206
207 static int psb_driver_unload(struct drm_device *dev)
208 {
209         struct drm_psb_private *dev_priv = dev->dev_private;
210
211         /* Kill vblank etc here */
212
213         gma_backlight_exit(dev);
214
215         if (drm_psb_no_fb == 0)
216                 psb_modeset_cleanup(dev);
217
218         if (dev_priv) {
219                 psb_lid_timer_takedown(dev_priv);
220                 gma_intel_opregion_exit(dev);
221
222                 if (dev_priv->ops->chip_teardown)
223                         dev_priv->ops->chip_teardown(dev);
224                 psb_do_takedown(dev);
225
226
227                 if (dev_priv->pf_pd) {
228                         psb_mmu_free_pagedir(dev_priv->pf_pd);
229                         dev_priv->pf_pd = NULL;
230                 }
231                 if (dev_priv->mmu) {
232                         struct psb_gtt *pg = &dev_priv->gtt;
233
234                         down_read(&pg->sem);
235                         psb_mmu_remove_pfn_sequence(
236                                 psb_mmu_get_default_pd
237                                 (dev_priv->mmu),
238                                 pg->mmu_gatt_start,
239                                 dev_priv->vram_stolen_size >> PAGE_SHIFT);
240                         up_read(&pg->sem);
241                         psb_mmu_driver_takedown(dev_priv->mmu);
242                         dev_priv->mmu = NULL;
243                 }
244                 psb_gtt_takedown(dev);
245                 if (dev_priv->scratch_page) {
246                         __free_page(dev_priv->scratch_page);
247                         dev_priv->scratch_page = NULL;
248                 }
249                 if (dev_priv->vdc_reg) {
250                         iounmap(dev_priv->vdc_reg);
251                         dev_priv->vdc_reg = NULL;
252                 }
253                 if (dev_priv->sgx_reg) {
254                         iounmap(dev_priv->sgx_reg);
255                         dev_priv->sgx_reg = NULL;
256                 }
257
258                 kfree(dev_priv);
259                 dev->dev_private = NULL;
260
261                 /*destroy VBT data*/
262                 psb_intel_destroy_bios(dev);
263         }
264
265         gma_power_uninit(dev);
266
267         return 0;
268 }
269
270
271 static int psb_driver_load(struct drm_device *dev, unsigned long chipset)
272 {
273         struct drm_psb_private *dev_priv;
274         unsigned long resource_start;
275         struct psb_gtt *pg;
276         unsigned long irqflags;
277         int ret = -ENOMEM;
278         uint32_t tt_pages;
279         struct drm_connector *connector;
280         struct psb_intel_output *psb_intel_output;
281
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
290         if (!IS_PSB(dev)) {
291                 if (pci_enable_msi(dev->pdev))
292                         dev_warn(dev->dev, "Enabling MSI failed!\n");
293         }
294
295         dev_priv->num_pipe = dev_priv->ops->pipes;
296
297         resource_start = pci_resource_start(dev->pdev, PSB_MMIO_RESOURCE);
298
299         dev_priv->vdc_reg =
300             ioremap(resource_start + PSB_VDC_OFFSET, PSB_VDC_SIZE);
301         if (!dev_priv->vdc_reg)
302                 goto out_err;
303
304         dev_priv->sgx_reg = ioremap(resource_start + dev_priv->ops->sgx_offset,
305                                                         PSB_SGX_SIZE);
306         if (!dev_priv->sgx_reg)
307                 goto out_err;
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((void *)0,
329                                         drm_psb_trap_pagefaults, 0,
330                                         dev_priv);
331         if (!dev_priv->mmu)
332                 goto out_err;
333
334         pg = &dev_priv->gtt;
335
336         tt_pages = (pg->gatt_pages < PSB_TT_PRIV0_PLIMIT) ?
337                 (pg->gatt_pages) : PSB_TT_PRIV0_PLIMIT;
338
339
340         dev_priv->pf_pd = psb_mmu_alloc_pd(dev_priv->mmu, 1, 0);
341         if (!dev_priv->pf_pd)
342                 goto out_err;
343
344         psb_mmu_set_pd_context(psb_mmu_get_default_pd(dev_priv->mmu), 0);
345         psb_mmu_set_pd_context(dev_priv->pf_pd, 1);
346
347         ret = psb_do_init(dev);
348         if (ret)
349                 return ret;
350
351         PSB_WSGX32(0x20000000, PSB_CR_PDS_EXEC_BASE);
352         PSB_WSGX32(0x30000000, PSB_CR_BIF_3D_REQ_BASE);
353
354 /*      igd_opregion_init(&dev_priv->opregion_dev); */
355         acpi_video_register();
356         if (dev_priv->lid_state)
357                 psb_lid_timer_init(dev_priv);
358
359         ret = drm_vblank_init(dev, dev_priv->num_pipe);
360         if (ret)
361                 goto out_err;
362
363         /*
364          * Install interrupt handlers prior to powering off SGX or else we will
365          * crash.
366          */
367         dev_priv->vdc_irq_mask = 0;
368         dev_priv->pipestat[0] = 0;
369         dev_priv->pipestat[1] = 0;
370         dev_priv->pipestat[2] = 0;
371         spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
372         PSB_WVDC32(0xFFFFFFFF, PSB_HWSTAM);
373         PSB_WVDC32(0x00000000, PSB_INT_ENABLE_R);
374         PSB_WVDC32(0xFFFFFFFF, PSB_INT_MASK_R);
375         spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
376         if (IS_PSB(dev) && drm_core_check_feature(dev, DRIVER_MODESET))
377                 drm_irq_install(dev);
378
379         dev->vblank_disable_allowed = 1;
380
381         dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
382
383         dev->driver->get_vblank_counter = psb_get_vblank_counter;
384
385         if (drm_psb_no_fb == 0) {
386                 psb_modeset_init(dev);
387                 psb_fbdev_init(dev);
388                 drm_kms_helper_poll_init(dev);
389         }
390
391         /* Only add backlight support if we have LVDS output */
392         list_for_each_entry(connector, &dev->mode_config.connector_list,
393                             head) {
394                 psb_intel_output = to_psb_intel_output(connector);
395
396                 switch (psb_intel_output->type) {
397                 case INTEL_OUTPUT_LVDS:
398                 case INTEL_OUTPUT_MIPI:
399                         ret = gma_backlight_init(dev);
400                         break;
401                 }
402         }
403
404         if (ret)
405                 return ret;
406 #if 0
407         /*enable runtime pm at last*/
408         pm_runtime_enable(&dev->pdev->dev);
409         pm_runtime_set_active(&dev->pdev->dev);
410 #endif
411         /*Intel drm driver load is done, continue doing pvr load*/
412         return 0;
413 out_err:
414         psb_driver_unload(dev);
415         return ret;
416 }
417
418 int psb_driver_device_is_agp(struct drm_device *dev)
419 {
420         return 0;
421 }
422
423 static inline void get_brightness(struct backlight_device *bd)
424 {
425 #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
426         if (bd) {
427                 bd->props.brightness = bd->ops->get_brightness(bd);
428                 backlight_update_status(bd);
429         }
430 #endif
431 }
432
433 static int psb_dpst_bl_ioctl(struct drm_device *dev, void *data,
434                        struct drm_file *file_priv)
435 {
436         struct drm_psb_private *dev_priv = psb_priv(dev);
437         uint32_t *arg = data;
438
439         dev_priv->blc_adj2 = *arg;
440         get_brightness(dev_priv->backlight_device);
441         return 0;
442 }
443
444 static int psb_adb_ioctl(struct drm_device *dev, void *data,
445                         struct drm_file *file_priv)
446 {
447         struct drm_psb_private *dev_priv = psb_priv(dev);
448         uint32_t *arg = data;
449
450         dev_priv->blc_adj1 = *arg;
451         get_brightness(dev_priv->backlight_device);
452         return 0;
453 }
454
455 static int psb_gamma_ioctl(struct drm_device *dev, void *data,
456                            struct drm_file *file_priv)
457 {
458         struct drm_psb_dpst_lut_arg *lut_arg = data;
459         struct drm_mode_object *obj;
460         struct drm_crtc *crtc;
461         struct drm_connector *connector;
462         struct psb_intel_crtc *psb_intel_crtc;
463         int i = 0;
464         int32_t obj_id;
465
466         obj_id = lut_arg->output_id;
467         obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_CONNECTOR);
468         if (!obj) {
469                 dev_dbg(dev->dev, "Invalid Connector object.\n");
470                 return -EINVAL;
471         }
472
473         connector = obj_to_connector(obj);
474         crtc = connector->encoder->crtc;
475         psb_intel_crtc = to_psb_intel_crtc(crtc);
476
477         for (i = 0; i < 256; i++)
478                 psb_intel_crtc->lut_adj[i] = lut_arg->lut[i];
479
480         psb_intel_crtc_load_lut(crtc);
481
482         return 0;
483 }
484
485 static int psb_mode_operation_ioctl(struct drm_device *dev, void *data,
486                                 struct drm_file *file_priv)
487 {
488         uint32_t obj_id;
489         uint16_t op;
490         struct drm_mode_modeinfo *umode;
491         struct drm_display_mode *mode = NULL;
492         struct drm_psb_mode_operation_arg *arg;
493         struct drm_mode_object *obj;
494         struct drm_connector *connector;
495         struct drm_connector_helper_funcs *connector_funcs;
496         int ret = 0;
497         int resp = MODE_OK;
498
499         arg = (struct drm_psb_mode_operation_arg *)data;
500         obj_id = arg->obj_id;
501         op = arg->operation;
502
503         switch (op) {
504         case PSB_MODE_OPERATION_MODE_VALID:
505                 umode = &arg->mode;
506
507                 mutex_lock(&dev->mode_config.mutex);
508
509                 obj = drm_mode_object_find(dev, obj_id,
510                                         DRM_MODE_OBJECT_CONNECTOR);
511                 if (!obj) {
512                         ret = -EINVAL;
513                         goto mode_op_out;
514                 }
515
516                 connector = obj_to_connector(obj);
517
518                 mode = drm_mode_create(dev);
519                 if (!mode) {
520                         ret = -ENOMEM;
521                         goto mode_op_out;
522                 }
523
524                 /* drm_crtc_convert_umode(mode, umode); */
525                 {
526                         mode->clock = umode->clock;
527                         mode->hdisplay = umode->hdisplay;
528                         mode->hsync_start = umode->hsync_start;
529                         mode->hsync_end = umode->hsync_end;
530                         mode->htotal = umode->htotal;
531                         mode->hskew = umode->hskew;
532                         mode->vdisplay = umode->vdisplay;
533                         mode->vsync_start = umode->vsync_start;
534                         mode->vsync_end = umode->vsync_end;
535                         mode->vtotal = umode->vtotal;
536                         mode->vscan = umode->vscan;
537                         mode->vrefresh = umode->vrefresh;
538                         mode->flags = umode->flags;
539                         mode->type = umode->type;
540                         strncpy(mode->name, umode->name, DRM_DISPLAY_MODE_LEN);
541                         mode->name[DRM_DISPLAY_MODE_LEN-1] = 0;
542                 }
543
544                 connector_funcs = (struct drm_connector_helper_funcs *)
545                                    connector->helper_private;
546
547                 if (connector_funcs->mode_valid) {
548                         resp = connector_funcs->mode_valid(connector, mode);
549                         arg->data = resp;
550                 }
551
552                 /*do some clean up work*/
553                 if (mode)
554                         drm_mode_destroy(dev, mode);
555 mode_op_out:
556                 mutex_unlock(&dev->mode_config.mutex);
557                 return ret;
558
559         default:
560                 dev_dbg(dev->dev, "Unsupported psb mode operation\n");
561                 return -EOPNOTSUPP;
562         }
563
564         return 0;
565 }
566
567 static int psb_stolen_memory_ioctl(struct drm_device *dev, void *data,
568                                    struct drm_file *file_priv)
569 {
570         struct drm_psb_private *dev_priv = psb_priv(dev);
571         struct drm_psb_stolen_memory_arg *arg = data;
572
573         arg->base = dev_priv->stolen_base;
574         arg->size = dev_priv->vram_stolen_size;
575
576         return 0;
577 }
578
579 static int psb_driver_open(struct drm_device *dev, struct drm_file *priv)
580 {
581         return 0;
582 }
583
584 static void psb_driver_close(struct drm_device *dev, struct drm_file *priv)
585 {
586 }
587
588 static long psb_unlocked_ioctl(struct file *filp, unsigned int cmd,
589                                unsigned long arg)
590 {
591         struct drm_file *file_priv = filp->private_data;
592         struct drm_device *dev = file_priv->minor->dev;
593         struct drm_psb_private *dev_priv = dev->dev_private;
594         static unsigned int runtime_allowed;
595
596         if (runtime_allowed == 1 && dev_priv->is_lvds_on) {
597                 runtime_allowed++;
598                 pm_runtime_allow(&dev->pdev->dev);
599                 dev_priv->rpm_enabled = 1;
600         }
601         return drm_ioctl(filp, cmd, arg);
602         /* FIXME: do we need to wrap the other side of this */
603 }
604
605
606 /* When a client dies:
607  *    - Check for and clean up flipped page state
608  */
609 void psb_driver_preclose(struct drm_device *dev, struct drm_file *priv)
610 {
611 }
612
613 static void psb_remove(struct pci_dev *pdev)
614 {
615         struct drm_device *dev = pci_get_drvdata(pdev);
616         drm_put_dev(dev);
617 }
618
619 static const struct dev_pm_ops psb_pm_ops = {
620         .resume = gma_power_resume,
621         .suspend = gma_power_suspend,
622         .runtime_suspend = psb_runtime_suspend,
623         .runtime_resume = psb_runtime_resume,
624         .runtime_idle = psb_runtime_idle,
625 };
626
627 static struct vm_operations_struct psb_gem_vm_ops = {
628         .fault = psb_gem_fault,
629         .open = drm_gem_vm_open,
630         .close = drm_gem_vm_close,
631 };
632
633 static const struct file_operations psb_gem_fops = {
634         .owner = THIS_MODULE,
635         .open = drm_open,
636         .release = drm_release,
637         .unlocked_ioctl = psb_unlocked_ioctl,
638         .mmap = drm_gem_mmap,
639         .poll = drm_poll,
640         .fasync = drm_fasync,
641         .read = drm_read,
642 };
643
644 static struct drm_driver driver = {
645         .driver_features = DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | \
646                            DRIVER_IRQ_VBL | DRIVER_MODESET | DRIVER_GEM ,
647         .load = psb_driver_load,
648         .unload = psb_driver_unload,
649
650         .ioctls = psb_ioctls,
651         .num_ioctls = DRM_ARRAY_SIZE(psb_ioctls),
652         .device_is_agp = psb_driver_device_is_agp,
653         .irq_preinstall = psb_irq_preinstall,
654         .irq_postinstall = psb_irq_postinstall,
655         .irq_uninstall = psb_irq_uninstall,
656         .irq_handler = psb_irq_handler,
657         .enable_vblank = psb_enable_vblank,
658         .disable_vblank = psb_disable_vblank,
659         .get_vblank_counter = psb_get_vblank_counter,
660         .lastclose = psb_lastclose,
661         .open = psb_driver_open,
662         .preclose = psb_driver_preclose,
663         .postclose = psb_driver_close,
664         .reclaim_buffers = drm_core_reclaim_buffers,
665
666         .gem_init_object = psb_gem_init_object,
667         .gem_free_object = psb_gem_free_object,
668         .gem_vm_ops = &psb_gem_vm_ops,
669         .dumb_create = psb_gem_dumb_create,
670         .dumb_map_offset = psb_gem_dumb_map_gtt,
671         .dumb_destroy = psb_gem_dumb_destroy,
672         .fops = &psb_gem_fops,
673         .name = DRIVER_NAME,
674         .desc = DRIVER_DESC,
675         .date = PSB_DRM_DRIVER_DATE,
676         .major = PSB_DRM_DRIVER_MAJOR,
677         .minor = PSB_DRM_DRIVER_MINOR,
678         .patchlevel = PSB_DRM_DRIVER_PATCHLEVEL
679 };
680
681 static struct pci_driver psb_pci_driver = {
682         .name = DRIVER_NAME,
683         .id_table = pciidlist,
684         .probe = psb_probe,
685         .remove = psb_remove,
686         .driver.pm = &psb_pm_ops,
687 };
688
689 static int psb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
690 {
691         return drm_get_pci_dev(pdev, ent, &driver);
692 }
693
694 static int __init psb_init(void)
695 {
696         return drm_pci_init(&driver, &psb_pci_driver);
697 }
698
699 static void __exit psb_exit(void)
700 {
701         drm_pci_exit(&driver, &psb_pci_driver);
702 }
703
704 late_initcall(psb_init);
705 module_exit(psb_exit);
706
707 MODULE_AUTHOR("Alan Cox <alan@linux.intel.com> and others");
708 MODULE_DESCRIPTION(DRIVER_DESC);
709 MODULE_LICENSE("GPL");