PageRenderTime 1088ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/gpu/drm/pvrsgx/1.14.3699939/eurasia_km/services4/srvkm/env/linux/pvr_bridge_k.c

https://github.com/goldelico/gta04-kernel
C | 525 lines | 371 code | 63 blank | 91 comment | 44 complexity | 4ea9467d6c83ef422f9b437d1d0dbb49 MD5 | raw file
  1. /*************************************************************************/ /*!
  2. @Title PVR Bridge Module (kernel side)
  3. @Copyright Copyright (c) Imagination Technologies Ltd. All Rights Reserved
  4. @Description Receives calls from the user portion of services and
  5. despatches them to functions in the kernel portion.
  6. @License Dual MIT/GPLv2
  7. 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:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. Alternatively, the contents of this file may be used under the terms of
  17. the GNU General Public License Version 2 ("GPL") in which case the provisions
  18. of GPL are applicable instead of those above.
  19. If you wish to allow use of your version of this file only under the terms of
  20. GPL, and not to allow others to use your version of this file under the terms
  21. of the MIT license, indicate your decision by deleting the provisions above
  22. and replace them with the notice and other provisions required by GPL as set
  23. out in the file called "GPL-COPYING" included in this distribution. If you do
  24. not delete the provisions above, a recipient may use your version of this file
  25. under the terms of either the MIT license or GPL.
  26. This License is also included in this distribution in the file called
  27. "MIT-COPYING".
  28. EXCEPT AS OTHERWISE STATED IN A NEGOTIATED AGREEMENT: (A) THE SOFTWARE IS
  29. PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  30. BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  31. PURPOSE AND NONINFRINGEMENT; AND (B) IN NO EVENT SHALL THE AUTHORS OR
  32. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  33. IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  34. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  35. */ /**************************************************************************/
  36. #include <linux/fs.h>
  37. #include "img_defs.h"
  38. #include "services.h"
  39. #include "pvr_bridge.h"
  40. #include "perproc.h"
  41. #include "mutex.h"
  42. #include "syscommon.h"
  43. #include "pvr_debug.h"
  44. #include "proc.h"
  45. #include "private_data.h"
  46. #include "linkage.h"
  47. #include "pvr_bridge_km.h"
  48. #include "pvr_uaccess.h"
  49. #include "refcount.h"
  50. #include "buffer_manager.h"
  51. #if defined(SUPPORT_DRI_DRM)
  52. #include <drm/drmP.h>
  53. #include "pvr_drm.h"
  54. #if defined(PVR_SECURE_DRM_AUTH_EXPORT)
  55. #include "env_perproc.h"
  56. #endif
  57. #endif
  58. /* VGX: */
  59. #if defined(SUPPORT_VGX)
  60. #include "vgx_bridge.h"
  61. #endif
  62. /* SGX: */
  63. #if defined(SUPPORT_SGX)
  64. #include "sgx_bridge.h"
  65. #endif
  66. #include "bridged_pvr_bridge.h"
  67. #if defined(SUPPORT_DRI_DRM)
  68. #define PRIVATE_DATA(pFile) ((pFile)->driver_priv)
  69. #else
  70. #define PRIVATE_DATA(pFile) ((pFile)->private_data)
  71. #endif
  72. #if defined(DEBUG_BRIDGE_KM)
  73. static struct pvr_proc_dir_entry *g_ProcBridgeStats =0;
  74. static void* ProcSeqNextBridgeStats(struct seq_file *sfile,void* el,loff_t off);
  75. static void ProcSeqShowBridgeStats(struct seq_file *sfile,void* el);
  76. static void* ProcSeqOff2ElementBridgeStats(struct seq_file * sfile, loff_t off);
  77. static void ProcSeqStartstopBridgeStats(struct seq_file *sfile,IMG_BOOL start);
  78. #endif
  79. extern PVRSRV_LINUX_MUTEX gPVRSRVLock;
  80. #if defined(SUPPORT_MEMINFO_IDS)
  81. IMG_UINT64 g_ui64MemInfoID;
  82. #endif /* defined(SUPPORT_MEMINFO_IDS) */
  83. PVRSRV_ERROR
  84. LinuxBridgeInit(IMG_VOID)
  85. {
  86. #if defined(DEBUG_BRIDGE_KM)
  87. {
  88. g_ProcBridgeStats = CreateProcReadEntrySeq(
  89. "bridge_stats",
  90. NULL,
  91. ProcSeqNextBridgeStats,
  92. ProcSeqShowBridgeStats,
  93. ProcSeqOff2ElementBridgeStats,
  94. ProcSeqStartstopBridgeStats
  95. );
  96. if(!g_ProcBridgeStats)
  97. {
  98. return PVRSRV_ERROR_OUT_OF_MEMORY;
  99. }
  100. }
  101. #endif
  102. return CommonBridgeInit();
  103. }
  104. IMG_VOID
  105. LinuxBridgeDeInit(IMG_VOID)
  106. {
  107. #if defined(DEBUG_BRIDGE_KM)
  108. RemoveProcEntrySeq(g_ProcBridgeStats);
  109. #endif
  110. }
  111. #if defined(DEBUG_BRIDGE_KM)
  112. /*
  113. * Lock MMap regions list (called on page start/stop while reading /proc/mmap)
  114. *
  115. * sfile : seq_file that handles /proc file
  116. * start : TRUE if it's start, FALSE if it's stop
  117. *
  118. */
  119. static void ProcSeqStartstopBridgeStats(struct seq_file *sfile,IMG_BOOL start)
  120. {
  121. if(start)
  122. {
  123. LinuxLockMutexNested(&gPVRSRVLock, PVRSRV_LOCK_CLASS_BRIDGE);
  124. }
  125. else
  126. {
  127. LinuxUnLockMutex(&gPVRSRVLock);
  128. }
  129. }
  130. /*
  131. * Convert offset (index from KVOffsetTable) to element
  132. * (called when reading /proc/mmap file)
  133. * sfile : seq_file that handles /proc file
  134. * off : index into the KVOffsetTable from which to print
  135. *
  136. * returns void* : Pointer to element that will be dumped
  137. *
  138. */
  139. static void* ProcSeqOff2ElementBridgeStats(struct seq_file *sfile, loff_t off)
  140. {
  141. if(!off)
  142. {
  143. return PVR_PROC_SEQ_START_TOKEN;
  144. }
  145. if(off > BRIDGE_DISPATCH_TABLE_ENTRY_COUNT)
  146. {
  147. return (void*)0;
  148. }
  149. return (void*)&g_BridgeDispatchTable[off-1];
  150. }
  151. /*
  152. * Gets next MMap element to show. (called when reading /proc/mmap file)
  153. * sfile : seq_file that handles /proc file
  154. * el : actual element
  155. * off : index into the KVOffsetTable from which to print
  156. *
  157. * returns void* : Pointer to element to show (0 ends iteration)
  158. */
  159. static void* ProcSeqNextBridgeStats(struct seq_file *sfile,void* el,loff_t off)
  160. {
  161. return ProcSeqOff2ElementBridgeStats(sfile,off);
  162. }
  163. /*
  164. * Show MMap element (called when reading /proc/mmap file)
  165. * sfile : seq_file that handles /proc file
  166. * el : actual element
  167. *
  168. */
  169. static void ProcSeqShowBridgeStats(struct seq_file *sfile,void* el)
  170. {
  171. PVRSRV_BRIDGE_DISPATCH_TABLE_ENTRY *psEntry = ( PVRSRV_BRIDGE_DISPATCH_TABLE_ENTRY*)el;
  172. if(el == PVR_PROC_SEQ_START_TOKEN)
  173. {
  174. seq_printf(sfile,
  175. "Total ioctl call count = %u\n"
  176. "Total number of bytes copied via copy_from_user = %u\n"
  177. "Total number of bytes copied via copy_to_user = %u\n"
  178. "Total number of bytes copied via copy_*_user = %u\n\n"
  179. "%-45s | %-40s | %10s | %20s | %10s\n",
  180. g_BridgeGlobalStats.ui32IOCTLCount,
  181. g_BridgeGlobalStats.ui32TotalCopyFromUserBytes,
  182. g_BridgeGlobalStats.ui32TotalCopyToUserBytes,
  183. g_BridgeGlobalStats.ui32TotalCopyFromUserBytes+g_BridgeGlobalStats.ui32TotalCopyToUserBytes,
  184. "Bridge Name",
  185. "Wrapper Function",
  186. "Call Count",
  187. "copy_from_user Bytes",
  188. "copy_to_user Bytes"
  189. );
  190. return;
  191. }
  192. seq_printf(sfile,
  193. "%-45s %-40s %-10u %-20u %-10u\n",
  194. psEntry->pszIOCName,
  195. psEntry->pszFunctionName,
  196. psEntry->ui32CallCount,
  197. psEntry->ui32CopyFromUserTotalBytes,
  198. psEntry->ui32CopyToUserTotalBytes);
  199. }
  200. #endif /* DEBUG_BRIDGE_KM */
  201. #if defined(SUPPORT_DRI_DRM)
  202. int
  203. PVRSRV_BridgeDispatchKM(struct drm_device unref__ *dev, void *arg, struct drm_file *pFile)
  204. #else
  205. long
  206. PVRSRV_BridgeDispatchKM(struct file *pFile, unsigned int unref__ ioctlCmd, unsigned long arg)
  207. #endif
  208. {
  209. IMG_UINT32 cmd;
  210. #if !defined(SUPPORT_DRI_DRM)
  211. PVRSRV_BRIDGE_PACKAGE *psBridgePackageUM = (PVRSRV_BRIDGE_PACKAGE *)arg;
  212. PVRSRV_BRIDGE_PACKAGE sBridgePackageKM;
  213. #endif
  214. PVRSRV_BRIDGE_PACKAGE *psBridgePackageKM;
  215. IMG_UINT32 ui32PID = OSGetCurrentProcessIDKM();
  216. PVRSRV_PER_PROCESS_DATA *psPerProc;
  217. IMG_INT err = -EFAULT;
  218. LinuxLockMutexNested(&gPVRSRVLock, PVRSRV_LOCK_CLASS_BRIDGE);
  219. #if defined(SUPPORT_DRI_DRM)
  220. psBridgePackageKM = (PVRSRV_BRIDGE_PACKAGE *)arg;
  221. PVR_ASSERT(psBridgePackageKM != IMG_NULL);
  222. #else
  223. psBridgePackageKM = &sBridgePackageKM;
  224. if(!OSAccessOK(PVR_VERIFY_WRITE,
  225. psBridgePackageUM,
  226. sizeof(PVRSRV_BRIDGE_PACKAGE)))
  227. {
  228. PVR_DPF((PVR_DBG_ERROR, "%s: Received invalid pointer to function arguments",
  229. __FUNCTION__));
  230. goto unlock_and_return;
  231. }
  232. /* FIXME - Currently the CopyFromUserWrapper which collects stats about
  233. * how much data is shifted to/from userspace isn't available to us
  234. * here. */
  235. if(OSCopyFromUser(IMG_NULL,
  236. psBridgePackageKM,
  237. psBridgePackageUM,
  238. sizeof(PVRSRV_BRIDGE_PACKAGE))
  239. != PVRSRV_OK)
  240. {
  241. goto unlock_and_return;
  242. }
  243. #endif
  244. cmd = psBridgePackageKM->ui32BridgeID;
  245. if(cmd != PVRSRV_BRIDGE_CONNECT_SERVICES)
  246. {
  247. PVRSRV_ERROR eError;
  248. eError = PVRSRVLookupHandle(KERNEL_HANDLE_BASE,
  249. (IMG_PVOID *)&psPerProc,
  250. psBridgePackageKM->hKernelServices,
  251. PVRSRV_HANDLE_TYPE_PERPROC_DATA);
  252. if(eError != PVRSRV_OK)
  253. {
  254. PVR_DPF((PVR_DBG_ERROR, "%s: Invalid kernel services handle (%d)",
  255. __FUNCTION__, eError));
  256. goto unlock_and_return;
  257. }
  258. if(psPerProc->ui32PID != ui32PID)
  259. {
  260. PVR_DPF((PVR_DBG_ERROR, "%s: Process %d tried to access data "
  261. "belonging to process %d", __FUNCTION__, ui32PID,
  262. psPerProc->ui32PID));
  263. goto unlock_and_return;
  264. }
  265. }
  266. else
  267. {
  268. /* lookup per-process data for this process */
  269. psPerProc = PVRSRVPerProcessData(ui32PID);
  270. if(psPerProc == IMG_NULL)
  271. {
  272. PVR_DPF((PVR_DBG_ERROR, "PVRSRV_BridgeDispatchKM: "
  273. "Couldn't create per-process data area"));
  274. goto unlock_and_return;
  275. }
  276. }
  277. psBridgePackageKM->ui32BridgeID = PVRSRV_GET_BRIDGE_ID(psBridgePackageKM->ui32BridgeID);
  278. switch(cmd)
  279. {
  280. case PVRSRV_BRIDGE_EXPORT_DEVICEMEM_2:
  281. {
  282. PVRSRV_FILE_PRIVATE_DATA *psPrivateData = PRIVATE_DATA(pFile);
  283. if(psPrivateData->hKernelMemInfo)
  284. {
  285. PVR_DPF((PVR_DBG_ERROR, "%s: Can only export one MemInfo "
  286. "per file descriptor", __FUNCTION__));
  287. err = -EINVAL;
  288. goto unlock_and_return;
  289. }
  290. break;
  291. }
  292. case PVRSRV_BRIDGE_MAP_DEV_MEMORY_2:
  293. {
  294. PVRSRV_BRIDGE_IN_MAP_DEV_MEMORY *psMapDevMemIN =
  295. (PVRSRV_BRIDGE_IN_MAP_DEV_MEMORY *)psBridgePackageKM->pvParamIn;
  296. PVRSRV_FILE_PRIVATE_DATA *psPrivateData = PRIVATE_DATA(pFile);
  297. if(!psPrivateData->hKernelMemInfo)
  298. {
  299. PVR_DPF((PVR_DBG_ERROR, "%s: File descriptor has no "
  300. "associated MemInfo handle", __FUNCTION__));
  301. err = -EINVAL;
  302. goto unlock_and_return;
  303. }
  304. if (pvr_put_user(psPrivateData->hKernelMemInfo, &psMapDevMemIN->hKernelMemInfo) != 0)
  305. {
  306. err = -EFAULT;
  307. goto unlock_and_return;
  308. }
  309. break;
  310. }
  311. default:
  312. {
  313. PVRSRV_FILE_PRIVATE_DATA *psPrivateData = PRIVATE_DATA(pFile);
  314. if(psPrivateData->hKernelMemInfo)
  315. {
  316. PVR_DPF((PVR_DBG_ERROR, "%s: Import/Export handle tried "
  317. "to use privileged service", __FUNCTION__));
  318. goto unlock_and_return;
  319. }
  320. break;
  321. }
  322. }
  323. #if defined(SUPPORT_DRI_DRM) && defined(PVR_SECURE_DRM_AUTH_EXPORT)
  324. switch(cmd)
  325. {
  326. case PVRSRV_BRIDGE_MAP_DEV_MEMORY:
  327. case PVRSRV_BRIDGE_MAP_DEVICECLASS_MEMORY:
  328. {
  329. PVRSRV_FILE_PRIVATE_DATA *psPrivateData;
  330. int authenticated = pFile->authenticated;
  331. PVRSRV_ENV_PER_PROCESS_DATA *psEnvPerProc;
  332. if (authenticated)
  333. {
  334. break;
  335. }
  336. /*
  337. * The DRM file structure we are using for Services
  338. * is not one that DRI authentication was done on.
  339. * Look for an authenticated file structure for
  340. * this process, making sure the DRM master is the
  341. * same as ours.
  342. */
  343. psEnvPerProc = (PVRSRV_ENV_PER_PROCESS_DATA *)PVRSRVProcessPrivateData(psPerProc);
  344. if (psEnvPerProc == IMG_NULL)
  345. {
  346. PVR_DPF((PVR_DBG_ERROR, "%s: Process private data not allocated", __FUNCTION__));
  347. err = -EFAULT;
  348. goto unlock_and_return;
  349. }
  350. list_for_each_entry(psPrivateData, &psEnvPerProc->sDRMAuthListHead, sDRMAuthListItem)
  351. {
  352. struct drm_file *psDRMFile = psPrivateData->psDRMFile;
  353. if (pFile->master == psDRMFile->master)
  354. {
  355. authenticated |= psDRMFile->authenticated;
  356. if (authenticated)
  357. {
  358. break;
  359. }
  360. }
  361. }
  362. if (!authenticated)
  363. {
  364. PVR_DPF((PVR_DBG_ERROR, "%s: Not authenticated for mapping device or device class memory", __FUNCTION__));
  365. err = -EPERM;
  366. goto unlock_and_return;
  367. }
  368. break;
  369. }
  370. default:
  371. break;
  372. }
  373. #endif /* defined(SUPPORT_DRI_DRM) && defined(PVR_SECURE_DRM_AUTH_EXPORT) */
  374. err = BridgedDispatchKM(psPerProc, psBridgePackageKM);
  375. if(err != PVRSRV_OK)
  376. goto unlock_and_return;
  377. switch(cmd)
  378. {
  379. case PVRSRV_BRIDGE_EXPORT_DEVICEMEM_2:
  380. {
  381. PVRSRV_BRIDGE_OUT_EXPORTDEVICEMEM *psExportDeviceMemOUT =
  382. (PVRSRV_BRIDGE_OUT_EXPORTDEVICEMEM *)psBridgePackageKM->pvParamOut;
  383. PVRSRV_FILE_PRIVATE_DATA *psPrivateData = PRIVATE_DATA(pFile);
  384. IMG_HANDLE hMemInfo;
  385. PVRSRV_KERNEL_MEM_INFO *psKernelMemInfo;
  386. if (pvr_get_user(hMemInfo, &psExportDeviceMemOUT->hMemInfo) != 0)
  387. {
  388. err = -EFAULT;
  389. goto unlock_and_return;
  390. }
  391. /* Look up the meminfo we just exported */
  392. if(PVRSRVLookupHandle(KERNEL_HANDLE_BASE,
  393. (IMG_PVOID *)&psKernelMemInfo,
  394. hMemInfo,
  395. PVRSRV_HANDLE_TYPE_MEM_INFO) != PVRSRV_OK)
  396. {
  397. PVR_DPF((PVR_DBG_ERROR, "%s: Failed to look up export handle", __FUNCTION__));
  398. err = -EFAULT;
  399. goto unlock_and_return;
  400. }
  401. /* Bump the refcount; decremented on release of the fd */
  402. PVRSRVKernelMemInfoIncRef(psKernelMemInfo);
  403. /* Tell the XProc about the export if required */
  404. if (psKernelMemInfo->sShareMemWorkaround.bInUse)
  405. {
  406. BM_XProcIndexAcquire(psKernelMemInfo->sShareMemWorkaround.ui32ShareIndex);
  407. }
  408. psPrivateData->hKernelMemInfo = hMemInfo;
  409. #if defined(SUPPORT_MEMINFO_IDS)
  410. psPrivateData->ui64Stamp = ++g_ui64MemInfoID;
  411. psKernelMemInfo->ui64Stamp = psPrivateData->ui64Stamp;
  412. if (pvr_put_user(psPrivateData->ui64Stamp, &psExportDeviceMemOUT->ui64Stamp) != 0)
  413. {
  414. err = -EFAULT;
  415. goto unlock_and_return;
  416. }
  417. #endif
  418. break;
  419. }
  420. #if defined(SUPPORT_MEMINFO_IDS)
  421. case PVRSRV_BRIDGE_MAP_DEV_MEMORY:
  422. case PVRSRV_BRIDGE_MAP_DEV_MEMORY_2:
  423. {
  424. PVRSRV_BRIDGE_OUT_MAP_DEV_MEMORY *psMapDeviceMemoryOUT =
  425. (PVRSRV_BRIDGE_OUT_MAP_DEV_MEMORY *)psBridgePackageKM->pvParamOut;
  426. PVRSRV_FILE_PRIVATE_DATA *psPrivateData = PRIVATE_DATA(pFile);
  427. if (pvr_put_user(psPrivateData->ui64Stamp, &psMapDeviceMemoryOUT->sDstClientMemInfo.ui64Stamp) != 0)
  428. {
  429. err = -EFAULT;
  430. goto unlock_and_return;
  431. }
  432. break;
  433. }
  434. case PVRSRV_BRIDGE_MAP_DEVICECLASS_MEMORY:
  435. {
  436. PVRSRV_BRIDGE_OUT_MAP_DEVICECLASS_MEMORY *psDeviceClassMemoryOUT =
  437. (PVRSRV_BRIDGE_OUT_MAP_DEVICECLASS_MEMORY *)psBridgePackageKM->pvParamOut;
  438. if (pvr_put_user(++g_ui64MemInfoID, &psDeviceClassMemoryOUT->sClientMemInfo.ui64Stamp) != 0)
  439. {
  440. err = -EFAULT;
  441. goto unlock_and_return;
  442. }
  443. break;
  444. }
  445. #endif /* defined(SUPPORT_MEMINFO_IDS) */
  446. default:
  447. break;
  448. }
  449. unlock_and_return:
  450. LinuxUnLockMutex(&gPVRSRVLock);
  451. return err;
  452. }