vg_lite_kernel.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  1. /****************************************************************************
  2. *
  3. * The MIT License (MIT)
  4. *
  5. * Copyright (c) 2014 - 2022 Vivante Corporation
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. *
  25. *****************************************************************************
  26. *
  27. * The GPL License (GPL)
  28. *
  29. * Copyright (C) 2014 - 2022 Vivante Corporation
  30. *
  31. * This program is free software; you can redistribute it and/or
  32. * modify it under the terms of the GNU General Public License
  33. * as published by the Free Software Foundation; either version 2
  34. * of the License, or (at your option) any later version.
  35. *
  36. * This program is distributed in the hope that it will be useful,
  37. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  39. * GNU General Public License for more details.
  40. *
  41. * You should have received a copy of the GNU General Public License
  42. * along with this program; if not, write to the Free Software Foundation,
  43. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  44. *
  45. *****************************************************************************
  46. *
  47. * Note: This software is released under dual MIT and GPL licenses. A
  48. * recipient may use this file under the terms of either the MIT license or
  49. * GPL License. If you wish to use only one license not the other, you can
  50. * indicate your decision by deleting one of the above license notices in your
  51. * version of this file.
  52. *
  53. *****************************************************************************/
  54. #include "vg_lite_platform.h"
  55. #include "vg_lite_kernel.h"
  56. #include "vg_lite_hal.h"
  57. #include "vg_lite_hw.h"
  58. #if defined(__linux__) && !defined(EMULATOR)
  59. #include <linux/sched.h>
  60. /*#include <asm/uaccess.h>*/
  61. #include <linux/uaccess.h>
  62. #include <linux/version.h>
  63. #include <linux/kernel.h>
  64. #include <linux/pm.h>
  65. #include <linux/suspend.h>
  66. #endif
  67. #if gcdVG_RECORD_HARDWARE_RUNNING_TIME
  68. #ifdef __linux__
  69. #include <linux/jiffies.h>
  70. unsigned long start_time, end_time;
  71. unsigned long period_time, total_time = 0;
  72. #else
  73. #include <sys/time.h>
  74. struct timeval start_time, end_time;
  75. unsigned long period_time, total_time = 0;
  76. #endif
  77. #endif
  78. #define FLEXA_TIMEOUT_STATE BIT(21)
  79. #define FLEXA_HANDSHEKE_FAIL_STATE BIT(22)
  80. #define MIN_TS_SIZE (8 << 10)
  81. #if gcdVG_ENABLE_BACKUP_COMMAND
  82. # define STATE_COMMAND(address) (0x30010000 | address)
  83. # define END_COMMAND(interrupt) (0x00000000 | interrupt)
  84. # define SEMAPHORE_COMMAND(id) (0x10000000 | id)
  85. # define STALL_COMMAND(id) (0x20000000 | id)
  86. static vg_lite_kernel_context_t global_power_context = {0};
  87. static uint32_t *power_context_klogical = NULL;
  88. static uint32_t state_map_table[4096];
  89. static uint32_t backup_command_buffer_physical;
  90. static void *backup_command_buffer_klogical;
  91. static uint32_t backup_command_buffer_size;
  92. uint32_t init_buffer[12];
  93. uint32_t is_init;
  94. size_t physical_address;
  95. #endif
  96. static int s_reference = 0;
  97. #if gcdVG_ENABLE_DELAY_RESUME
  98. static int delay_resume = 0;
  99. #endif
  100. #if gcdVG_ENABLE_GPU_RESET
  101. static uint32_t gpu_reset_count = 0;
  102. #endif
  103. static vg_lite_error_t do_terminate(vg_lite_kernel_terminate_t * data);
  104. static vg_lite_error_t vg_lite_kernel_vidmem_allocate(uint32_t *bytes, uint32_t flags, vg_lite_vidmem_pool_t pool, void **memory, void **kmemory, uint32_t *memory_gpu, void **memory_handle);
  105. static vg_lite_error_t vg_lite_kernel_vidmem_free(void *handle);
  106. static void soft_reset(void);
  107. static vg_lite_error_t do_wait(vg_lite_kernel_wait_t * data);
  108. #if gcdVG_ENABLE_BACKUP_COMMAND
  109. static vg_lite_error_t restore_gpu_state(void);
  110. static vg_lite_error_t restore_init_command(uint32_t physical, uint32_t size)
  111. {
  112. vg_lite_error_t error = VG_LITE_SUCCESS;
  113. vg_lite_uint32_t total_suspend_time = 0;
  114. vg_lite_uint32_t suspend_time_limit = 1000;
  115. /* flush cache. */
  116. vg_lite_hal_barrier();
  117. vg_lite_hal_poke(VG_LITE_HW_CMDBUF_ADDRESS, physical);
  118. vg_lite_hal_poke(VG_LITE_HW_CMDBUF_SIZE, (size + 7) / 8);
  119. while (!vg_lite_hal_peek(VG_LITE_INTR_STATUS)) {
  120. vg_lite_hal_delay(2);
  121. if (total_suspend_time < suspend_time_limit) {
  122. total_suspend_time += 2;
  123. } else {
  124. error = VG_LITE_TIMEOUT;
  125. break;
  126. }
  127. }
  128. vg_lite_hal_delay(2);
  129. return error;
  130. }
  131. #if gcdVG_ENABLE_GPU_RESET && gcdVG_ENABLE_BACKUP_COMMAND
  132. static vg_lite_error_t execute_command(uint32_t physical, uint32_t size, vg_lite_gpu_reset_type_t reset_type)
  133. {
  134. vg_lite_kernel_wait_t wait;
  135. vg_lite_error_t error = VG_LITE_SUCCESS;
  136. wait.timeout_ms = 1000;
  137. wait.event_mask = (uint32_t)~0;
  138. wait.reset_type = reset_type;
  139. /* flush cache. */
  140. vg_lite_hal_barrier();
  141. vg_lite_hal_poke(VG_LITE_HW_CMDBUF_ADDRESS, physical);
  142. vg_lite_hal_poke(VG_LITE_HW_CMDBUF_SIZE, (size + 7) / 8);
  143. error = do_wait(&wait);
  144. return error;
  145. }
  146. #endif
  147. static uint32_t push_command(uint32_t command, uint32_t data, uint32_t index)
  148. {
  149. uint32_t address = 0;
  150. if ((command & 0xFFFF0000) == 0x30010000) {
  151. address = command & 0x0000FFFF;
  152. state_map_table[address] = index;
  153. }
  154. if (NULL == power_context_klogical)
  155. power_context_klogical = global_power_context.power_context_klogical;
  156. power_context_klogical[index++] = command;
  157. power_context_klogical[index++] = data;
  158. return index;
  159. }
  160. static vg_lite_error_t backup_power_context_buffer(uint32_t *command_buffer_klogical, uint32_t size)
  161. {
  162. int index = 0;
  163. uint32_t command = 0;
  164. uint32_t address = 0;
  165. uint32_t context_index = 0;
  166. uint32_t data = 0;
  167. if (NULL == command_buffer_klogical) {
  168. return VG_LITE_INVALID_ARGUMENT;
  169. }
  170. for (index = 0; index < size; index++) {
  171. command = command_buffer_klogical[index];
  172. if (((command & 0xFFFF0000) == 0x30010000) && ((index % 2) == 0)) {
  173. data = command_buffer_klogical[index+1];
  174. address = command & 0x0000FFFF;
  175. context_index = state_map_table[address];
  176. if((address < 0) || (address > 4095))
  177. {
  178. vg_lite_kernel_print("Index out of bounds, wrong address and data 0x%08X 0x%08X\n", command , data);
  179. return VG_LITE_INVALID_ARGUMENT;
  180. }
  181. if (-1 != context_index) {
  182. power_context_klogical[context_index + 1] = data;
  183. } else {
  184. power_context_klogical[global_power_context.power_context_size / 4 + 0] = command;
  185. power_context_klogical[global_power_context.power_context_size / 4 + 1] = data;
  186. state_map_table[address] = global_power_context.power_context_size / 4;
  187. global_power_context.power_context_size += 8;
  188. }
  189. }
  190. }
  191. return VG_LITE_SUCCESS;
  192. }
  193. #endif
  194. static void gpu(int enable)
  195. {
  196. vg_lite_hw_clock_control_t value;
  197. uint32_t reset_timer = 2;
  198. const uint32_t reset_timer_limit = 1000;
  199. #if gcdVG_ENABLE_AUTO_CLOCK_GATING
  200. uint32_t data;
  201. #endif
  202. if (enable) {
  203. /* Enable clock gating. */
  204. value.data = vg_lite_hal_peek(VG_LITE_HW_CLOCK_CONTROL);
  205. value.control.clock_gate = 0;
  206. vg_lite_hal_poke(VG_LITE_HW_CLOCK_CONTROL, value.data);
  207. vg_lite_hal_delay(1);
  208. /* Set clock speed. */
  209. value.control.scale = 64;
  210. value.control.scale_load = 1;
  211. vg_lite_hal_poke(VG_LITE_HW_CLOCK_CONTROL, value.data);
  212. vg_lite_hal_delay(1);
  213. value.control.scale_load = 0;
  214. vg_lite_hal_poke(VG_LITE_HW_CLOCK_CONTROL, value.data);
  215. vg_lite_hal_delay(5);
  216. #if gcdVG_DUMP_DEBUG_REGISTER
  217. value.control.debug_registers = 0;
  218. vg_lite_hal_poke(VG_LITE_HW_CLOCK_CONTROL, value.data);
  219. #endif
  220. /* Perform a soft reset. */
  221. soft_reset();
  222. do {
  223. vg_lite_hal_delay(reset_timer);
  224. reset_timer *= 2; // If reset failed, try again with a longer wait. Need to check why if dead lopp happens here.
  225. } while (!VG_LITE_KERNEL_IS_GPU_IDLE());
  226. #if gcdVG_ENABLE_AUTO_CLOCK_GATING
  227. /* Enable Module Clock gating */
  228. data = vg_lite_hal_peek(VG_LITE_POWER_CONTROL);
  229. data |= 0x1;
  230. vg_lite_hal_poke(VG_LITE_POWER_CONTROL, data);
  231. vg_lite_hal_delay(1);
  232. #if !gcFEATURE_VG_CLOCK_GATING
  233. data = vg_lite_hal_peek(VG_LITE_POWER_MODULE_CONTROL);
  234. data |= 0x800;
  235. vg_lite_hal_poke(VG_LITE_POWER_MODULE_CONTROL, data);
  236. vg_lite_hal_delay(1);
  237. #endif
  238. #endif
  239. }
  240. else
  241. {
  242. while (!VG_LITE_KERNEL_IS_GPU_IDLE() &&
  243. (reset_timer < reset_timer_limit) // Force shutdown if timeout.
  244. ) {
  245. vg_lite_hal_delay(reset_timer);
  246. reset_timer *= 2;
  247. }
  248. /* Set idle speed. */
  249. value.data = vg_lite_hal_peek(VG_LITE_HW_CLOCK_CONTROL);
  250. value.control.scale = 1;
  251. value.control.scale_load = 1;
  252. vg_lite_hal_poke(VG_LITE_HW_CLOCK_CONTROL, value.data);
  253. vg_lite_hal_delay(1);
  254. value.control.scale_load = 0;
  255. vg_lite_hal_poke(VG_LITE_HW_CLOCK_CONTROL, value.data);
  256. vg_lite_hal_delay(5);
  257. /* Disable clock gating. */
  258. value.control.clock_gate = 1;
  259. vg_lite_hal_poke(VG_LITE_HW_CLOCK_CONTROL, value.data);
  260. vg_lite_hal_delay(1);
  261. }
  262. }
  263. /* Initialize some customized modeuls [DDRLess]. */
  264. static vg_lite_error_t init_3rd(vg_lite_kernel_initialize_t * data)
  265. {
  266. vg_lite_error_t error = VG_LITE_SUCCESS;
  267. /* TODO: Init the YUV<->RGB converters. Reserved for SOC. */
  268. /* vg_lite_hal_poke(0x00514, data->yuv_pre);
  269. vg_lite_hal_poke(0x00518, data->yuv_post);
  270. */
  271. return error;
  272. }
  273. static vg_lite_error_t init_vglite(vg_lite_kernel_initialize_t * data)
  274. {
  275. vg_lite_error_t error = VG_LITE_SUCCESS;
  276. vg_lite_kernel_context_t * context;
  277. vg_lite_uint32_t flags = 0, i;
  278. #if gcdVG_ENABLE_BACKUP_COMMAND
  279. vg_lite_uint32_t index;
  280. #endif
  281. #if defined(__linux__) && !defined(EMULATOR)
  282. vg_lite_kernel_context_t __user * context_usr;
  283. vg_lite_kernel_context_t mycontext = {
  284. .command_buffer = { 0 },
  285. .command_buffer_logical = { 0 },
  286. .command_buffer_klogical = { 0 },
  287. .command_buffer_physical = { 0 },
  288. };
  289. // Construct the context.
  290. context_usr = (vg_lite_kernel_context_t __user *) data->context;
  291. #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
  292. if (!access_ok(VERIFY_READ, context_usr, sizeof(*context_usr)) ||
  293. !access_ok(VERIFY_WRITE, context_usr, sizeof(*context_usr))) {
  294. #else
  295. if (!access_ok(context_usr, sizeof(*context_usr)) ||
  296. !access_ok(context_usr, sizeof(*context_usr))) {
  297. #endif
  298. /* Out of memory. */
  299. return VG_LITE_OUT_OF_MEMORY;
  300. }
  301. context = &mycontext;
  302. #else
  303. // Construct the context.
  304. context = data->context;
  305. if (context == NULL)
  306. {
  307. /* Out of memory. */
  308. return VG_LITE_OUT_OF_MEMORY;
  309. }
  310. #endif
  311. /* Zero out all pointers. */
  312. for (i = 0; i < CMDBUF_COUNT; i++) {
  313. context->command_buffer[i] = NULL;
  314. context->command_buffer_logical[i] = NULL;
  315. context->command_buffer_physical[i] = 0;
  316. }
  317. context->tess_buffer = NULL;
  318. context->tessbuf_logical = NULL;
  319. context->tessbuf_physical = 0;
  320. #if gcdVG_ENABLE_BACKUP_COMMAND
  321. global_power_context.power_context_logical = NULL;
  322. global_power_context.power_context_klogical = NULL;
  323. global_power_context.power_context_physical = 0;
  324. global_power_context.power_context = NULL;
  325. global_power_context.power_context_capacity = 32 << 10;
  326. global_power_context.power_context_size = 0;
  327. #endif
  328. /* Increment reference counter. */
  329. if (s_reference++ == 0) {
  330. /* Initialize the SOC. */
  331. vg_lite_hal_initialize();
  332. /* Enable the GPU. */
  333. gpu(1);
  334. }
  335. /* Fill in hardware capabilities. */
  336. data->capabilities.data = 0;
  337. /* Allocate the command buffer. */
  338. if (data->command_buffer_size) {
  339. for (i = 0; i < CMDBUF_COUNT; i ++)
  340. {
  341. /* Allocate the memory. */
  342. error = vg_lite_kernel_vidmem_allocate(&data->command_buffer_size,
  343. flags,
  344. data->command_buffer_pool,
  345. &context->command_buffer_logical[i],
  346. &context->command_buffer_klogical[i],
  347. &context->command_buffer_physical[i],
  348. &context->command_buffer[i]);
  349. if (error != VG_LITE_SUCCESS) {
  350. /* Free any allocated memory. */
  351. vg_lite_kernel_terminate_t terminate = { context };
  352. do_terminate(&terminate);
  353. /* Out of memory. */
  354. ONERROR(error);
  355. }
  356. /* Return command buffer logical pointer and GPU address. */
  357. data->command_buffer[i] = context->command_buffer_logical[i];
  358. data->command_buffer_gpu[i] = context->command_buffer_physical[i];
  359. }
  360. }
  361. #if gcdVG_ENABLE_BACKUP_COMMAND
  362. if (global_power_context.power_context_capacity) {
  363. /* Allocate the backup buffer. */
  364. error = vg_lite_kernel_vidmem_allocate(&global_power_context.power_context_capacity,
  365. flags,
  366. VG_LITE_POOL_RESERVED_MEMORY1,
  367. &global_power_context.power_context_logical,
  368. &global_power_context.power_context_klogical,
  369. &global_power_context.power_context_physical,
  370. &global_power_context.power_context);
  371. if (error != VG_LITE_SUCCESS) {
  372. /* Free any allocated memory. */
  373. vg_lite_kernel_terminate_t terminate = { &global_power_context };
  374. do_terminate(&terminate);
  375. /* Out of memory. */
  376. ONERROR(error);
  377. }
  378. /* Initialize power context buffer */
  379. for (i = 0; i < sizeof(state_map_table) / sizeof(state_map_table[0]); i++)
  380. state_map_table[i] = -1;
  381. #if (CHIPID==0x355 || CHIPID==0x255)
  382. index = push_command(STATE_COMMAND(0x0A30), 0x00000000, 0);
  383. index = push_command(STATE_COMMAND(0x0A31), 0x00000000, index);
  384. index = push_command(STATE_COMMAND(0x0A32), 0x00000000, index);
  385. index = push_command(STATE_COMMAND(0x0A33), 0x00000000, index);
  386. index = push_command(STATE_COMMAND(0x0A35), 0x00000000, index);
  387. index = push_command(STATE_COMMAND(0x0A36), 0x00000000, index);
  388. index = push_command(STATE_COMMAND(0x0A37), 0x00000000, index);
  389. index = push_command(STATE_COMMAND(0x0A38), 0x00000000, index);
  390. index = push_command(STATE_COMMAND(0x0A3A), 0x00000000, index);
  391. index = push_command(STATE_COMMAND(0x0A3D), 0x00000000, index);
  392. #else
  393. index = push_command(STATE_COMMAND(0x0A35), 0x00000000, 0);
  394. index = push_command(STATE_COMMAND(0x0AC8), 0x00000000, index);
  395. index = push_command(STATE_COMMAND(0x0ACB), 0x00000000, index);
  396. index = push_command(STATE_COMMAND(0x0ACC), 0x00000000, index);
  397. #endif
  398. index = push_command(STATE_COMMAND(0x0A90), 0x00000000, index);
  399. index = push_command(STATE_COMMAND(0x0A91), 0x00000000, index);
  400. index = push_command(STATE_COMMAND(0x0A92), 0x00000000, index);
  401. index = push_command(STATE_COMMAND(0x0A93), 0x00000000, index);
  402. index = push_command(STATE_COMMAND(0x0A94), 0x00000000, index);
  403. index = push_command(STATE_COMMAND(0x0A95), 0x00000000, index);
  404. index = push_command(STATE_COMMAND(0x0A96), 0x00000000, index);
  405. index = push_command(STATE_COMMAND(0x0A97), 0x00000000, index);
  406. index = push_command(STATE_COMMAND(0x0A10), 0x00000000, index);
  407. index = push_command(STATE_COMMAND(0x0AC8), 0x00000000, index);
  408. index = push_command(STATE_COMMAND(0x0AC8), 0x00000000, index);
  409. index = push_command(STATE_COMMAND(0x0A5C), 0x00000000, index);
  410. index = push_command(STATE_COMMAND(0x0A5D), 0x00000000, index);
  411. index = push_command(STATE_COMMAND(0x0A11), 0x00000000, index);
  412. index = push_command(STATE_COMMAND(0x0A12), 0x00000000, index);
  413. index = push_command(STATE_COMMAND(0x0A13), 0x00000000, index);
  414. global_power_context.power_context_size = index * 4;
  415. }
  416. #endif
  417. /* Allocate the tessellation buffer. */
  418. if ((data->tess_width > 0) && (data->tess_height > 0))
  419. {
  420. int width = data->tess_width;
  421. int height = 0;
  422. int vg_countbuffer_size = 0, total_size = 0, ts_buffer_size = 0;
  423. height = VG_LITE_ALIGN(data->tess_height, 16);
  424. #if (CHIPID==0x355 || CHIPID==0x255)
  425. {
  426. unsigned long stride, buffer_size, l1_size, l2_size;
  427. #if (CHIPID==0x355)
  428. data->capabilities.cap.l2_cache = 1;
  429. width = VG_LITE_ALIGN(width, 128);
  430. #endif
  431. /* Check if we can used tiled tessellation (128x16). */
  432. if (((width & 127) == 0) && ((height & 15) == 0)) {
  433. data->capabilities.cap.tiled = 0x3;
  434. } else {
  435. data->capabilities.cap.tiled = 0x2;
  436. }
  437. /* Compute tessellation buffer size. */
  438. stride = VG_LITE_ALIGN(width * 8, 64);
  439. buffer_size = VG_LITE_ALIGN(stride * height, 64);
  440. /* Each bit in the L1 cache represents 64 bytes of tessellation data. */
  441. l1_size = VG_LITE_ALIGN(VG_LITE_ALIGN(buffer_size / 64, 64) / 8, 64);
  442. #if (CHIPID==0x355)
  443. /* Each bit in the L2 cache represents 32 bytes of L1 data. */
  444. l2_size = VG_LITE_ALIGN(VG_LITE_ALIGN(l1_size / 32, 64) / 8, 64);
  445. #else
  446. l2_size = 0;
  447. #endif
  448. total_size = buffer_size + l1_size + l2_size;
  449. ts_buffer_size = buffer_size;
  450. }
  451. #else /* (CHIPID==0x355 || CHIPID==0x255) */
  452. {
  453. /* Check if we can used tiled tessellation (128x16). */
  454. if (((width & 127) == 0) && ((height & 15) == 0)) {
  455. data->capabilities.cap.tiled = 0x3;
  456. }
  457. else {
  458. data->capabilities.cap.tiled = 0x2;
  459. }
  460. vg_countbuffer_size = (height + 13) / 14;
  461. vg_countbuffer_size = vg_countbuffer_size * 64;
  462. total_size = height * 128 + vg_countbuffer_size;
  463. if (total_size < MIN_TS_SIZE)
  464. total_size = MIN_TS_SIZE;
  465. ts_buffer_size = total_size - vg_countbuffer_size;
  466. }
  467. #endif /* (CHIPID==0x355 || CHIPID==0x255) */
  468. /* Allocate the memory. */
  469. error = vg_lite_kernel_vidmem_allocate((uint32_t*)&total_size,
  470. flags,
  471. data->tess_buffer_pool,
  472. &context->tessbuf_logical,
  473. &context->tessbuf_klogical,
  474. &context->tessbuf_physical,
  475. &context->tess_buffer);
  476. if (error != VG_LITE_SUCCESS) {
  477. /* Free any allocated memory. */
  478. vg_lite_kernel_terminate_t terminate = { context };
  479. do_terminate(&terminate);
  480. /* Out of memory. */
  481. ONERROR(error);
  482. }
  483. /* Return the tessellation buffer pointers and GPU addresses. */
  484. data->physical_addr = context->tessbuf_physical;
  485. data->logical_addr = (uint8_t *)context->tessbuf_logical;
  486. data->tessbuf_size = ts_buffer_size;
  487. data->countbuf_size = vg_countbuffer_size;
  488. data->tess_w_h = width | (height << 16);
  489. }
  490. #if gcdVG_ENABLE_GPU_RESET
  491. gpu_reset_count = 0;
  492. #endif
  493. vg_lite_set_gpu_execute_state(VG_LITE_GPU_STOP);
  494. /* Enable all interrupts. */
  495. vg_lite_hal_poke(VG_LITE_INTR_ENABLE, 0xFFFFFFFF);
  496. #if defined(__linux__) && !defined(EMULATOR)
  497. if (copy_to_user(context_usr, context, sizeof(vg_lite_kernel_context_t)) != 0) {
  498. // Free any allocated memory.
  499. vg_lite_kernel_terminate_t terminate = { context };
  500. do_terminate(&terminate);
  501. return VG_LITE_NO_CONTEXT;
  502. }
  503. #endif
  504. on_error:
  505. return error;
  506. }
  507. static vg_lite_error_t do_initialize(vg_lite_kernel_initialize_t * data)
  508. {
  509. vg_lite_error_t error = VG_LITE_SUCCESS;
  510. /* Free any allocated memory for the context. */
  511. do {
  512. error = init_vglite(data);
  513. if (error != VG_LITE_SUCCESS)
  514. break;
  515. error = init_3rd(data);
  516. if (error != VG_LITE_SUCCESS)
  517. break;
  518. } while (0);
  519. return error;
  520. }
  521. static vg_lite_error_t terminate_vglite(vg_lite_kernel_terminate_t * data)
  522. {
  523. vg_lite_kernel_context_t *context = NULL;
  524. #if defined(__linux__) && !defined(EMULATOR)
  525. vg_lite_kernel_context_t mycontext = {
  526. .command_buffer = { 0 },
  527. .command_buffer_logical = { 0 },
  528. .command_buffer_klogical = { 0 },
  529. .command_buffer_physical = { 0 },
  530. };
  531. if (copy_from_user(&mycontext, data->context, sizeof(vg_lite_kernel_context_t)) != 0) {
  532. return VG_LITE_NO_CONTEXT;
  533. }
  534. context = &mycontext;
  535. #else
  536. context = data->context;
  537. #endif
  538. /* Free any allocated memory for the context. */
  539. if (context->command_buffer[0]) {
  540. /* Free the command buffer. */
  541. vg_lite_kernel_vidmem_free(context->command_buffer[0]);
  542. context->command_buffer[0] = NULL;
  543. }
  544. #if !gcFEATURE_VG_SINGLE_COMMAND_BUFFER
  545. if (context->command_buffer[1]) {
  546. /* Free the command buffer. */
  547. vg_lite_kernel_vidmem_free(context->command_buffer[1]);
  548. context->command_buffer[1] = NULL;
  549. }
  550. #endif
  551. #if gcdVG_ENABLE_BACKUP_COMMAND
  552. if (global_power_context.power_context) {
  553. /* Free the power context. */
  554. vg_lite_kernel_vidmem_free(global_power_context.power_context);
  555. global_power_context.power_context = NULL;
  556. }
  557. #endif
  558. if (context->tess_buffer) {
  559. /* Free the tessellation buffer. */
  560. vg_lite_kernel_vidmem_free(context->tess_buffer);
  561. context->tess_buffer = NULL;
  562. }
  563. vg_lite_hal_free_os_heap();
  564. /* Decrement reference counter. */
  565. if (--s_reference == 0) {
  566. /* Disable the GPU. */
  567. gpu(0);
  568. /* De-initialize the SOC. */
  569. vg_lite_hal_deinitialize();
  570. }
  571. #if defined(__linux__) && !defined(EMULATOR)
  572. if (copy_to_user((vg_lite_kernel_context_t __user *) data->context,
  573. &mycontext, sizeof(vg_lite_kernel_context_t)) != 0) {
  574. return VG_LITE_NO_CONTEXT;
  575. }
  576. #endif
  577. return VG_LITE_SUCCESS;
  578. }
  579. static vg_lite_error_t terminate_3rd(vg_lite_kernel_terminate_t * data)
  580. {
  581. /* TODO: Terminate the converters. */
  582. return VG_LITE_SUCCESS;
  583. }
  584. static vg_lite_error_t do_terminate(vg_lite_kernel_terminate_t * data)
  585. {
  586. terminate_vglite(data);
  587. terminate_3rd(data);
  588. return VG_LITE_SUCCESS;
  589. }
  590. static vg_lite_error_t vg_lite_kernel_vidmem_allocate(uint32_t *bytes, uint32_t flags, vg_lite_vidmem_pool_t pool, void **memory, void **kmemory, uint32_t *memory_gpu, void **memory_handle)
  591. {
  592. vg_lite_error_t error = VG_LITE_SUCCESS;
  593. error = vg_lite_hal_allocate_contiguous(*bytes, pool, memory, kmemory, memory_gpu, memory_handle);
  594. if (VG_IS_ERROR(error)) {
  595. ONERROR(error);
  596. }
  597. return error;
  598. on_error:
  599. return error;
  600. }
  601. static vg_lite_error_t vg_lite_kernel_vidmem_free(void *handle)
  602. {
  603. vg_lite_error_t error = VG_LITE_SUCCESS;
  604. vg_lite_hal_free_contiguous(handle);
  605. return error;
  606. }
  607. static vg_lite_error_t do_allocate(vg_lite_kernel_allocate_t * data)
  608. {
  609. vg_lite_error_t error = VG_LITE_SUCCESS;
  610. error = vg_lite_kernel_vidmem_allocate(&data->bytes, data->flags, data->pool, &data->memory, &data->kmemory, &data->memory_gpu, &data->memory_handle);
  611. return error;
  612. }
  613. static vg_lite_error_t do_free(vg_lite_kernel_free_t * data)
  614. {
  615. vg_lite_error_t error = VG_LITE_SUCCESS;
  616. error = vg_lite_kernel_vidmem_free(data->memory_handle);
  617. return error;
  618. }
  619. static vg_lite_error_t do_submit(vg_lite_kernel_submit_t * data)
  620. {
  621. uint32_t offset;
  622. vg_lite_kernel_context_t *context = NULL;
  623. uint32_t physical = data->context->command_buffer_physical[data->command_id];
  624. #if defined(__linux__) && !defined(EMULATOR)
  625. vg_lite_kernel_context_t mycontext = {
  626. .command_buffer = { 0 },
  627. .command_buffer_logical = { 0 },
  628. .command_buffer_klogical = { 0 },
  629. .command_buffer_physical = { 0 },
  630. };
  631. if (copy_from_user(&mycontext, data->context, sizeof(vg_lite_kernel_context_t)) != 0) {
  632. return VG_LITE_NO_CONTEXT;
  633. }
  634. context = &mycontext;
  635. physical = context->command_buffer_physical[data->command_id];
  636. #else
  637. context = data->context;
  638. if (context == NULL)
  639. {
  640. return VG_LITE_NO_CONTEXT;
  641. }
  642. #endif
  643. /* Perform a memory barrier. */
  644. vg_lite_hal_barrier();
  645. offset = (uint8_t *) data->commands - (uint8_t *)context->command_buffer_logical[data->command_id];
  646. #if gcdVG_ENABLE_BACKUP_COMMAND
  647. backup_power_context_buffer((uint32_t *)((uint8_t *)context->command_buffer_klogical[data->command_id] + offset), (data->command_size + 3) / 4);
  648. backup_command_buffer_physical = physical + offset;
  649. backup_command_buffer_klogical = (uint32_t *)((uint8_t *)context->command_buffer_klogical[data->command_id] + offset);
  650. backup_command_buffer_size = data->command_size;
  651. #endif
  652. #if gcdVG_RECORD_HARDWARE_RUNNING_TIME
  653. #ifdef __linux__
  654. start_time = jiffies;
  655. #else
  656. gettimeofday(&start_time, NULL);
  657. #endif
  658. #endif
  659. /* set gpu to busy state */
  660. vg_lite_set_gpu_execute_state(VG_LITE_GPU_RUN);
  661. /* Write the registers to kick off the command execution (CMDBUF_SIZE). */
  662. vg_lite_hal_poke(VG_LITE_HW_CMDBUF_ADDRESS, physical + offset);
  663. vg_lite_hal_poke(VG_LITE_HW_CMDBUF_SIZE, (data->command_size + 7) / 8);
  664. return VG_LITE_SUCCESS;
  665. }
  666. #if gcdVG_ENABLE_DUMP_COMMAND && gcdVG_ENABLE_BACKUP_COMMAND
  667. static void dump_last_frame(void)
  668. {
  669. uint32_t *ptr = backup_command_buffer_klogical;
  670. uint32_t size = backup_command_buffer_size;
  671. uint32_t i = 0;
  672. uint32_t data = 0;
  673. vg_lite_kernel_print("This is init command buffer:\n");
  674. vg_lite_kernel_print("@[%s 0x%08X 0x00000088\n", "command", physical_address);
  675. vg_lite_kernel_print(" 0x30010A35 0x%08X 0x30010AC8 0x%08X\n", init_buffer[0], init_buffer[1]);
  676. vg_lite_kernel_print(" 0x30010ACB 0x%08X 0x30010ACC 0x%08X\n", init_buffer[2], init_buffer[3]);
  677. vg_lite_kernel_print(" 0x30010A90 0x%08X 0x30010A91 0x%08X\n", init_buffer[4], init_buffer[5]);
  678. vg_lite_kernel_print(" 0x30010A92 0x%08X 0x30010A93 0x%08X\n", init_buffer[6], init_buffer[7]);
  679. vg_lite_kernel_print(" 0x30010A94 0x%08X 0x30010A95 0x%08X\n", init_buffer[8], init_buffer[9]);
  680. vg_lite_kernel_print(" 0x30010A96 0x%08X 0x30010A97 0x%08X\n", init_buffer[10], init_buffer[11]);
  681. vg_lite_kernel_print(" 0x30010A00 0x00000001 0x30010A1B 0x00000011\n");
  682. vg_lite_kernel_print(" 0x10000007 0x00000000 0x20000007 0x00000000\n");
  683. vg_lite_kernel_print(" 0x00000000 0x00000000\n");
  684. vg_lite_kernel_print("] -- %s\n", "command");
  685. if (is_init == 1)
  686. {
  687. vg_lite_kernel_print("the last submit command is init command.\n");
  688. }
  689. else
  690. {
  691. vg_lite_kernel_print("the last submit command before hang:\n");
  692. vg_lite_kernel_print( "@[%s 0x%08X 0x%08X\n", "command", backup_command_buffer_klogical, size);
  693. for (i = 0; i < size; i += 4) {
  694. vg_lite_kernel_print(" 0x%08X 0x%08X 0x%08X 0x%08X\n", ptr[i], ptr[i + 1], ptr[i + 2], ptr[i + 3]);
  695. }
  696. if (size % 16)
  697. {
  698. int j = size % 16 / 4;
  699. switch (j)
  700. {
  701. case 1:
  702. vg_lite_kernel_print(" 0x%08X\n", ptr[(size - size % 16) / 4]);
  703. break;
  704. case 2:
  705. vg_lite_kernel_print(" 0x%08X 0x%08X\n", ptr[(size - size % 16) / 4], ptr[(size - size % 16) / 4 + 1]);
  706. break;
  707. case 3:
  708. vg_lite_kernel_print(" 0x%08X 0x%08X 0x%08X\n", ptr[(size - size % 16) / 4], ptr[(size - size % 16) / 4 + 1], ptr[(size - size % 16) / 4 + 2]);
  709. break;
  710. default:
  711. break;
  712. }
  713. }
  714. }
  715. vg_lite_kernel_print("] -- %s\n", "command");
  716. data = vg_lite_hal_peek(VG_LITE_HW_IDLE);
  717. vg_lite_kernel_print("vgidle reg = 0x%08X\n", data);
  718. }
  719. #endif
  720. static vg_lite_error_t do_wait(vg_lite_kernel_wait_t * data)
  721. {
  722. #if gcdVG_ENABLE_GPU_RESET && gcdVG_ENABLE_BACKUP_COMMAND
  723. vg_lite_error_t error = VG_LITE_SUCCESS;
  724. #endif
  725. /* Wait for interrupt. */
  726. #if gcdVG_DUMP_DEBUG_REGISTER
  727. if (!vg_lite_hal_wait_interrupt(5000, data->event_mask, &data->event_got)) {
  728. /* Timeout. */
  729. unsigned int debug;
  730. unsigned int iter;
  731. debug = vg_lite_hal_peek(VG_LITE_HW_IDLE);
  732. vg_lite_kernel_print("idle = 0x%x\n",debug);
  733. debug = vg_lite_hal_peek(VG_LITE_HW_CLOCK_CONTROL);
  734. vg_lite_kernel_print("QAHiClockControl = 0x%x\n", debug);
  735. for(iter =0; iter < 16 ; iter ++)
  736. {
  737. vg_lite_hal_poke(0x470, iter);
  738. debug = vg_lite_hal_peek(0x448);
  739. vg_lite_kernel_print("0x448[%d] = 0x%x\n", iter, debug);
  740. }
  741. for(iter =0; iter < 16 ; iter ++)
  742. {
  743. vg_lite_hal_poke(0x470, iter<<8);
  744. debug = vg_lite_hal_peek(0x44C);
  745. vg_lite_kernel_print("0x44c[%d] = 0x%x\n", iter, debug);
  746. }
  747. for(iter =0; iter < 28 ; iter ++)
  748. {
  749. vg_lite_hal_poke(0x470, iter<<16);
  750. debug = vg_lite_hal_peek(0x450);
  751. vg_lite_kernel_print("0x450[%d] = 0x%x\n", iter, debug);
  752. }
  753. for (iter = 0; iter < 31; iter++)
  754. {
  755. vg_lite_hal_poke(0x470, iter<<24);
  756. debug = vg_lite_hal_peek(0x454);
  757. vg_lite_kernel_print("0x454[%d] = 0x%x\n", iter, debug);
  758. }
  759. for (iter = 128; iter < 133; iter++)
  760. {
  761. vg_lite_hal_poke(0x470, iter<<24);
  762. debug = vg_lite_hal_peek(0x454);
  763. vg_lite_kernel_print("0x454[%d] = 0x%x\n", iter, debug);
  764. }
  765. for (iter = 0; iter < 21; iter++)
  766. {
  767. vg_lite_hal_poke(0x474, iter);
  768. debug = vg_lite_hal_peek(0x458);
  769. vg_lite_kernel_print("0x458[%d] = 0x%x\n", iter, debug);
  770. }
  771. for (iter = 0; iter < 62; iter++)
  772. {
  773. vg_lite_hal_poke(0x474, iter<<8);
  774. debug = vg_lite_hal_peek(0x45C);
  775. vg_lite_kernel_print("0x45C[%d] = 0x%x\n", iter, debug);
  776. }
  777. for (iter = 0; iter < 16; iter++)
  778. {
  779. vg_lite_hal_poke(0x474, iter<<16);
  780. debug = vg_lite_hal_peek(0x460);
  781. vg_lite_kernel_print("0x460[%d] = 0x%x\n", iter, debug);
  782. }
  783. for (iter = 0x40; iter <= 0x60; iter+=4)
  784. {
  785. debug = vg_lite_hal_peek(iter);
  786. vg_lite_kernel_print("0x%x = 0x%x\n", iter, debug);
  787. }
  788. debug = vg_lite_hal_peek(0x438);
  789. vg_lite_kernel_print("0x%x = 0x%x\n", 0x438, debug);
  790. debug = vg_lite_hal_peek(0x43C);
  791. vg_lite_kernel_print("0x%x = 0x%x\n", 0x43C, debug);
  792. debug = vg_lite_hal_peek(0x440);
  793. vg_lite_kernel_print("0x%x = 0x%x\n", 0x440, debug);
  794. debug = vg_lite_hal_peek(0x444);
  795. vg_lite_kernel_print("0x%x = 0x%x\n", 0x444, debug);
  796. debug = vg_lite_hal_peek(0x500);
  797. vg_lite_kernel_print("0x%x = 0x%x\n", 0x500, debug);
  798. debug = vg_lite_hal_peek(0x504);
  799. vg_lite_kernel_print("0x%x = 0x%x\n", 0x504, debug);
  800. debug = vg_lite_hal_peek(0x508);
  801. vg_lite_kernel_print("0x%x = 0x%x\n", 0x508, debug);
  802. debug = vg_lite_hal_peek(0x10);
  803. vg_lite_kernel_print("0x%x = 0x%x\n", 0x10, debug);
  804. for (iter = 0x14; iter <= 0x34; iter += 4)
  805. {
  806. debug = vg_lite_hal_peek(iter);
  807. vg_lite_kernel_print("0x%x = 0x%08x\n", iter, debug);
  808. }
  809. debug = vg_lite_hal_peek(0x98);
  810. vg_lite_kernel_print("0x%x = 0x%08x\n", 0x98, debug);
  811. debug = vg_lite_hal_peek(0xA4);
  812. vg_lite_kernel_print("0x%x = 0x%08x\n", 0xA4, debug);
  813. debug = vg_lite_hal_peek(0xA8);
  814. vg_lite_kernel_print("0x%x = 0x%08x\n", 0xA8, debug);
  815. debug = vg_lite_hal_peek(0xE8);
  816. vg_lite_kernel_print("0x%x = 0x%08x\n", 0xE8, debug);
  817. #if gcdVG_ENABLE_DUMP_COMMAND && gcdVG_ENABLE_BACKUP_COMMAND
  818. dump_last_frame();
  819. #endif
  820. return VG_LITE_TIMEOUT;
  821. }
  822. #else
  823. if (!vg_lite_hal_wait_interrupt(data->timeout_ms, data->event_mask, &data->event_got)) {
  824. /* Timeout. */
  825. #if gcdVG_ENABLE_DUMP_COMMAND && gcdVG_ENABLE_BACKUP_COMMAND
  826. dump_last_frame();
  827. #endif
  828. #if gcdVG_ENABLE_GPU_RESET && gcdVG_ENABLE_BACKUP_COMMAND
  829. gpu_reset_count++;
  830. if (gpu_reset_count <= 1) {
  831. if (data->reset_type == RESTORE_INIT_COMMAND) {
  832. error = VG_LITE_SUCCESS;
  833. } else if (data->reset_type == RESTORE_LAST_COMMAND) {
  834. error = VG_LITE_SUCCESS;
  835. } else if (data->reset_type == RESTORE_ALL_COMMAND){
  836. /* reset and enable the GPU interrupt */
  837. gpu(1);
  838. vg_lite_hal_poke(VG_LITE_INTR_ENABLE, 0xFFFFFFFF);
  839. /* restore gpu state */
  840. error = execute_command(global_power_context.power_context_physical, global_power_context.power_context_size + 32,
  841. RESTORE_INIT_COMMAND);
  842. error = execute_command(backup_command_buffer_physical, backup_command_buffer_size, RESTORE_LAST_COMMAND);
  843. } else {
  844. error = VG_LITE_TIMEOUT;
  845. }
  846. gpu_reset_count = 0;
  847. return error;
  848. }
  849. vg_lite_kernel_print("GPU reset fail!\n");
  850. #endif
  851. return VG_LITE_TIMEOUT;
  852. }
  853. #endif
  854. #if gcFEATURE_VG_FLEXA
  855. if (data->event_got & FLEXA_TIMEOUT_STATE)
  856. return VG_LITE_FLEXA_TIME_OUT;
  857. if (data->event_got & FLEXA_HANDSHEKE_FAIL_STATE)
  858. return VG_LITE_FLEXA_HANDSHAKE_FAIL;
  859. #endif
  860. /* set gpu to idle state */
  861. vg_lite_set_gpu_execute_state(VG_LITE_GPU_STOP);
  862. return VG_LITE_SUCCESS;
  863. }
  864. #if gcdVG_ENABLE_BACKUP_COMMAND
  865. static vg_lite_error_t restore_gpu_state(void)
  866. {
  867. vg_lite_error_t error = VG_LITE_SUCCESS;
  868. int i = 0;
  869. uint32_t total_size = 0;
  870. power_context_klogical[global_power_context.power_context_size / 4 + 0] = 0x30010A1B;
  871. power_context_klogical[global_power_context.power_context_size / 4 + 1] = 0x00000001;
  872. power_context_klogical[global_power_context.power_context_size / 4 + 2] = 0x10000007;
  873. power_context_klogical[global_power_context.power_context_size / 4 + 3] = 0x00000000;
  874. power_context_klogical[global_power_context.power_context_size / 4 + 4] = 0x20000007;
  875. power_context_klogical[global_power_context.power_context_size / 4 + 5] = 0x00000000;
  876. power_context_klogical[global_power_context.power_context_size / 4 + 6] = 0x00000000;
  877. power_context_klogical[global_power_context.power_context_size / 4 + 7] = 0x00000000;
  878. total_size = global_power_context.power_context_size + 32;
  879. vg_lite_kernel_print("after resume and the power_context is:\n");
  880. for (i = 0; i < total_size / 4; i += 4) {
  881. vg_lite_kernel_print("0x%08X 0x%08X",
  882. power_context_klogical[i], power_context_klogical[i + 1]);
  883. if ((i + 2) <= (total_size / 4 - 1))
  884. #if defined(__linux__)
  885. vg_lite_kernel_print(KERN_CONT " 0x%08X 0x%08X\n",
  886. power_context_klogical[i + 2], power_context_klogical[i + 3]);
  887. #else
  888. vg_lite_kernel_print(" 0x%08X 0x%08X\n",
  889. power_context_klogical[i + 2], power_context_klogical[i + 3]);
  890. #endif
  891. }
  892. vg_lite_kernel_print("global_power_context size = %d\n", total_size);
  893. /* submit the backup power context */
  894. error = restore_init_command(global_power_context.power_context_physical, total_size);
  895. if (error == VG_LITE_SUCCESS)
  896. vg_lite_kernel_print("Initialize the GPU state success!\n");
  897. /* submit last frame before suspend */
  898. /*error = restore_init_command(backup_command_buffer_physical, backup_command_buffer_size);
  899. if (error == VG_LITE_SUCCESS)
  900. vg_lite_kernel_print("Initialize the GPU state success!\n");*/
  901. return error;
  902. }
  903. #endif
  904. static vg_lite_error_t do_reset(void)
  905. {
  906. /* reset and enable the GPU interrupt */
  907. gpu(1);
  908. #if gcdVG_ENABLE_BACKUP_COMMAND
  909. restore_gpu_state();
  910. #endif
  911. vg_lite_hal_poke(VG_LITE_INTR_ENABLE, 0xFFFFFFFF);
  912. return VG_LITE_SUCCESS;
  913. }
  914. static vg_lite_error_t do_gpu_close(void)
  915. {
  916. gpu(0);
  917. vg_lite_kernel_hintmsg("gpu is shutdown!\n");
  918. return VG_LITE_SUCCESS;
  919. }
  920. static vg_lite_error_t do_debug(void)
  921. {
  922. return VG_LITE_SUCCESS;
  923. }
  924. static vg_lite_error_t do_map(vg_lite_kernel_map_t * data)
  925. {
  926. data->memory_handle = vg_lite_hal_map(data->flags, data->bytes, data->logical, data->physical, data->dma_buf_fd, &data->memory_gpu);
  927. if (data->memory_handle == NULL)
  928. return VG_LITE_OUT_OF_RESOURCES;
  929. else if ((long)data->memory_handle == (long)-1)
  930. return VG_LITE_NOT_SUPPORT;
  931. else
  932. return VG_LITE_SUCCESS;
  933. }
  934. static vg_lite_error_t do_unmap(vg_lite_kernel_unmap_t * data)
  935. {
  936. vg_lite_hal_unmap(data->memory_handle);
  937. return VG_LITE_SUCCESS;
  938. }
  939. static vg_lite_error_t do_peek(vg_lite_kernel_info_t * data)
  940. {
  941. data->reg = vg_lite_hal_peek(data->addr);
  942. return VG_LITE_SUCCESS;
  943. }
  944. #if gcFEATURE_VG_FLEXA
  945. static vg_lite_error_t do_flexa_enable(vg_lite_kernel_flexa_info_t * data)
  946. {
  947. /* reset all flexa states */
  948. vg_lite_hal_poke(0x03600, 0x0);
  949. /* set sync mode */
  950. vg_lite_hal_poke(0x03604, data->segment_address);
  951. vg_lite_hal_poke(0x03608, data->segment_count);
  952. vg_lite_hal_poke(0x0360C, data->segment_size);
  953. vg_lite_hal_poke(0x0520, data->sync_mode);
  954. vg_lite_hal_poke(0x03610, data->stream_id | data->sbi_mode | data->start_flag | data->stop_flag | data->reset_flag);
  955. return VG_LITE_SUCCESS;
  956. }
  957. static vg_lite_error_t do_flexa_set_background_address(vg_lite_kernel_flexa_info_t * data)
  958. {
  959. vg_lite_hal_poke(0x03604, data->segment_address);
  960. vg_lite_hal_poke(0x03608, data->segment_count);
  961. vg_lite_hal_poke(0x0360C, data->segment_size);
  962. vg_lite_hal_poke(0x03610, data->stream_id | data->sbi_mode | data->start_flag | data->stop_flag | data->reset_flag);
  963. return VG_LITE_SUCCESS;
  964. }
  965. static vg_lite_error_t do_flexa_disable(vg_lite_kernel_flexa_info_t * data)
  966. {
  967. vg_lite_hal_poke(0x0520, data->sync_mode);
  968. vg_lite_hal_poke(0x03610, data->stream_id | data->sbi_mode);
  969. /* reset all flexa states */
  970. vg_lite_hal_poke(0x03600, 0x0);
  971. return VG_LITE_SUCCESS;
  972. }
  973. static vg_lite_error_t do_flexa_stop_frame(vg_lite_kernel_flexa_info_t * data)
  974. {
  975. vg_lite_hal_poke(0x03610, data->stream_id | data->sbi_mode | data->start_flag | data->stop_flag | data->reset_flag);
  976. return VG_LITE_SUCCESS;
  977. }
  978. #endif
  979. static vg_lite_error_t do_query_mem(vg_lite_kernel_mem_t * data)
  980. {
  981. vg_lite_error_t error = VG_LITE_SUCCESS;
  982. error = vg_lite_hal_query_mem(data);
  983. return error;
  984. }
  985. #if gcdVG_ENABLE_DELAY_RESUME
  986. static vg_lite_error_t set_delay_resume(vg_lite_kernel_delay_resume_t* data)
  987. {
  988. delay_resume = data->set_delay_resume;
  989. return VG_LITE_SUCCESS;
  990. }
  991. static int do_query_delay_resume(void)
  992. {
  993. if (delay_resume == 1)
  994. {
  995. /* Reset delay resume to 0 after query*/
  996. delay_resume = 0;
  997. return 1;
  998. }
  999. else
  1000. return 0;
  1001. }
  1002. static int set_gpu_clock_state(vg_lite_kernel_gpu_clock_state_t* gpu_state)
  1003. {
  1004. #ifdef __ZEPHYR__
  1005. vg_lite_gpu_execute_state_t state = gpu_state->state;
  1006. vg_lite_set_gpu_clock_state(state);
  1007. #endif
  1008. return VG_LITE_SUCCESS;
  1009. }
  1010. #endif
  1011. static vg_lite_error_t do_map_memory(vg_lite_kernel_map_memory_t * data)
  1012. {
  1013. vg_lite_error_t error = VG_LITE_SUCCESS;
  1014. error = vg_lite_hal_map_memory(data);
  1015. return error;
  1016. }
  1017. static vg_lite_error_t do_unmap_memory(vg_lite_kernel_unmap_memory_t * data)
  1018. {
  1019. vg_lite_error_t error = VG_LITE_SUCCESS;
  1020. error = vg_lite_hal_unmap_memory(data);
  1021. return error;
  1022. }
  1023. static vg_lite_error_t do_cache(vg_lite_kernel_cache_t * data)
  1024. {
  1025. vg_lite_error_t error = VG_LITE_SUCCESS;
  1026. error = vg_lite_hal_operation_cache(data->memory_handle, data->cache_op);
  1027. return error;
  1028. }
  1029. static vg_lite_error_t do_export_memory(vg_lite_kernel_export_memory_t * data)
  1030. {
  1031. vg_lite_error_t error = VG_LITE_SUCCESS;
  1032. error = vg_lite_hal_memory_export(&data->fd);
  1033. return error;
  1034. }
  1035. static vg_lite_error_t do_get_running_time(vg_lite_kernel_hardware_running_time_t * data)
  1036. {
  1037. vg_lite_error_t error = VG_LITE_SUCCESS;
  1038. #if gcdVG_RECORD_HARDWARE_RUNNING_TIME
  1039. #ifdef __linux__
  1040. data->run_time = total_time;
  1041. data->hertz = HZ;
  1042. #else
  1043. data->run_time = total_time;
  1044. data->hertz = 1e6;
  1045. #endif
  1046. #endif
  1047. return error;
  1048. }
  1049. vg_lite_error_t record_running_time(void)
  1050. {
  1051. vg_lite_error_t error = VG_LITE_SUCCESS;
  1052. #if gcdVG_RECORD_HARDWARE_RUNNING_TIME
  1053. #ifdef __linux__
  1054. end_time = jiffies;
  1055. period_time = end_time - start_time;
  1056. total_time += period_time;
  1057. #else
  1058. gettimeofday(&end_time, NULL);
  1059. period_time = (end_time.tv_sec - start_time.tv_sec)*1e6 + end_time.tv_usec - start_time.tv_usec;
  1060. total_time += period_time;
  1061. //printk("GPU hardware running period time: %f s\n", (float)period_time/1e-6);
  1062. //printk("GPU hardware running total time: %f s\n", (float)total_time/1e-6);
  1063. #endif
  1064. #endif
  1065. return error;
  1066. }
  1067. static void soft_reset(void)
  1068. {
  1069. vg_lite_hw_clock_control_t value;
  1070. value.data = vg_lite_hal_peek(VG_LITE_HW_CLOCK_CONTROL);
  1071. /* Perform a soft reset. */
  1072. value.control.isolate = 1;
  1073. vg_lite_hal_poke(VG_LITE_HW_CLOCK_CONTROL, value.data);
  1074. value.control.soft_reset = 1;
  1075. vg_lite_hal_poke(VG_LITE_HW_CLOCK_CONTROL, value.data);
  1076. vg_lite_hal_delay(5);
  1077. value.control.soft_reset = 0;
  1078. vg_lite_hal_poke(VG_LITE_HW_CLOCK_CONTROL, value.data);
  1079. value.control.isolate = 0;
  1080. vg_lite_hal_poke(VG_LITE_HW_CLOCK_CONTROL, value.data);
  1081. }
  1082. vg_lite_error_t vg_lite_kernel(vg_lite_kernel_command_t command, void * data)
  1083. {
  1084. /* Dispatch on command. */
  1085. switch (command) {
  1086. case VG_LITE_INITIALIZE:
  1087. /* Initialize the context. */
  1088. return do_initialize(data);
  1089. case VG_LITE_TERMINATE:
  1090. /* Terminate the context. */
  1091. return do_terminate(data);
  1092. case VG_LITE_ALLOCATE:
  1093. /* Allocate contiguous memory. */
  1094. return do_allocate(data);
  1095. case VG_LITE_FREE:
  1096. /* Free contiguous memory. */
  1097. return do_free(data);
  1098. case VG_LITE_SUBMIT:
  1099. /* Submit a command buffer. */
  1100. return do_submit(data);
  1101. case VG_LITE_WAIT:
  1102. /* Wait for the GPU. */
  1103. return do_wait(data);
  1104. case VG_LITE_RESET:
  1105. /* Reset the GPU. */
  1106. return do_reset();
  1107. case VG_LITE_DEBUG:
  1108. /* Perform debugging features. */
  1109. return do_debug();
  1110. case VG_LITE_MAP:
  1111. /* Map some memory. */
  1112. return do_map(data);
  1113. case VG_LITE_UNMAP:
  1114. /* Unmap some memory. */
  1115. return do_unmap(data);
  1116. /* Get register info. */
  1117. case VG_LITE_CHECK:
  1118. /* Get register value. */
  1119. return do_peek(data);
  1120. #if gcFEATURE_VG_FLEXA
  1121. case VG_LITE_FLEXA_DISABLE:
  1122. /* Write register value. */
  1123. return do_flexa_disable(data);
  1124. case VG_LITE_FLEXA_ENABLE:
  1125. /* Write register value. */
  1126. return do_flexa_enable(data);
  1127. case VG_LITE_FLEXA_STOP_FRAME:
  1128. /* Write register value. */
  1129. return do_flexa_stop_frame(data);
  1130. case VG_LITE_FLEXA_SET_BACKGROUND_ADDRESS:
  1131. /* Write register value. */
  1132. return do_flexa_set_background_address(data);
  1133. #endif
  1134. case VG_LITE_QUERY_MEM:
  1135. return do_query_mem(data);
  1136. case VG_LITE_MAP_MEMORY:
  1137. /* Map memory to user */
  1138. return do_map_memory(data);
  1139. case VG_LITE_UNMAP_MEMORY:
  1140. /* Unmap memory to user */
  1141. return do_unmap_memory(data);
  1142. case VG_LITE_CLOSE:
  1143. return do_gpu_close();
  1144. case VG_LITE_CACHE:
  1145. return do_cache(data);
  1146. case VG_LITE_EXPORT_MEMORY:
  1147. return do_export_memory(data);
  1148. case VG_LITE_RECORD_RUNNING_TIME:
  1149. return do_get_running_time(data);
  1150. #if gcdVG_ENABLE_DELAY_RESUME
  1151. case VG_LITE_SET_DELAY_RESUME:
  1152. return set_delay_resume(data);
  1153. case VG_LITE_QUERY_DELAY_RESUME:
  1154. return do_query_delay_resume();
  1155. case VG_LITE_SET_GPU_CLOCK_STATE:
  1156. return set_gpu_clock_state(data);
  1157. #endif
  1158. default:
  1159. break;
  1160. }
  1161. /* Invalid command. */
  1162. return VG_LITE_INVALID_ARGUMENT;
  1163. }