[android-sdk/device-ti-proprietary-open.git] / omap5 / sgx_src / eurasia_km / services4 / srvkm / env / linux / module.c
1 /*************************************************************************/ /*!
2 @Title Linux module setup
3 @Copyright Copyright (c) Imagination Technologies Ltd. All Rights Reserved
4 @License Dual MIT/GPLv2
6 The contents of this file are subject to the MIT license as set out below.
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
18 Alternatively, the contents of this file may be used under the terms of
19 the GNU General Public License Version 2 ("GPL") in which case the provisions
20 of GPL are applicable instead of those above.
22 If you wish to allow use of your version of this file only under the terms of
23 GPL, and not to allow others to use your version of this file under the terms
24 of the MIT license, indicate your decision by deleting the provisions above
25 and replace them with the notice and other provisions required by GPL as set
26 out in the file called "GPL-COPYING" included in this distribution. If you do
27 not delete the provisions above, a recipient may use your version of this file
28 under the terms of either the MIT license or GPL.
30 This License is also included in this distribution in the file called
31 "MIT-COPYING".
33 EXCEPT AS OTHERWISE STATED IN A NEGOTIATED AGREEMENT: (A) THE SOFTWARE IS
34 PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
35 BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
36 PURPOSE AND NONINFRINGEMENT; AND (B) IN NO EVENT SHALL THE AUTHORS OR
37 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
38 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
39 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 */ /**************************************************************************/
42 #include <linux/version.h>
44 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38))
45 #ifndef AUTOCONF_INCLUDED
46 #include <linux/config.h>
47 #endif
48 #endif
50 #if defined(SUPPORT_DRI_DRM) && !defined(SUPPORT_DRI_DRM_PLUGIN)
51 #define PVR_MOD_STATIC
52 #else
53 /*
54 * For LDM drivers, define PVR_LDM_MODULE to indicate generic LDM
55 * support is required, besides indicating the exact support
56 * required (e.g. platform, or PCI device).
57 */
58 #if defined(LDM_PLATFORM)
59 #define PVR_LDM_PLATFORM_MODULE
60 #define PVR_LDM_DEVICE_CLASS
61 #define PVR_LDM_MODULE
62 #else
63 #if defined(LDM_PCI)
64 #define PVR_LDM_DEVICE_CLASS
65 #define PVR_LDM_PCI_MODULE
66 #define PVR_LDM_MODULE
67 #else
68 #if defined(SYS_SHARES_WITH_3PKM)
69 #define PVR_LDM_DEVICE_CLASS
70 #endif
71 #endif
72 #endif
73 #define PVR_MOD_STATIC static
74 #endif
76 #if defined(PVR_LDM_PLATFORM_PRE_REGISTERED)
77 #if !defined(NO_HARDWARE)
78 #define PVR_USE_PRE_REGISTERED_PLATFORM_DEV
79 #endif
80 #endif
82 #include <linux/init.h>
83 #include <linux/kernel.h>
84 #include <linux/module.h>
85 #include <linux/fs.h>
86 #include <linux/proc_fs.h>
88 #if defined(SUPPORT_DRI_DRM)
89 #include <drm/drmP.h>
90 #if defined(PVR_SECURE_DRM_AUTH_EXPORT)
91 #include "env_perproc.h"
92 #endif
93 #endif
95 #if defined(PVR_LDM_PLATFORM_MODULE)
96 #include <linux/platform_device.h>
97 #endif /* PVR_LDM_PLATFORM_MODULE */
99 #if defined(PVR_LDM_PCI_MODULE)
100 #include <linux/pci.h>
101 #endif /* PVR_LDM_PCI_MODULE */
103 #if defined(PVR_LDM_DEVICE_CLASS)
104 #include <linux/device.h>
105 #endif /* PVR_LDM_DEVICE_CLASS */
107 #if defined(DEBUG) && defined(PVR_MANUAL_POWER_CONTROL)
108 #include <asm/uaccess.h>
109 #endif
111 #include "img_defs.h"
112 #include "services.h"
113 #include "kerneldisplay.h"
114 #include "kernelbuffer.h"
115 #include "syscommon.h"
116 #include "pvrmmap.h"
117 #include "mutils.h"
118 #include "mm.h"
119 #include "mmap.h"
120 #include "mutex.h"
121 #include "pvr_debug.h"
122 #include "srvkm.h"
123 #include "perproc.h"
124 #include "handle.h"
125 #include "pvr_bridge_km.h"
126 #include "proc.h"
127 #include "pvrmodule.h"
128 #include "private_data.h"
129 #include "lock.h"
130 #include "linkage.h"
131 #include "buffer_manager.h"
133 #if defined(SUPPORT_DRI_DRM)
134 #include "pvr_drm.h"
135 #endif
136 /*
137 * DRVNAME is the name we use to register our driver.
138 * DEVNAME is the name we use to register actual device nodes.
139 */
140 #if defined(PVR_LDM_MODULE)
141 #define DRVNAME PVR_LDM_DRIVER_REGISTRATION_NAME
142 #endif
143 #define DEVNAME PVRSRV_MODNAME
145 #if defined(SUPPORT_DRI_DRM)
146 #define PRIVATE_DATA(pFile) ((pFile)->driver_priv)
147 #else
148 #define PRIVATE_DATA(pFile) ((pFile)->private_data)
149 #endif
151 /*
152 * This is all module configuration stuff required by the linux kernel.
153 */
154 MODULE_SUPPORTED_DEVICE(DEVNAME);
156 #if defined(PVRSRV_NEED_PVR_DPF)
157 #include <linux/moduleparam.h>
158 extern IMG_UINT32 gPVRDebugLevel;
159 module_param(gPVRDebugLevel, uint, 0644);
160 MODULE_PARM_DESC(gPVRDebugLevel, "Sets the level of debug output (default 0x7)");
161 #endif /* defined(PVRSRV_NEED_PVR_DPF) */
163 #if defined(CONFIG_ION_OMAP)
164 #include <linux/ion.h>
165 #include <linux/omap_ion.h>
166 #include "ion.h"
167 extern void omap_ion_register_pvr_export(void *);
168 extern struct ion_device *omap_ion_device;
169 struct ion_client *gpsIONClient;
170 EXPORT_SYMBOL(gpsIONClient);
171 #endif /* defined(CONFIG_ION_OMAP) */
173 /* PRQA S 3207 2 */ /* ignore 'not used' warning */
174 EXPORT_SYMBOL(PVRGetDisplayClassJTable);
175 EXPORT_SYMBOL(PVRGetBufferClassJTable);
177 #if defined(PVR_LDM_DEVICE_CLASS) && !defined(SUPPORT_DRI_DRM)
178 /*
179 * Device class used for /sys entries (and udev device node creation)
180 */
181 static struct class *psPvrClass;
182 #endif
184 #if !defined(SUPPORT_DRI_DRM)
185 /*
186 * This is the major number we use for all nodes in /dev.
187 */
188 static int AssignedMajorNumber;
190 /*
191 * These are the operations that will be associated with the device node
192 * we create.
193 *
194 * With gcc -W, specifying only the non-null members produces "missing
195 * initializer" warnings.
196 */
197 static int PVRSRVOpen(struct inode* pInode, struct file* pFile);
198 static int PVRSRVRelease(struct inode* pInode, struct file* pFile);
200 static struct file_operations pvrsrv_fops =
201 {
202 .owner=THIS_MODULE,
203 .unlocked_ioctl = PVRSRV_BridgeDispatchKM,
204 .open=PVRSRVOpen,
205 .release=PVRSRVRelease,
206 .mmap=PVRMMap,
207 };
208 #endif
210 PVRSRV_LINUX_MUTEX gPVRSRVLock;
212 /* PID of process being released */
213 IMG_UINT32 gui32ReleasePID;
215 #if defined(DEBUG) && defined(PVR_MANUAL_POWER_CONTROL)
216 static IMG_UINT32 gPVRPowerLevel;
217 #endif
219 #if defined(PVR_LDM_MODULE)
221 #if defined(PVR_LDM_PLATFORM_MODULE)
222 #define LDM_DEV struct platform_device
223 #define LDM_DRV struct platform_driver
224 #endif /*PVR_LDM_PLATFORM_MODULE */
226 #if defined(PVR_LDM_PCI_MODULE)
227 #define LDM_DEV struct pci_dev
228 #define LDM_DRV struct pci_driver
229 #endif /* PVR_LDM_PCI_MODULE */
230 /*
231 * This is the driver interface we support.
232 */
233 #if defined(PVR_LDM_PLATFORM_MODULE)
234 static int PVRSRVDriverRemove(LDM_DEV *device);
235 static int PVRSRVDriverProbe(LDM_DEV *device);
236 #endif
237 #if defined(PVR_LDM_PCI_MODULE)
238 static void PVRSRVDriverRemove(LDM_DEV *device);
239 static int PVRSRVDriverProbe(LDM_DEV *device, const struct pci_device_id *id);
240 #endif
241 static int PVRSRVDriverSuspend(LDM_DEV *device, pm_message_t state);
242 static void PVRSRVDriverShutdown(LDM_DEV *device);
243 static int PVRSRVDriverResume(LDM_DEV *device);
245 #if defined(PVR_LDM_PCI_MODULE)
246 /* This structure is used by the Linux module code */
247 struct pci_device_id powervr_id_table[] __devinitdata = {
248 {PCI_DEVICE(SYS_SGX_DEV_VENDOR_ID, SYS_SGX_DEV_DEVICE_ID)},
249 #if defined (SYS_SGX_DEV1_DEVICE_ID)
250 {PCI_DEVICE(SYS_SGX_DEV_VENDOR_ID, SYS_SGX_DEV1_DEVICE_ID)},
251 #endif
252 {0}
253 };
255 MODULE_DEVICE_TABLE(pci, powervr_id_table);
256 #endif
258 #if defined(PVR_USE_PRE_REGISTERED_PLATFORM_DEV)
259 static struct platform_device_id powervr_id_table[] __devinitdata = {
260 {SYS_SGX_DEV_NAME, 0},
261 {}
262 };
263 #endif
265 static LDM_DRV powervr_driver = {
266 #if defined(PVR_LDM_PLATFORM_MODULE)
267 .driver = {
268 .name = DRVNAME,
269 },
270 #endif
271 #if defined(PVR_LDM_PCI_MODULE)
272 .name = DRVNAME,
273 #endif
274 #if defined(PVR_LDM_PCI_MODULE) || defined(PVR_USE_PRE_REGISTERED_PLATFORM_DEV)
275 .id_table = powervr_id_table,
276 #endif
277 .probe = PVRSRVDriverProbe,
278 #if defined(PVR_LDM_PLATFORM_MODULE)
279 .remove = PVRSRVDriverRemove,
280 #endif
281 #if defined(PVR_LDM_PCI_MODULE)
282 .remove = __devexit_p(PVRSRVDriverRemove),
283 #endif
284 .suspend = PVRSRVDriverSuspend,
285 .resume = PVRSRVDriverResume,
286 .shutdown = PVRSRVDriverShutdown,
287 };
289 LDM_DEV *gpsPVRLDMDev;
291 #if defined(MODULE) && defined(PVR_LDM_PLATFORM_MODULE) && \
292 !defined(PVR_USE_PRE_REGISTERED_PLATFORM_DEV)
293 static void PVRSRVDeviceRelease(struct device unref__ *pDevice)
294 {
295 }
297 static struct platform_device powervr_device = {
298 .name = DEVNAME,
299 .id = -1,
300 .dev = {
301 .release = PVRSRVDeviceRelease
302 }
303 };
304 #endif
306 /*!
307 ******************************************************************************
309 @Function PVRSRVDriverProbe
311 @Description
313 See whether a given device is really one we can drive. The platform bus
314 handler has already established that we should be able to service this device
315 because of the name match. We probably don't need to do anything else.
317 @input pDevice - the device for which a probe is requested
319 @Return 0 for success or <0 for an error.
321 *****************************************************************************/
322 #if defined(PVR_LDM_PLATFORM_MODULE)
323 static int PVRSRVDriverProbe(LDM_DEV *pDevice)
324 #endif
325 #if defined(PVR_LDM_PCI_MODULE)
326 static int __devinit PVRSRVDriverProbe(LDM_DEV *pDevice, const struct pci_device_id *id)
327 #endif
328 {
329 SYS_DATA *psSysData;
331 PVR_TRACE(("PVRSRVDriverProbe(pDevice=%p)", pDevice));
333 #if 0 /* INTEGRATION_POINT */
334 /* Some systems require device-specific system initialisation.
335 * E.g. this lets the OS track a device's dependencies on various
336 * system hardware.
337 *
338 * Note: some systems use this to enable HW that SysAcquireData
339 * will depend on, therefore it must be called first.
340 */
341 if (PerDeviceSysInitialise((IMG_PVOID)pDevice) != PVRSRV_OK)
342 {
343 return -EINVAL;
344 }
345 #endif
346 /* SysInitialise only designed to be called once.
347 */
348 psSysData = SysAcquireDataNoCheck();
349 if (psSysData == IMG_NULL)
350 {
351 gpsPVRLDMDev = pDevice;
352 if (SysInitialise() != PVRSRV_OK)
353 {
354 return -ENODEV;
355 }
356 }
358 #if defined(CONFIG_ION_OMAP)
359 gpsIONClient = ion_client_create(omap_ion_device,
360 1 << ION_HEAP_TYPE_CARVEOUT |
361 1 << OMAP_ION_HEAP_TYPE_TILER |
362 1 << ION_HEAP_TYPE_SYSTEM,
363 "pvr");
364 if (IS_ERR_OR_NULL(gpsIONClient))
365 {
366 PVR_DPF((PVR_DBG_ERROR, "PVRSRVDriverProbe: Couldn't create ion client"));
367 return PTR_ERR(gpsIONClient);
368 }
369 omap_ion_register_pvr_export(&PVRSRVExportFDToIONHandles);
370 #endif /* defined(CONFIG_ION_OMAP) */
372 return 0;
373 }
376 /*!
377 ******************************************************************************
379 @Function PVRSRVDriverRemove
381 @Description
383 This call is the opposite of the probe call: it is called when the device is
384 being removed from the driver's control. See the file $KERNELDIR/drivers/
385 base/bus.c:device_release_driver() for the call to this function.
387 This is the correct place to clean up anything our driver did while it was
388 asoociated with the device.
390 @input pDevice - the device for which driver detachment is happening
392 @Return 0 for success or <0 for an error.
394 *****************************************************************************/
395 #if defined (PVR_LDM_PLATFORM_MODULE)
396 static int PVRSRVDriverRemove(LDM_DEV *pDevice)
397 #endif
398 #if defined(PVR_LDM_PCI_MODULE)
399 static void __devexit PVRSRVDriverRemove(LDM_DEV *pDevice)
400 #endif
401 {
402 SYS_DATA *psSysData;
404 PVR_TRACE(("PVRSRVDriverRemove(pDevice=%p)", pDevice));
406 #if defined(CONFIG_ION_OMAP)
407 ion_client_destroy(gpsIONClient);
408 gpsIONClient = IMG_NULL;
409 #endif
411 SysAcquireData(&psSysData);
413 #if defined(DEBUG) && defined(PVR_MANUAL_POWER_CONTROL)
414 if (gPVRPowerLevel != 0)
415 {
416 if (PVRSRVSetPowerStateKM(PVRSRV_SYS_POWER_STATE_D0) == PVRSRV_OK)
417 {
418 gPVRPowerLevel = 0;
419 }
420 }
421 #endif
422 (void) SysDeinitialise(psSysData);
424 gpsPVRLDMDev = IMG_NULL;
426 #if 0 /* INTEGRATION_POINT */
427 /* See previous integration point for details. */
428 if (PerDeviceSysDeInitialise((IMG_PVOID)pDevice) != PVRSRV_OK)
429 {
430 return -EINVAL;
431 }
432 #endif
434 #if defined (PVR_LDM_PLATFORM_MODULE)
435 return 0;
436 #endif
437 #if defined (PVR_LDM_PCI_MODULE)
438 return;
439 #endif
440 }
441 #endif /* defined(PVR_LDM_MODULE) */
444 #if defined(PVR_LDM_MODULE) || defined(SUPPORT_DRI_DRM)
445 static PVRSRV_LINUX_MUTEX gsPMMutex;
446 static IMG_BOOL bDriverIsSuspended;
447 static IMG_BOOL bDriverIsShutdown;
448 #endif
450 #if defined(PVR_LDM_MODULE) || defined(PVR_DRI_DRM_PLATFORM_DEV)
451 /*!
452 ******************************************************************************
454 @Function PVRSRVDriverShutdown
456 @Description
458 Suspend device operation for system shutdown. This is called as part of the
459 system halt/reboot process. The driver is put into a quiescent state by
460 setting the power state to D3.
462 @input pDevice - the device for which shutdown is requested
464 @Return nothing
466 *****************************************************************************/
467 #if defined(SUPPORT_DRI_DRM) && !defined(PVR_DRI_DRM_PLATFORM_DEV) && \
468 !defined(SUPPORT_DRI_DRM_PLUGIN)
469 void PVRSRVDriverShutdown(struct drm_device *pDevice)
470 #else
471 PVR_MOD_STATIC void PVRSRVDriverShutdown(LDM_DEV *pDevice)
472 #endif
473 {
474 PVR_TRACE(("PVRSRVDriverShutdown(pDevice=%p)", pDevice));
476 LinuxLockMutex(&gsPMMutex);
478 if (!bDriverIsShutdown && !bDriverIsSuspended)
479 {
480 /*
481 * Take the bridge mutex, and never release it, to stop
482 * processes trying to use the driver after it has been
483 * shutdown.
484 */
485 LinuxLockMutex(&gPVRSRVLock);
487 (void) PVRSRVSetPowerStateKM(PVRSRV_SYS_POWER_STATE_D3);
488 }
490 bDriverIsShutdown = IMG_TRUE;
492 /* The bridge mutex is held on exit */
493 LinuxUnLockMutex(&gsPMMutex);
494 }
496 #endif /* defined(PVR_LDM_MODULE) || defined(PVR_DRI_DRM_PLATFORM_DEV) */
499 #if defined(PVR_LDM_MODULE) || defined(SUPPORT_DRI_DRM)
500 /*!
501 ******************************************************************************
503 @Function PVRSRVDriverSuspend
505 @Description
507 For 2.6 kernels:
508 Suspend device operation. We always get three calls to this regardless of
509 the state (D1-D3) chosen. The order is SUSPEND_DISABLE, SUSPEND_SAVE_STATE
510 then SUSPEND_POWER_DOWN. We take action as soon as we get the disable call,
511 the other states not being handled by us yet.
513 For MontaVista 2.4 kernels:
514 This call gets made once only when someone does something like
516 # echo -e -n "suspend powerdown 0" >/sys.devices/legacy/pvrsrv0/power
518 The 3rd, numeric parameter (0) in the above has no relevence and is not
519 passed into us. The state parameter is always zero and the level parameter
520 is always SUSPEND_POWER_DOWN. Vive la difference!
522 @input pDevice - the device for which resume is requested
524 @Return 0 for success or <0 for an error.
526 *****************************************************************************/
527 #if defined(SUPPORT_DRI_DRM) && !defined(PVR_DRI_DRM_PLATFORM_DEV) && \
528 !defined(SUPPORT_DRI_DRM_PLUGIN)
529 #if defined(SUPPORT_DRM_MODESET)
530 int PVRSRVDriverSuspend(struct pci_dev *pDevice, pm_message_t state)
531 #else
532 int PVRSRVDriverSuspend(struct drm_device *pDevice, pm_message_t state)
533 #endif
534 #else
535 PVR_MOD_STATIC int PVRSRVDriverSuspend(LDM_DEV *pDevice, pm_message_t state)
536 #endif
537 {
538 int res = 0;
539 #if !(defined(DEBUG) && defined(PVR_MANUAL_POWER_CONTROL) && !defined(SUPPORT_DRI_DRM))
540 PVR_TRACE(( "PVRSRVDriverSuspend(pDevice=%p)", pDevice));
542 LinuxLockMutex(&gsPMMutex);
544 if (!bDriverIsSuspended && !bDriverIsShutdown)
545 {
546 LinuxLockMutex(&gPVRSRVLock);
548 if (PVRSRVSetPowerStateKM(PVRSRV_SYS_POWER_STATE_D3) == PVRSRV_OK)
549 {
550 /* The bridge mutex will be held until we resume */
551 bDriverIsSuspended = IMG_TRUE;
552 }
553 else
554 {
555 LinuxUnLockMutex(&gPVRSRVLock);
556 res = -EINVAL;
557 }
558 }
560 LinuxUnLockMutex(&gsPMMutex);
561 #endif
562 return res;
563 }
566 /*!
567 ******************************************************************************
569 @Function PVRSRVDriverResume
571 @Description
573 Resume device operation following a lull due to earlier suspension. It is
574 implicit we're returning to D0 (fully operational) state. We always get three
575 calls to this using level thus: RESUME_POWER_ON, RESUME_RESTORE_STATE then
576 RESUME_ENABLE. On 2.6 kernels We don't do anything until we get the enable
577 call; on the MontaVista set-up we only ever get the RESUME_POWER_ON call.
579 @input pDevice - the device for which resume is requested
581 @Return 0 for success or <0 for an error.
583 *****************************************************************************/
584 #if defined(SUPPORT_DRI_DRM) && !defined(PVR_DRI_DRM_PLATFORM_DEV) && \
585 !defined(SUPPORT_DRI_DRM_PLUGIN)
586 #if defined(SUPPORT_DRM_MODESET)
587 int PVRSRVDriverResume(struct pci_dev *pDevice)
588 #else
589 int PVRSRVDriverResume(struct drm_device *pDevice)
590 #endif
591 #else
592 PVR_MOD_STATIC int PVRSRVDriverResume(LDM_DEV *pDevice)
593 #endif
594 {
595 int res = 0;
596 #if !(defined(DEBUG) && defined(PVR_MANUAL_POWER_CONTROL) && !defined(SUPPORT_DRI_DRM))
597 PVR_TRACE(("PVRSRVDriverResume(pDevice=%p)", pDevice));
599 LinuxLockMutex(&gsPMMutex);
601 if (bDriverIsSuspended && !bDriverIsShutdown)
602 {
603 if (PVRSRVSetPowerStateKM(PVRSRV_SYS_POWER_STATE_D0) == PVRSRV_OK)
604 {
605 bDriverIsSuspended = IMG_FALSE;
606 LinuxUnLockMutex(&gPVRSRVLock);
607 }
608 else
609 {
610 /* The bridge mutex is not released on failure */
611 res = -EINVAL;
612 }
613 }
615 LinuxUnLockMutex(&gsPMMutex);
616 #endif
617 return res;
618 }
619 #endif /* defined(PVR_LDM_MODULE) || defined(SUPPORT_DRI_DRM) */
622 #if defined(DEBUG) && defined(PVR_MANUAL_POWER_CONTROL) && !defined(SUPPORT_DRI_DRM)
623 /*
624 * If PVR_LDM_PCI_MODULE is defined (and PVR_MANUAL_POWER_CONTROL is *NOT* defined),
625 * the device can be suspended and resumed without suspending/resuming the
626 * system, by writing values into the power/state sysfs file for the device.
627 * To suspend:
628 * echo -n 2 > power/state
629 * To Resume:
630 * echo -n 0 > power/state
631 *
632 * The problem with this approach is that the device is usually left
633 * powered up; it is the responsibility of the bus driver to remove
634 * the power.
635 *
636 * Defining PVR_MANUAL_POWER_CONTROL is intended to make it easier to
637 * debug power management issues, especially when power is really removed
638 * from the device. It is easier to debug the driver if it is not being
639 * suspended/resumed with the rest of the system.
640 *
641 * When PVR_MANUAL_POWER_CONTROL is defined, the following proc entry is
642 * created:
643 * /proc/pvr/power_control
644 * The driver suspend/resume entry points defined below no longer suspend or
645 * resume the device. To suspend the device, type the following:
646 * echo 2 > /proc/pvr/power_control
647 * To resume the device, type:
648 * echo 0 > /proc/pvr/power_control
649 *
650 * The following example shows how to suspend/resume the device independently
651 * of the rest of the system.
652 * Suspend the device:
653 * echo 2 > /proc/pvr/power_control
654 * Suspend the system. Then you should be able to suspend and resume
655 * as normal. To resume the device type the following:
656 * echo 0 > /proc/pvr/power_control
657 */
659 IMG_INT PVRProcSetPowerLevel(struct file *file, const IMG_CHAR *buffer, IMG_UINT32 count, IMG_VOID *data)
660 {
661 IMG_CHAR data_buffer[2];
662 IMG_UINT32 PVRPowerLevel;
664 if (count != sizeof(data_buffer))
665 {
666 return -EINVAL;
667 }
668 else
669 {
670 if (copy_from_user(data_buffer, buffer, count))
671 return -EINVAL;
672 if (data_buffer[count - 1] != '\n')
673 return -EINVAL;
674 PVRPowerLevel = data_buffer[0] - '0';
675 if (PVRPowerLevel != gPVRPowerLevel)
676 {
677 if (PVRPowerLevel != 0)
678 {
679 if (PVRSRVSetPowerStateKM(PVRSRV_SYS_POWER_STATE_D3) != PVRSRV_OK)
680 {
681 return -EINVAL;
682 }
683 }
684 else
685 {
686 if (PVRSRVSetPowerStateKM(PVRSRV_SYS_POWER_STATE_D0) != PVRSRV_OK)
687 {
688 return -EINVAL;
689 }
690 }
692 gPVRPowerLevel = PVRPowerLevel;
693 }
694 }
695 return (count);
696 }
698 void ProcSeqShowPowerLevel(struct seq_file *sfile,void* el)
699 {
700 seq_printf(sfile, "%lu\n", gPVRPowerLevel);
701 }
703 #endif
705 /*!
706 ******************************************************************************
708 @Function PVRSRVOpen
710 @Description
712 Release access the PVR services node - called when a file is closed, whether
713 at exit or using close(2) system call.
715 @input pInode - the inode for the file being openeded
717 @input pFile - the file handle data for the actual file being opened
719 @Return 0 for success or <0 for an error.
721 *****************************************************************************/
722 #if defined(SUPPORT_DRI_DRM)
723 int PVRSRVOpen(struct drm_device unref__ *dev, struct drm_file *pFile)
724 #else
725 static int PVRSRVOpen(struct inode unref__ * pInode, struct file *pFile)
726 #endif
727 {
728 PVRSRV_FILE_PRIVATE_DATA *psPrivateData;
729 IMG_HANDLE hBlockAlloc;
730 int iRet = -ENOMEM;
731 PVRSRV_ERROR eError;
732 IMG_UINT32 ui32PID;
733 #if defined(SUPPORT_DRI_DRM) && defined(PVR_SECURE_DRM_AUTH_EXPORT)
734 PVRSRV_ENV_PER_PROCESS_DATA *psEnvPerProc;
735 #endif
737 LinuxLockMutex(&gPVRSRVLock);
739 ui32PID = OSGetCurrentProcessIDKM();
741 if (PVRSRVProcessConnect(ui32PID, 0) != PVRSRV_OK)
742 goto err_unlock;
744 #if defined(SUPPORT_DRI_DRM) && defined(PVR_SECURE_DRM_AUTH_EXPORT)
745 psEnvPerProc = PVRSRVPerProcessPrivateData(ui32PID);
746 if (psEnvPerProc == IMG_NULL)
747 {
748 PVR_DPF((PVR_DBG_ERROR, "%s: No per-process private data", __FUNCTION__));
749 goto err_unlock;
750 }
751 #endif
753 eError = OSAllocMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
754 sizeof(PVRSRV_FILE_PRIVATE_DATA),
755 (IMG_PVOID *)&psPrivateData,
756 &hBlockAlloc,
757 "File Private Data");
759 if(eError != PVRSRV_OK)
760 goto err_unlock;
762 #if defined (SUPPORT_SID_INTERFACE)
763 psPrivateData->hKernelMemInfo = 0;
764 #else
765 psPrivateData->hKernelMemInfo = NULL;
766 #endif
767 #if defined(SUPPORT_DRI_DRM) && defined(PVR_SECURE_DRM_AUTH_EXPORT)
768 psPrivateData->psDRMFile = pFile;
770 list_add_tail(&psPrivateData->sDRMAuthListItem, &psEnvPerProc->sDRMAuthListHead);
771 #endif
772 psPrivateData->ui32OpenPID = ui32PID;
773 psPrivateData->hBlockAlloc = hBlockAlloc;
774 PRIVATE_DATA(pFile) = psPrivateData;
775 iRet = 0;
776 err_unlock:
777 LinuxUnLockMutex(&gPVRSRVLock);
778 return iRet;
779 }
782 /*!
783 ******************************************************************************
785 @Function PVRSRVRelease
787 @Description
789 Release access the PVR services node - called when a file is closed, whether
790 at exit or using close(2) system call.
792 @input pInode - the inode for the file being released
794 @input pFile - the file handle data for the actual file being released
796 @Return 0 for success or <0 for an error.
798 *****************************************************************************/
799 #if defined(SUPPORT_DRI_DRM)
800 void PVRSRVRelease(void *pvPrivData)
801 #else
802 static int PVRSRVRelease(struct inode unref__ * pInode, struct file *pFile)
803 #endif
804 {
805 PVRSRV_FILE_PRIVATE_DATA *psPrivateData;
806 int err = 0;
808 LinuxLockMutex(&gPVRSRVLock);
810 #if defined(SUPPORT_DRI_DRM)
811 psPrivateData = (PVRSRV_FILE_PRIVATE_DATA *)pvPrivData;
812 #else
813 psPrivateData = PRIVATE_DATA(pFile);
814 #endif
815 if (psPrivateData != IMG_NULL)
816 {
817 #if defined(SUPPORT_DRI_DRM) && defined(PVR_SECURE_DRM_AUTH_EXPORT)
818 list_del(&psPrivateData->sDRMAuthListItem);
819 #endif
821 if(psPrivateData->hKernelMemInfo)
822 {
823 PVRSRV_KERNEL_MEM_INFO *psKernelMemInfo;
825 /* Look up the meminfo we just exported */
826 if(PVRSRVLookupHandle(KERNEL_HANDLE_BASE,
827 (IMG_PVOID *)&psKernelMemInfo,
828 psPrivateData->hKernelMemInfo,
829 PVRSRV_HANDLE_TYPE_MEM_INFO) != PVRSRV_OK)
830 {
831 PVR_DPF((PVR_DBG_ERROR, "%s: Failed to look up export handle", __FUNCTION__));
832 err = -EFAULT;
833 goto err_unlock;
834 }
836 /* Tell the XProc about the export if required */
837 if (psKernelMemInfo->sShareMemWorkaround.bInUse)
838 {
839 BM_XProcIndexRelease(psKernelMemInfo->sShareMemWorkaround.ui32ShareIndex);
840 }
842 /* This drops the psMemInfo refcount bumped on export */
843 if(FreeMemCallBackCommon(psKernelMemInfo, 0,
844 PVRSRV_FREE_CALLBACK_ORIGIN_EXTERNAL) != PVRSRV_OK)
845 {
846 PVR_DPF((PVR_DBG_ERROR, "%s: FreeMemCallBackCommon failed", __FUNCTION__));
847 err = -EFAULT;
848 goto err_unlock;
849 }
850 }
852 /* Usually this is the same as OSGetCurrentProcessIDKM(),
853 * but not necessarily (e.g. fork(), child closes last..)
854 */
855 gui32ReleasePID = psPrivateData->ui32OpenPID;
856 PVRSRVProcessDisconnect(psPrivateData->ui32OpenPID);
857 gui32ReleasePID = 0;
859 OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
860 sizeof(PVRSRV_FILE_PRIVATE_DATA),
861 psPrivateData, psPrivateData->hBlockAlloc);
863 #if !defined(SUPPORT_DRI_DRM)
864 PRIVATE_DATA(pFile) = IMG_NULL; /*nulling shared pointer*/
865 #endif
866 }
868 err_unlock:
869 LinuxUnLockMutex(&gPVRSRVLock);
870 #if defined(SUPPORT_DRI_DRM)
871 return;
872 #else
873 return err;
874 #endif
875 }
878 /*!
879 ******************************************************************************
881 @Function PVRCore_Init
883 @Description
885 Insert the driver into the kernel.
887 The device major number is allocated by the kernel dynamically. This means
888 that the device node (nominally /dev/pvrsrv) will need to be re-made at boot
889 time if the number changes between subsequent loads of the module. While the
890 number often stays constant between loads this is not guaranteed. The node
891 is made as root on the shell with:
893 mknod /dev/pvrsrv c nnn 0
895 where nnn is the major number found in /proc/devices for DEVNAME and also
896 reported by the PVR_DPF() - look at the boot log using dmesg' to see this).
898 Currently the auto-generated script /etc/init.d/rc.pvr handles creation of
899 the device. In other environments the device may be created either through
900 devfs or sysfs.
902 Readable proc-filesystem entries under /proc/pvr are created with
903 CreateProcEntries(). These can be read at runtime to get information about
904 the device (eg. 'cat /proc/pvr/vm')
906 __init places the function in a special memory section that the kernel frees
907 once the function has been run. Refer also to module_init() macro call below.
909 @input none
911 @Return none
913 *****************************************************************************/
914 #if defined(SUPPORT_DRI_DRM)
915 int PVRCore_Init(void)
916 #else
917 static int __init PVRCore_Init(void)
918 #endif
919 {
920 int error;
921 #if !defined(PVR_LDM_MODULE)
922 PVRSRV_ERROR eError;
923 #endif
924 #if !defined(SUPPORT_DRI_DRM) && defined(PVR_LDM_DEVICE_CLASS)
925 struct device *psDev;
926 #endif
928 #if !defined(SUPPORT_DRI_DRM)
929 /*
930 * Must come before attempting to print anything via Services.
931 * For DRM, the initialisation will already have been done.
932 */
933 PVRDPFInit();
934 #endif
935 PVR_TRACE(("PVRCore_Init"));
937 #if defined(PVR_LDM_MODULE) || defined(SUPPORT_DRI_DRM)
938 LinuxInitMutex(&gsPMMutex);
939 #endif
940 LinuxInitMutex(&gPVRSRVLock);
942 if (CreateProcEntries ())
943 {
944 error = -ENOMEM;
945 return error;
946 }
948 if (PVROSFuncInit() != PVRSRV_OK)
949 {
950 error = -ENOMEM;
951 goto init_failed;
952 }
954 PVRLinuxMUtilsInit();
956 if(LinuxMMInit() != PVRSRV_OK)
957 {
958 error = -ENOMEM;
959 goto init_failed;
960 }
962 LinuxBridgeInit();
964 PVRMMapInit();
966 #if defined(PVR_LDM_MODULE)
968 #if defined(PVR_LDM_PLATFORM_MODULE) || defined(SUPPORT_DRI_DRM_PLUGIN)
969 if ((error = platform_driver_register(&powervr_driver)) != 0)
970 {
971 PVR_DPF((PVR_DBG_ERROR, "PVRCore_Init: unable to register platform driver (%d)", error));
973 goto init_failed;
974 }
976 #if defined(MODULE) && !defined(PVR_USE_PRE_REGISTERED_PLATFORM_DEV)
977 if ((error = platform_device_register(&powervr_device)) != 0)
978 {
979 platform_driver_unregister(&powervr_driver);
981 PVR_DPF((PVR_DBG_ERROR, "PVRCore_Init: unable to register platform device (%d)", error));
983 goto init_failed;
984 }
985 #endif
986 #endif /* PVR_LDM_PLATFORM_MODULE */
988 #if defined(PVR_LDM_PCI_MODULE)
989 if ((error = pci_register_driver(&powervr_driver)) != 0)
990 {
991 PVR_DPF((PVR_DBG_ERROR, "PVRCore_Init: unable to register PCI driver (%d)", error));
993 goto init_failed;
994 }
995 #endif /* PVR_LDM_PCI_MODULE */
996 #endif /* defined(PVR_LDM_MODULE) */
998 #if !defined(PVR_LDM_MODULE)
999 /*
1000 * Drivers using LDM, will call SysInitialise in the probe/attach code
1001 */
1002 if ((eError = SysInitialise()) != PVRSRV_OK)
1003 {
1004 error = -ENODEV;
1005 #if defined(TCF_REV) && (TCF_REV == 110)
1006 if(eError == PVRSRV_ERROR_NOT_SUPPORTED)
1007 {
1008 printk("\nAtlas wrapper (FPGA image) version mismatch");
1009 error = -ENODEV;
1010 }
1011 #endif
1012 goto init_failed;
1013 }
1014 #endif /* !defined(PVR_LDM_MODULE) */
1016 #if !defined(SUPPORT_DRI_DRM)
1017 AssignedMajorNumber = register_chrdev(0, DEVNAME, &pvrsrv_fops);
1019 if (AssignedMajorNumber <= 0)
1020 {
1021 PVR_DPF((PVR_DBG_ERROR, "PVRCore_Init: unable to get major number"));
1023 error = -EBUSY;
1024 goto sys_deinit;
1025 }
1027 PVR_TRACE(("PVRCore_Init: major device %d", AssignedMajorNumber));
1029 #if defined(PVR_LDM_DEVICE_CLASS)
1030 /*
1031 * This code (using GPL symbols) facilitates automatic device
1032 * node creation on platforms with udev (or similar).
1033 */
1034 psPvrClass = class_create(THIS_MODULE, "pvr");
1036 if (IS_ERR(psPvrClass))
1037 {
1038 PVR_DPF((PVR_DBG_ERROR, "PVRCore_Init: unable to create class (%ld)", PTR_ERR(psPvrClass)));
1039 error = -EBUSY;
1040 goto unregister_device;
1041 }
1043 psDev = device_create(psPvrClass, NULL, MKDEV(AssignedMajorNumber, 0),
1044 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26))
1045 NULL,
1046 #endif /* (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)) */
1047 DEVNAME);
1048 if (IS_ERR(psDev))
1049 {
1050 PVR_DPF((PVR_DBG_ERROR, "PVRCore_Init: unable to create device (%ld)", PTR_ERR(psDev)));
1051 error = -EBUSY;
1052 goto destroy_class;
1053 }
1054 #endif /* defined(PVR_LDM_DEVICE_CLASS) */
1055 #endif /* !defined(SUPPORT_DRI_DRM) */
1057 return 0;
1059 #if !defined(SUPPORT_DRI_DRM)
1060 #if defined(PVR_LDM_DEVICE_CLASS)
1061 destroy_class:
1062 class_destroy(psPvrClass);
1063 unregister_device:
1064 unregister_chrdev((IMG_UINT)AssignedMajorNumber, DEVNAME);
1065 #endif
1066 sys_deinit:
1067 #endif
1068 #if defined(PVR_LDM_MODULE)
1069 #if defined(PVR_LDM_PCI_MODULE)
1070 pci_unregister_driver(&powervr_driver);
1071 #endif
1073 #if defined (PVR_LDM_PLATFORM_MODULE)
1074 #if defined(MODULE) && !defined(PVR_USE_PRE_REGISTERED_PLATFORM_DEV)
1075 platform_device_unregister(&powervr_device);
1076 #endif
1077 platform_driver_unregister(&powervr_driver);
1078 #endif
1080 #else /* defined(PVR_LDM_MODULE) */
1081 /* LDM drivers call SysDeinitialise during PVRSRVDriverRemove */
1082 {
1083 SYS_DATA *psSysData;
1085 psSysData = SysAcquireDataNoCheck();
1086 if (psSysData != IMG_NULL)
1087 {
1088 (void) SysDeinitialise(psSysData);
1089 }
1090 }
1091 #endif /* defined(PVR_LDM_MODULE) */
1092 init_failed:
1093 PVRMMapCleanup();
1094 LinuxMMCleanup();
1095 LinuxBridgeDeInit();
1096 PVROSFuncDeInit();
1097 RemoveProcEntries();
1099 return error;
1101 } /*PVRCore_Init*/
1104 /*!
1105 *****************************************************************************
1107 @Function PVRCore_Cleanup
1109 @Description
1111 Remove the driver from the kernel.
1113 There's no way we can get out of being unloaded other than panicking; we
1114 just do everything and plough on regardless of error.
1116 __exit places the function in a special memory section that the kernel frees
1117 once the function has been run. Refer also to module_exit() macro call below.
1119 Note that the for LDM on MontaVista kernels, the positioning of the driver
1120 de-registration is the opposite way around than would be suggested by the
1121 registration case or the 2,6 kernel case. This is the correct way to do it
1122 and the kernel panics if you change it. You have been warned.
1124 @input none
1126 @Return none
1128 *****************************************************************************/
1129 #if defined(SUPPORT_DRI_DRM)
1130 void PVRCore_Cleanup(void)
1131 #else
1132 static void __exit PVRCore_Cleanup(void)
1133 #endif
1134 {
1135 #if !defined(PVR_LDM_MODULE)
1136 SYS_DATA *psSysData;
1137 #endif
1138 PVR_TRACE(("PVRCore_Cleanup"));
1140 #if !defined(PVR_LDM_MODULE)
1141 SysAcquireData(&psSysData);
1142 #endif
1144 #if !defined(SUPPORT_DRI_DRM)
1146 #if defined(PVR_LDM_DEVICE_CLASS)
1147 device_destroy(psPvrClass, MKDEV(AssignedMajorNumber, 0));
1148 class_destroy(psPvrClass);
1149 #endif
1151 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,22))
1152 if (
1153 #endif /* (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,22)) */
1154 unregister_chrdev((IMG_UINT)AssignedMajorNumber, DEVNAME)
1155 #if !(LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,22))
1156 ;
1157 #else /* (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,22)) */
1158 )
1159 {
1160 PVR_DPF((PVR_DBG_ERROR," can't unregister device major %d", AssignedMajorNumber));
1161 }
1162 #endif /* (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,22)) */
1163 #endif /* !defined(SUPPORT_DRI_DRM) */
1165 #if defined(PVR_LDM_MODULE)
1167 #if defined(PVR_LDM_PCI_MODULE)
1168 pci_unregister_driver(&powervr_driver);
1169 #endif
1171 #if defined (PVR_LDM_PLATFORM_MODULE)
1172 #if defined(MODULE) && !defined(PVR_USE_PRE_REGISTERED_PLATFORM_DEV)
1173 platform_device_unregister(&powervr_device);
1174 #endif
1175 platform_driver_unregister(&powervr_driver);
1176 #endif
1178 #else /* defined(PVR_LDM_MODULE) */
1179 #if defined(DEBUG) && defined(PVR_MANUAL_POWER_CONTROL)
1180 if (gPVRPowerLevel != 0)
1181 {
1182 if (PVRSRVSetPowerStateKM(PVRSRV_SYS_POWER_STATE_D0) == PVRSRV_OK)
1183 {
1184 gPVRPowerLevel = 0;
1185 }
1186 }
1187 #endif
1188 /* LDM drivers call SysDeinitialise during PVRSRVDriverRemove */
1189 (void) SysDeinitialise(psSysData);
1190 #endif /* defined(PVR_LDM_MODULE) */
1192 PVRMMapCleanup();
1194 LinuxMMCleanup();
1196 LinuxBridgeDeInit();
1198 PVROSFuncDeInit();
1200 RemoveProcEntries();
1202 PVR_TRACE(("PVRCore_Cleanup: unloading"));
1203 }
1205 /*
1206 * These macro calls define the initialisation and removal functions of the
1207 * driver. Although they are prefixed `module_', they apply when compiling
1208 * statically as well; in both cases they define the function the kernel will
1209 * run to start/stop the driver.
1210 */
1211 #if !defined(SUPPORT_DRI_DRM)
1212 module_init(PVRCore_Init);
1213 module_exit(PVRCore_Cleanup);
1214 #endif