vg_lite_kernel.c 46 KB

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