62c8f2eb256cc2dc871c3e25f8118df882e87a7c
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / rogue_m / services / server / env / linux / module.c
1 /*************************************************************************/ /*!
2 @File
3 @Title          Linux module setup
4 @Copyright      Copyright (c) Imagination Technologies Ltd. All Rights Reserved
5 @License        Dual MIT/GPLv2
6
7 The contents of this file are subject to the MIT license as set out below.
8
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
19 Alternatively, the contents of this file may be used under the terms of
20 the GNU General Public License Version 2 ("GPL") in which case the provisions
21 of GPL are applicable instead of those above.
22
23 If you wish to allow use of your version of this file only under the terms of
24 GPL, and not to allow others to use your version of this file under the terms
25 of the MIT license, indicate your decision by deleting the provisions above
26 and replace them with the notice and other provisions required by GPL as set
27 out in the file called "GPL-COPYING" included in this distribution. If you do
28 not delete the provisions above, a recipient may use your version of this file
29 under the terms of either the MIT license or GPL.
30
31 This License is also included in this distribution in the file called
32 "MIT-COPYING".
33
34 EXCEPT AS OTHERWISE STATED IN A NEGOTIATED AGREEMENT: (A) THE SOFTWARE IS
35 PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
36 BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
37 PURPOSE AND NONINFRINGEMENT; AND (B) IN NO EVENT SHALL THE AUTHORS OR
38 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
39 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
40 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41 */ /**************************************************************************/
42
43 #include <linux/version.h>
44
45 #if defined(PVR_LDM_PLATFORM_PRE_REGISTERED)
46 #define PVR_USE_PRE_REGISTERED_PLATFORM_DEV
47 #endif
48
49 #include <linux/module.h>
50 #include <linux/device.h>
51
52 #include <linux/fs.h>
53
54 #if defined(LDM_PLATFORM)
55 #include <linux/dma-mapping.h>
56 #endif
57
58 #include "pvr_debug.h"
59 #include "srvkm.h"
60 #include "pvrmodule.h"
61 #include "linkage.h"
62 #include "sysinfo.h"
63 #include "module_common.h"
64
65 #if defined(SUPPORT_SYSTEM_INTERRUPT_HANDLING)
66 #include "syscommon.h"
67 #endif
68
69 #if defined(SUPPORT_SHARED_SLC)
70 #include "rgxapi_km.h"
71 #endif
72
73 #if defined(SUPPORT_DISPLAY_CLASS)
74 #include "kerneldisplay.h"
75 #endif
76
77 /*
78  * DRVNAME is the name we use to register our driver.
79  * DEVNAME is the name we use to register actual device nodes.
80  */
81 #define DRVNAME         PVR_LDM_DRIVER_REGISTRATION_NAME
82 #define DEVNAME         PVRSRV_MODNAME
83
84 /*
85  * This is all module configuration stuff required by the linux kernel.
86  */
87 MODULE_SUPPORTED_DEVICE(DEVNAME);
88
89 #if defined(SUPPORT_DISPLAY_CLASS)
90 /* Display class interface */
91 #include "kerneldisplay.h"
92 EXPORT_SYMBOL(DCRegisterDevice);
93 EXPORT_SYMBOL(DCUnregisterDevice);
94 EXPORT_SYMBOL(DCDisplayConfigurationRetired);
95 EXPORT_SYMBOL(DCDisplayHasPendingCommand);
96 EXPORT_SYMBOL(DCImportBufferAcquire);
97 EXPORT_SYMBOL(DCImportBufferRelease);
98
99 /* Physmem interface (required by LMA DC drivers) */
100 #include "physheap.h"
101 EXPORT_SYMBOL(PhysHeapAcquire);
102 EXPORT_SYMBOL(PhysHeapRelease);
103 EXPORT_SYMBOL(PhysHeapGetType);
104 EXPORT_SYMBOL(PhysHeapGetAddress);
105 EXPORT_SYMBOL(PhysHeapGetSize);
106 EXPORT_SYMBOL(PhysHeapCpuPAddrToDevPAddr);
107 #endif
108
109 /* System interface (required by DC drivers) */
110 #if defined(SUPPORT_SYSTEM_INTERRUPT_HANDLING)
111 EXPORT_SYMBOL(SysInstallDeviceLISR);
112 EXPORT_SYMBOL(SysUninstallDeviceLISR);
113 #endif
114
115 #if defined(SUPPORT_SHARED_SLC)
116 EXPORT_SYMBOL(RGXInitSLC);
117 #endif
118 #include "pvrversion.h"  //add by zxl
119
120 #if defined(CONFIG_OF)
121 #include <linux/of.h>
122 #include <linux/of_device.h>
123 #endif
124
125 struct device *psDev;
126
127 /*
128  * Device class used for /sys entries (and udev device node creation)
129  */
130 static struct class *psPvrClass;
131
132 /*
133  * This is the major number we use for all nodes in /dev.
134  */
135 static int AssignedMajorNumber;
136
137 static const struct of_device_id rockchip_gpu_dt_ids[] = {
138     { .compatible = "arm,rogue-G6110", },
139         { .compatible = "arm,rk3368-gpu", },
140         {},
141 };
142
143 /*
144  * These are the operations that will be associated with the device node
145  * we create.
146  *
147  * With gcc -W, specifying only the non-null members produces "missing
148  * initializer" warnings.
149 */
150 static int PVRSRVOpen(struct inode* pInode, struct file* pFile);
151 static int PVRSRVRelease(struct inode* pInode, struct file* pFile);
152
153 static struct file_operations pvrsrv_fops =
154 {
155         .owner          = THIS_MODULE,
156         .unlocked_ioctl = PVRSRV_BridgeDispatchKM,
157 #if defined(CONFIG_COMPAT)
158         .compat_ioctl   = PVRSRV_BridgeCompatDispatchKM,
159 #endif
160         .open           = PVRSRVOpen,
161         .release        = PVRSRVRelease,
162         .mmap           = MMapPMR,
163 };
164
165 #if defined(LDM_PLATFORM)
166 #define LDM_DRV struct platform_driver
167 #endif /*LDM_PLATFORM */
168
169 #if defined(LDM_PCI)
170 #define LDM_DRV struct pci_driver
171 #endif /* LDM_PCI */
172
173 #if defined(LDM_PLATFORM)
174 static int PVRSRVDriverRemove(LDM_DEV *device);
175 static int PVRSRVDriverProbe(LDM_DEV *device);
176 #endif
177
178 #if defined(LDM_PCI)
179 static void PVRSRVDriverRemove(LDM_DEV *device);
180 static int PVRSRVDriverProbe(LDM_DEV *device, const struct pci_device_id *id);
181 #endif
182
183 #if defined(LDM_PCI)
184 /* This structure is used by the Linux module code */
185 struct pci_device_id powervr_id_table[] __devinitdata = {
186         {PCI_DEVICE(SYS_RGX_DEV_VENDOR_ID, SYS_RGX_DEV_DEVICE_ID)},
187 #if defined (SYS_RGX_DEV1_DEVICE_ID)
188         {PCI_DEVICE(SYS_RGX_DEV_VENDOR_ID, SYS_RGX_DEV1_DEVICE_ID)},
189 #endif
190         {0}
191 };
192 MODULE_DEVICE_TABLE(pci, powervr_id_table);
193 #endif /*defined(LDM_PCI) */
194
195 #if defined(PVR_USE_PRE_REGISTERED_PLATFORM_DEV)
196 static struct platform_device_id powervr_id_table[] __devinitdata = {
197         {SYS_RGX_DEV_NAME, 0},
198         {}
199 };
200 #endif
201
202 static struct dev_pm_ops powervr_dev_pm_ops = {
203         .suspend        = PVRSRVDriverSuspend,
204         .resume         = PVRSRVDriverResume,
205 };
206
207 static LDM_DRV powervr_driver = {
208 #if defined(LDM_PLATFORM)
209         .driver = {
210                 .name   = DRVNAME,
211                 .pm     = &powervr_dev_pm_ops,
212                 .of_match_table = of_match_ptr(rockchip_gpu_dt_ids),
213         },
214 #endif
215 #if defined(LDM_PCI)
216         .name           = DRVNAME,
217         .driver.pm      = &powervr_dev_pm_ops,
218 #endif
219 #if defined(LDM_PCI) || defined(PVR_USE_PRE_REGISTERED_PLATFORM_DEV)
220         .id_table       = powervr_id_table,
221 #endif
222         .probe          = PVRSRVDriverProbe,
223 #if defined(LDM_PLATFORM)
224         .remove         = PVRSRVDriverRemove,
225 #endif
226 #if defined(LDM_PCI)
227         .remove         = __devexit_p(PVRSRVDriverRemove),
228 #endif
229         .shutdown       = PVRSRVDriverShutdown,
230 };
231
232 #if defined(LDM_PLATFORM)
233 #if defined(MODULE) && !defined(PVR_USE_PRE_REGISTERED_PLATFORM_DEV)
234 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3,2,0))
235 /*static void PVRSRVDeviceRelease(struct device unref__ *pDevice)
236 {
237 }
238
239 static struct platform_device powervr_device =
240 {
241         .name                   = DEVNAME,
242         .id                     = -1,
243         .dev                    = {
244                 .release        = PVRSRVDeviceRelease
245         }
246 };*/
247 //time:2012-09-08
248 //move platform_device_register from devices.c to sgx
249 static struct resource resources_sgx[] = {
250     [0] = {
251         .name  = "gpu_irq",
252         .start     = IRQ_GPU,
253         .end    = IRQ_GPU,
254         .flags  = IORESOURCE_IRQ,
255     },
256     [1] = {
257         .name   = "gpu_base",
258         .start  = RK30_GPU_PHYS ,
259         .end    = RK30_GPU_PHYS  + RK30_GPU_SIZE - 1,
260         .flags  = IORESOURCE_MEM,
261     },
262 };
263 static struct platform_device powervr_device = {
264     .name             = DEVNAME,
265     .id               = 0,
266     .num_resources    = ARRAY_SIZE(resources_sgx),
267     .resource         = resources_sgx,
268 };
269 #else
270 static struct platform_device_info powervr_device_info =
271 {
272         .name                   = DEVNAME,
273         .id                     = -1,
274         .dma_mask               = DMA_BIT_MASK(32),
275 };
276 #endif  /* (LINUX_VERSION_CODE < KERNEL_VERSION(3,2,0)) */
277 #endif  /* defined(MODULE) && !defined(PVR_USE_PRE_REGISTERED_PLATFORM_DEV) */
278 #endif  /* defined(LDM_PLATFORM) */
279
280 static IMG_BOOL bCalledSysInit = IMG_FALSE;
281 static IMG_BOOL bDriverProbeSucceeded = IMG_FALSE;
282
283 /*!
284 ******************************************************************************
285
286  @Function              PVRSRVSystemInit
287
288  @Description
289
290  Wrapper for PVRSRVInit.
291
292  @input pDevice - the device for which a probe is requested
293
294  @Return 0 for success or <0 for an error.
295
296 *****************************************************************************/
297 static int PVRSRVSystemInit(LDM_DEV *pDevice)
298 {
299         PVR_TRACE(("PVRSRVSystemInit (pDevice=%p)", pDevice));
300
301         gpsPVRLDMDev = pDevice;
302         bCalledSysInit = IMG_TRUE;
303
304         if (PVRSRVInit(pDevice) != PVRSRV_OK)
305         {
306                 return -ENODEV;
307         }
308
309         return 0;
310 }
311
312 /*!
313 ******************************************************************************
314
315  @Function              PVRSRVSystemDeInit
316
317  @Description
318
319  Wrapper for PVRSRVDeInit.
320
321  @input pDevice - the device for which a probe is requested
322  @Return nothing.
323
324 *****************************************************************************/
325 static void PVRSRVSystemDeInit(LDM_DEV *pDevice)
326 {
327         PVR_TRACE(("PVRSRVSystemDeInit"));
328
329         PVRSRVDeInit(pDevice);
330
331 #if !defined(LDM_PLATFORM) || (LINUX_VERSION_CODE < KERNEL_VERSION(3,2,0))
332         gpsPVRLDMDev = IMG_NULL;
333 #endif
334 }
335
336 /*!
337 ******************************************************************************
338
339  @Function              PVRSRVDriverProbe
340
341  @Description
342
343  See whether a given device is really one we can drive.
344
345  @input pDevice - the device for which a probe is requested
346
347  @Return 0 for success or <0 for an error.
348
349 *****************************************************************************/
350 #if defined(LDM_PLATFORM)
351 static int PVRSRVDriverProbe(LDM_DEV *pDevice)
352 #endif
353 #if defined(LDM_PCI)
354 static int __devinit PVRSRVDriverProbe(LDM_DEV *pDevice, const struct pci_device_id *pID)
355 #endif
356 {
357         int result = 0;
358
359         PVR_TRACE(("PVRSRVDriverProbe (pDevice=%p)", pDevice));
360
361     //zxl:print gpu version on boot time
362     printk("PVR_K: sys.gpvr.version=%s\n",RKVERSION);
363
364         result = PVRSRVSystemInit(pDevice);
365         bDriverProbeSucceeded = (result == 0);
366         return result;
367 }
368
369
370 /*!
371 ******************************************************************************
372
373  @Function              PVRSRVDriverRemove
374
375  @Description
376
377  This call is the opposite of the probe call; it is called when the device is
378  being removed from the driver's control.
379
380  @input pDevice - the device for which driver detachment is happening
381
382  @Return 0, or no return value at all, depending on the device type.
383
384 *****************************************************************************/
385 #if defined (LDM_PLATFORM)
386 static int PVRSRVDriverRemove(LDM_DEV *pDevice)
387 #endif
388 #if defined(LDM_PCI)
389 static void __devexit PVRSRVDriverRemove(LDM_DEV *pDevice)
390 #endif
391 {
392         PVR_TRACE(("PVRSRVDriverRemove (pDevice=%p)", pDevice));
393
394         PVRSRVSystemDeInit(pDevice);
395
396 #if defined(LDM_PLATFORM)
397         return 0;
398 #endif
399 }
400
401 /*!
402 ******************************************************************************
403
404  @Function              PVRSRVOpen
405
406  @Description
407
408  Open the PVR services node.
409
410  @input pInode - the inode for the file being openeded.
411  @input dev    - the DRM device corresponding to this driver.
412
413  @input pFile - the file handle data for the actual file being opened
414
415  @Return 0 for success or <0 for an error.
416
417 *****************************************************************************/
418 static int PVRSRVOpen(struct inode unref__ *pInode, struct file *pFile)
419 {
420         int err;
421
422         if (!try_module_get(THIS_MODULE))
423         {
424                 PVR_DPF((PVR_DBG_ERROR, "Failed to get module"));
425                 return -ENOENT;
426         }
427
428         if ((err = PVRSRVCommonOpen(pFile)) != 0)
429         {
430                 module_put(THIS_MODULE);
431         }
432
433         return err;
434 }
435
436 /*!
437 ******************************************************************************
438
439  @Function              PVRSRVRelease
440
441  @Description
442
443  Release access the PVR services node - called when a file is closed, whether
444  at exit or using close(2) system call.
445
446  @input pInode - the inode for the file being released
447  @input pvPrivData - driver private data
448
449  @input pFile - the file handle data for the actual file being released
450
451  @Return 0 for success or <0 for an error.
452
453 *****************************************************************************/
454 static int PVRSRVRelease(struct inode unref__ *pInode, struct file *pFile)
455 {
456         PVRSRVCommonRelease(pFile);
457
458         module_put(THIS_MODULE);
459
460         return 0;
461 }
462
463 /*!
464 ******************************************************************************
465
466  @Function              PVRCore_Init
467
468  @Description
469
470  Insert the driver into the kernel.
471
472  Readable and/or writable debugfs entries under /sys/kernel/debug/pvr are
473  created with PVRDebugFSCreateEntry().  These can be read at runtime to get
474  information about the device (eg. 'cat /sys/kernel/debug/pvr/nodes')
475
476  __init places the function in a special memory section that the kernel frees
477  once the function has been run.  Refer also to module_init() macro call below.
478
479  @input none
480
481  @Return none
482
483 *****************************************************************************/
484 static int __init PVRCore_Init(void)
485 {
486         int error = 0;
487
488         PVR_TRACE(("PVRCore_Init"));
489
490         if ((error = PVRSRVDriverInit()) != 0)
491         {
492                 return error;
493         }
494
495         AssignedMajorNumber = register_chrdev(0, DEVNAME, &pvrsrv_fops);
496         if (AssignedMajorNumber <= 0)
497         {
498                 PVR_DPF((PVR_DBG_ERROR, "PVRCore_Init: unable to get major number"));
499                 return -EBUSY;
500         }
501
502         PVR_TRACE(("PVRCore_Init: major device %d", AssignedMajorNumber));
503
504         /*
505          * This code facilitates automatic device node creation on platforms
506          * with udev (or similar).
507          */
508         psPvrClass = class_create(THIS_MODULE, "pvr");
509         if (IS_ERR(psPvrClass))
510         {
511                 PVR_DPF((PVR_DBG_ERROR, "PVRCore_Init: unable to create class (%ld)", PTR_ERR(psPvrClass)));
512                 return -EBUSY;
513         }
514
515         psDev = device_create(psPvrClass, NULL, MKDEV(AssignedMajorNumber, 0),
516                                   NULL, DEVNAME);
517         if (IS_ERR(psDev))
518         {
519                 PVR_DPF((PVR_DBG_ERROR, "PVRCore_Init: unable to create device (%ld)", PTR_ERR(psDev)));
520                 return -EBUSY;
521         }
522
523 #if defined(LDM_PLATFORM)
524         error = platform_driver_register(&powervr_driver);
525         if (error != 0)
526         {
527                 PVR_DPF((PVR_DBG_ERROR, "PVRCore_Init: unable to register platform driver (%d)", error));
528                 return error;
529         }
530
531 #if defined(MODULE) && !defined(PVR_USE_PRE_REGISTERED_PLATFORM_DEV)
532 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3,2,0))
533         error = platform_device_register(&powervr_device);
534 #else
535         gpsPVRLDMDev = platform_device_register_full(&powervr_device_info);
536         error = IS_ERR(gpsPVRLDMDev) ? PTR_ERR(gpsPVRLDMDev) : 0;
537 #endif
538         if (error != 0)
539         {
540                 PVR_DPF((PVR_DBG_ERROR, "PVRCore_Init: unable to register platform device (%d)", error));
541                 return error;
542         }
543 #endif /* defined(MODULE) && !defined(PVR_USE_PRE_REGISTERED_PLATFORM_DEV) */
544 #endif /* defined(LDM_PLATFORM) */ 
545
546 #if defined(LDM_PCI)
547         error = pci_register_driver(&powervr_driver);
548         if (error != 0)
549         {
550                 PVR_DPF((PVR_DBG_ERROR, "PVRCore_Init: unable to register PCI driver (%d)", error));
551                 return error;
552         }
553 #endif /* defined(LDM_PCI) */
554
555         /* Check that the driver probe function was called */
556         if (!bDriverProbeSucceeded)
557         {
558                 PVR_TRACE(("PVRCore_Init: PVRSRVDriverProbe has not been called or did not succeed - check that hardware is detected"));
559                 return error;
560         }
561
562         return PVRSRVDeviceInit();
563 }
564
565
566 /*!
567 *****************************************************************************
568
569  @Function              PVRCore_Cleanup
570
571  @Description   
572
573  Remove the driver from the kernel.
574
575  There's no way we can get out of being unloaded other than panicking; we
576  just do everything and plough on regardless of error.
577
578  __exit places the function in a special memory section that the kernel frees
579  once the function has been run.  Refer also to module_exit() macro call below.
580
581  @input none
582
583  @Return none
584
585 *****************************************************************************/
586 static void __exit PVRCore_Cleanup(void)
587 {
588         PVR_TRACE(("PVRCore_Cleanup"));
589
590         PVRSRVDeviceDeinit();
591
592         if (psDev)
593         {
594                 device_destroy(psPvrClass, MKDEV(AssignedMajorNumber, 0));
595         }
596
597         if (psPvrClass)
598         {
599                 class_destroy(psPvrClass);
600         }
601
602         if (AssignedMajorNumber > 0)
603         {
604                 unregister_chrdev((IMG_UINT)AssignedMajorNumber, DEVNAME);
605         }
606
607 #if defined(LDM_PCI)
608         pci_unregister_driver(&powervr_driver);
609 #endif /* defined(LDM_PCI) */
610
611 #if defined (LDM_PLATFORM)
612 #if defined(MODULE) && !defined(PVR_USE_PRE_REGISTERED_PLATFORM_DEV)
613 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3,2,0))
614         platform_device_unregister(&powervr_device);
615 #else
616         PVR_ASSERT(gpsPVRLDMDev != NULL);
617         platform_device_unregister(gpsPVRLDMDev);
618 #endif /* (LINUX_VERSION_CODE < KERNEL_VERSION(3,2,0)) */
619 #endif /* defined(MODULE) && !defined(PVR_USE_PRE_REGISTERED_PLATFORM_DEV) */
620         platform_driver_unregister(&powervr_driver);
621 #endif /* defined (LDM_PLATFORM) */
622
623         PVRSRVDriverDeinit();
624
625         PVR_TRACE(("PVRCore_Cleanup: unloading"));
626 }
627
628 /*
629  * These macro calls define the initialisation and removal functions of the
630  * driver.  Although they are prefixed `module_', they apply when compiling
631  * statically as well; in both cases they define the function the kernel will
632  * run to start/stop the driver.
633 */
634 module_init(PVRCore_Init);
635 module_exit(PVRCore_Cleanup);