mali_kbase_device.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. *
  3. * (C) COPYRIGHT 2010-2016 ARM Limited. All rights reserved.
  4. *
  5. * This program is free software and is provided to you under the terms of the
  6. * GNU General Public License version 2 as published by the Free Software
  7. * Foundation, and any use by you of this program is subject to the terms
  8. * of such GNU licence.
  9. *
  10. * A copy of the licence is included with the program, and can also be obtained
  11. * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  12. * Boston, MA 02110-1301, USA.
  13. *
  14. */
  15. /*
  16. * Base kernel device APIs
  17. */
  18. #include <linux/debugfs.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/of_platform.h>
  24. #include <mali_kbase.h>
  25. #include <mali_kbase_defs.h>
  26. #include <mali_kbase_hw.h>
  27. #include <mali_kbase_config_defaults.h>
  28. /* NOTE: Magic - 0x45435254 (TRCE in ASCII).
  29. * Supports tracing feature provided in the base module.
  30. * Please keep it in sync with the value of base module.
  31. */
  32. #define TRACE_BUFFER_HEADER_SPECIAL 0x45435254
  33. #if KBASE_TRACE_ENABLE
  34. static const char *kbasep_trace_code_string[] = {
  35. /* IMPORTANT: USE OF SPECIAL #INCLUDE OF NON-STANDARD HEADER FILE
  36. * THIS MUST BE USED AT THE START OF THE ARRAY */
  37. #define KBASE_TRACE_CODE_MAKE_CODE(X) # X
  38. #include "mali_kbase_trace_defs.h"
  39. #undef KBASE_TRACE_CODE_MAKE_CODE
  40. };
  41. #endif
  42. #define DEBUG_MESSAGE_SIZE 256
  43. static int kbasep_trace_init(struct kbase_device *kbdev);
  44. static void kbasep_trace_term(struct kbase_device *kbdev);
  45. static void kbasep_trace_hook_wrapper(void *param);
  46. struct kbase_device *kbase_device_alloc(void)
  47. {
  48. return kzalloc(sizeof(struct kbase_device), GFP_KERNEL);
  49. }
  50. static int kbase_device_as_init(struct kbase_device *kbdev, int i)
  51. {
  52. const char format[] = "mali_mmu%d";
  53. char name[sizeof(format)];
  54. const char poke_format[] = "mali_mmu%d_poker";
  55. char poke_name[sizeof(poke_format)];
  56. if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8316))
  57. snprintf(poke_name, sizeof(poke_name), poke_format, i);
  58. snprintf(name, sizeof(name), format, i);
  59. kbdev->as[i].number = i;
  60. kbdev->as[i].fault_addr = 0ULL;
  61. kbdev->as[i].pf_wq = alloc_workqueue(name, 0, 1);
  62. if (!kbdev->as[i].pf_wq)
  63. return -EINVAL;
  64. mutex_init(&kbdev->as[i].transaction_mutex);
  65. INIT_WORK(&kbdev->as[i].work_pagefault, page_fault_worker);
  66. INIT_WORK(&kbdev->as[i].work_busfault, bus_fault_worker);
  67. if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8316)) {
  68. struct hrtimer *poke_timer = &kbdev->as[i].poke_timer;
  69. struct work_struct *poke_work = &kbdev->as[i].poke_work;
  70. kbdev->as[i].poke_wq = alloc_workqueue(poke_name, 0, 1);
  71. if (!kbdev->as[i].poke_wq) {
  72. destroy_workqueue(kbdev->as[i].pf_wq);
  73. return -EINVAL;
  74. }
  75. KBASE_DEBUG_ASSERT(!object_is_on_stack(poke_work));
  76. INIT_WORK(poke_work, kbasep_as_do_poke);
  77. hrtimer_init(poke_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  78. poke_timer->function = kbasep_as_poke_timer_callback;
  79. kbdev->as[i].poke_refcount = 0;
  80. kbdev->as[i].poke_state = 0u;
  81. }
  82. return 0;
  83. }
  84. static void kbase_device_as_term(struct kbase_device *kbdev, int i)
  85. {
  86. destroy_workqueue(kbdev->as[i].pf_wq);
  87. if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8316))
  88. destroy_workqueue(kbdev->as[i].poke_wq);
  89. }
  90. static int kbase_device_all_as_init(struct kbase_device *kbdev)
  91. {
  92. int i, err;
  93. for (i = 0; i < kbdev->nr_hw_address_spaces; i++) {
  94. err = kbase_device_as_init(kbdev, i);
  95. if (err)
  96. goto free_workqs;
  97. }
  98. return 0;
  99. free_workqs:
  100. for (; i > 0; i--)
  101. kbase_device_as_term(kbdev, i);
  102. return err;
  103. }
  104. static void kbase_device_all_as_term(struct kbase_device *kbdev)
  105. {
  106. int i;
  107. for (i = 0; i < kbdev->nr_hw_address_spaces; i++)
  108. kbase_device_as_term(kbdev, i);
  109. }
  110. int kbase_device_init(struct kbase_device * const kbdev)
  111. {
  112. int err;
  113. #ifdef CONFIG_ARM64
  114. struct device_node *np = NULL;
  115. #endif /* CONFIG_ARM64 */
  116. spin_lock_init(&kbdev->mmu_mask_change);
  117. #ifdef CONFIG_ARM64
  118. kbdev->cci_snoop_enabled = false;
  119. np = kbdev->dev->of_node;
  120. if (np != NULL) {
  121. if (of_property_read_u32(np, "snoop_enable_smc",
  122. &kbdev->snoop_enable_smc))
  123. kbdev->snoop_enable_smc = 0;
  124. if (of_property_read_u32(np, "snoop_disable_smc",
  125. &kbdev->snoop_disable_smc))
  126. kbdev->snoop_disable_smc = 0;
  127. /* Either both or none of the calls should be provided. */
  128. if (!((kbdev->snoop_disable_smc == 0
  129. && kbdev->snoop_enable_smc == 0)
  130. || (kbdev->snoop_disable_smc != 0
  131. && kbdev->snoop_enable_smc != 0))) {
  132. WARN_ON(1);
  133. err = -EINVAL;
  134. goto fail;
  135. }
  136. }
  137. #endif /* CONFIG_ARM64 */
  138. /* Get the list of workarounds for issues on the current HW
  139. * (identified by the GPU_ID register)
  140. */
  141. err = kbase_hw_set_issues_mask(kbdev);
  142. if (err)
  143. goto fail;
  144. /* Set the list of features available on the current HW
  145. * (identified by the GPU_ID register)
  146. */
  147. kbase_hw_set_features_mask(kbdev);
  148. kbase_gpuprops_set_features(kbdev);
  149. /* On Linux 4.0+, dma coherency is determined from device tree */
  150. #ifdef CONFIG_ARM64
  151. set_dma_ops(kbdev->dev, &noncoherent_swiotlb_dma_ops);
  152. #endif
  153. /* Workaround a pre-3.13 Linux issue, where dma_mask is NULL when our
  154. * device structure was created by device-tree
  155. */
  156. if (!kbdev->dev->dma_mask)
  157. kbdev->dev->dma_mask = &kbdev->dev->coherent_dma_mask;
  158. err = dma_set_mask(kbdev->dev,
  159. DMA_BIT_MASK(kbdev->gpu_props.mmu.pa_bits));
  160. if (err)
  161. goto dma_set_mask_failed;
  162. err = dma_set_coherent_mask(kbdev->dev,
  163. DMA_BIT_MASK(kbdev->gpu_props.mmu.pa_bits));
  164. if (err)
  165. goto dma_set_mask_failed;
  166. kbdev->nr_hw_address_spaces = kbdev->gpu_props.num_address_spaces;
  167. err = kbase_device_all_as_init(kbdev);
  168. if (err)
  169. goto as_init_failed;
  170. spin_lock_init(&kbdev->hwcnt.lock);
  171. err = kbasep_trace_init(kbdev);
  172. if (err)
  173. goto term_as;
  174. mutex_init(&kbdev->cacheclean_lock);
  175. #ifdef CONFIG_MALI_TRACE_TIMELINE
  176. for (i = 0; i < BASE_JM_MAX_NR_SLOTS; ++i)
  177. kbdev->timeline.slot_atoms_submitted[i] = 0;
  178. for (i = 0; i <= KBASEP_TIMELINE_PM_EVENT_LAST; ++i)
  179. atomic_set(&kbdev->timeline.pm_event_uid[i], 0);
  180. #endif /* CONFIG_MALI_TRACE_TIMELINE */
  181. kbase_debug_assert_register_hook(&kbasep_trace_hook_wrapper, kbdev);
  182. atomic_set(&kbdev->ctx_num, 0);
  183. err = kbase_instr_backend_init(kbdev);
  184. if (err)
  185. goto term_trace;
  186. kbdev->pm.dvfs_period = DEFAULT_PM_DVFS_PERIOD;
  187. kbdev->reset_timeout_ms = DEFAULT_RESET_TIMEOUT_MS;
  188. #ifdef CONFIG_MALI_GPU_MMU_AARCH64
  189. kbdev->mmu_mode = kbase_mmu_mode_get_aarch64();
  190. #else
  191. kbdev->mmu_mode = kbase_mmu_mode_get_lpae();
  192. #endif /* CONFIG_MALI_GPU_MMU_AARCH64 */
  193. #ifdef CONFIG_MALI_DEBUG
  194. init_waitqueue_head(&kbdev->driver_inactive_wait);
  195. #endif /* CONFIG_MALI_DEBUG */
  196. return 0;
  197. term_trace:
  198. kbasep_trace_term(kbdev);
  199. term_as:
  200. kbase_device_all_as_term(kbdev);
  201. as_init_failed:
  202. dma_set_mask_failed:
  203. fail:
  204. return err;
  205. }
  206. void kbase_device_term(struct kbase_device *kbdev)
  207. {
  208. KBASE_DEBUG_ASSERT(kbdev);
  209. #if KBASE_TRACE_ENABLE
  210. kbase_debug_assert_register_hook(NULL, NULL);
  211. #endif
  212. kbase_instr_backend_term(kbdev);
  213. kbasep_trace_term(kbdev);
  214. kbase_device_all_as_term(kbdev);
  215. }
  216. void kbase_device_free(struct kbase_device *kbdev)
  217. {
  218. kfree(kbdev);
  219. }
  220. int kbase_device_trace_buffer_install(
  221. struct kbase_context *kctx, u32 *tb, size_t size)
  222. {
  223. unsigned long flags;
  224. KBASE_DEBUG_ASSERT(kctx);
  225. KBASE_DEBUG_ASSERT(tb);
  226. /* Interface uses 16-bit value to track last accessed entry. Each entry
  227. * is composed of two 32-bit words.
  228. * This limits the size that can be handled without an overflow. */
  229. if (0xFFFF * (2 * sizeof(u32)) < size)
  230. return -EINVAL;
  231. /* set up the header */
  232. /* magic number in the first 4 bytes */
  233. tb[0] = TRACE_BUFFER_HEADER_SPECIAL;
  234. /* Store (write offset = 0, wrap counter = 0, transaction active = no)
  235. * write offset 0 means never written.
  236. * Offsets 1 to (wrap_offset - 1) used to store values when trace started
  237. */
  238. tb[1] = 0;
  239. /* install trace buffer */
  240. spin_lock_irqsave(&kctx->jctx.tb_lock, flags);
  241. kctx->jctx.tb_wrap_offset = size / 8;
  242. kctx->jctx.tb = tb;
  243. spin_unlock_irqrestore(&kctx->jctx.tb_lock, flags);
  244. return 0;
  245. }
  246. void kbase_device_trace_buffer_uninstall(struct kbase_context *kctx)
  247. {
  248. unsigned long flags;
  249. KBASE_DEBUG_ASSERT(kctx);
  250. spin_lock_irqsave(&kctx->jctx.tb_lock, flags);
  251. kctx->jctx.tb = NULL;
  252. kctx->jctx.tb_wrap_offset = 0;
  253. spin_unlock_irqrestore(&kctx->jctx.tb_lock, flags);
  254. }
  255. void kbase_device_trace_register_access(struct kbase_context *kctx, enum kbase_reg_access_type type, u16 reg_offset, u32 reg_value)
  256. {
  257. unsigned long flags;
  258. spin_lock_irqsave(&kctx->jctx.tb_lock, flags);
  259. if (kctx->jctx.tb) {
  260. u16 wrap_count;
  261. u16 write_offset;
  262. u32 *tb = kctx->jctx.tb;
  263. u32 header_word;
  264. header_word = tb[1];
  265. KBASE_DEBUG_ASSERT(0 == (header_word & 0x1));
  266. wrap_count = (header_word >> 1) & 0x7FFF;
  267. write_offset = (header_word >> 16) & 0xFFFF;
  268. /* mark as transaction in progress */
  269. tb[1] |= 0x1;
  270. mb();
  271. /* calculate new offset */
  272. write_offset++;
  273. if (write_offset == kctx->jctx.tb_wrap_offset) {
  274. /* wrap */
  275. write_offset = 1;
  276. wrap_count++;
  277. wrap_count &= 0x7FFF; /* 15bit wrap counter */
  278. }
  279. /* store the trace entry at the selected offset */
  280. tb[write_offset * 2 + 0] = (reg_offset & ~0x3) | ((type == REG_WRITE) ? 0x1 : 0x0);
  281. tb[write_offset * 2 + 1] = reg_value;
  282. mb();
  283. /* new header word */
  284. header_word = (write_offset << 16) | (wrap_count << 1) | 0x0; /* transaction complete */
  285. tb[1] = header_word;
  286. }
  287. spin_unlock_irqrestore(&kctx->jctx.tb_lock, flags);
  288. }
  289. /*
  290. * Device trace functions
  291. */
  292. #if KBASE_TRACE_ENABLE
  293. static int kbasep_trace_init(struct kbase_device *kbdev)
  294. {
  295. struct kbase_trace *rbuf;
  296. rbuf = kmalloc_array(KBASE_TRACE_SIZE, sizeof(*rbuf), GFP_KERNEL);
  297. if (!rbuf)
  298. return -EINVAL;
  299. kbdev->trace_rbuf = rbuf;
  300. spin_lock_init(&kbdev->trace_lock);
  301. return 0;
  302. }
  303. static void kbasep_trace_term(struct kbase_device *kbdev)
  304. {
  305. kfree(kbdev->trace_rbuf);
  306. }
  307. static void kbasep_trace_format_msg(struct kbase_trace *trace_msg, char *buffer, int len)
  308. {
  309. s32 written = 0;
  310. /* Initial part of message */
  311. written += MAX(snprintf(buffer + written, MAX(len - written, 0), "%d.%.6d,%d,%d,%s,%p,", (int)trace_msg->timestamp.tv_sec, (int)(trace_msg->timestamp.tv_nsec / 1000), trace_msg->thread_id, trace_msg->cpu, kbasep_trace_code_string[trace_msg->code], trace_msg->ctx), 0);
  312. if (trace_msg->katom)
  313. written += MAX(snprintf(buffer + written, MAX(len - written, 0), "atom %d (ud: 0x%llx 0x%llx)", trace_msg->atom_number, trace_msg->atom_udata[0], trace_msg->atom_udata[1]), 0);
  314. written += MAX(snprintf(buffer + written, MAX(len - written, 0), ",%.8llx,", trace_msg->gpu_addr), 0);
  315. /* NOTE: Could add function callbacks to handle different message types */
  316. /* Jobslot present */
  317. if (trace_msg->flags & KBASE_TRACE_FLAG_JOBSLOT)
  318. written += MAX(snprintf(buffer + written, MAX(len - written, 0), "%d", trace_msg->jobslot), 0);
  319. written += MAX(snprintf(buffer + written, MAX(len - written, 0), ","), 0);
  320. /* Refcount present */
  321. if (trace_msg->flags & KBASE_TRACE_FLAG_REFCOUNT)
  322. written += MAX(snprintf(buffer + written, MAX(len - written, 0), "%d", trace_msg->refcount), 0);
  323. written += MAX(snprintf(buffer + written, MAX(len - written, 0), ","), 0);
  324. /* Rest of message */
  325. written += MAX(snprintf(buffer + written, MAX(len - written, 0), "0x%.8lx", trace_msg->info_val), 0);
  326. }
  327. static void kbasep_trace_dump_msg(struct kbase_device *kbdev, struct kbase_trace *trace_msg)
  328. {
  329. char buffer[DEBUG_MESSAGE_SIZE];
  330. kbasep_trace_format_msg(trace_msg, buffer, DEBUG_MESSAGE_SIZE);
  331. dev_dbg(kbdev->dev, "%s", buffer);
  332. }
  333. void kbasep_trace_add(struct kbase_device *kbdev, enum kbase_trace_code code, void *ctx, struct kbase_jd_atom *katom, u64 gpu_addr, u8 flags, int refcount, int jobslot, unsigned long info_val)
  334. {
  335. unsigned long irqflags;
  336. struct kbase_trace *trace_msg;
  337. spin_lock_irqsave(&kbdev->trace_lock, irqflags);
  338. trace_msg = &kbdev->trace_rbuf[kbdev->trace_next_in];
  339. /* Fill the message */
  340. trace_msg->thread_id = task_pid_nr(current);
  341. trace_msg->cpu = task_cpu(current);
  342. getnstimeofday(&trace_msg->timestamp);
  343. trace_msg->code = code;
  344. trace_msg->ctx = ctx;
  345. if (katom == NULL) {
  346. trace_msg->katom = false;
  347. } else {
  348. trace_msg->katom = true;
  349. trace_msg->atom_number = kbase_jd_atom_id(katom->kctx, katom);
  350. trace_msg->atom_udata[0] = katom->udata.blob[0];
  351. trace_msg->atom_udata[1] = katom->udata.blob[1];
  352. }
  353. trace_msg->gpu_addr = gpu_addr;
  354. trace_msg->jobslot = jobslot;
  355. trace_msg->refcount = MIN((unsigned int)refcount, 0xFF);
  356. trace_msg->info_val = info_val;
  357. trace_msg->flags = flags;
  358. /* Update the ringbuffer indices */
  359. kbdev->trace_next_in = (kbdev->trace_next_in + 1) & KBASE_TRACE_MASK;
  360. if (kbdev->trace_next_in == kbdev->trace_first_out)
  361. kbdev->trace_first_out = (kbdev->trace_first_out + 1) & KBASE_TRACE_MASK;
  362. /* Done */
  363. spin_unlock_irqrestore(&kbdev->trace_lock, irqflags);
  364. }
  365. void kbasep_trace_clear(struct kbase_device *kbdev)
  366. {
  367. unsigned long flags;
  368. spin_lock_irqsave(&kbdev->trace_lock, flags);
  369. kbdev->trace_first_out = kbdev->trace_next_in;
  370. spin_unlock_irqrestore(&kbdev->trace_lock, flags);
  371. }
  372. void kbasep_trace_dump(struct kbase_device *kbdev)
  373. {
  374. unsigned long flags;
  375. u32 start;
  376. u32 end;
  377. dev_dbg(kbdev->dev, "Dumping trace:\nsecs,nthread,cpu,code,ctx,katom,gpu_addr,jobslot,refcount,info_val");
  378. spin_lock_irqsave(&kbdev->trace_lock, flags);
  379. start = kbdev->trace_first_out;
  380. end = kbdev->trace_next_in;
  381. while (start != end) {
  382. struct kbase_trace *trace_msg = &kbdev->trace_rbuf[start];
  383. kbasep_trace_dump_msg(kbdev, trace_msg);
  384. start = (start + 1) & KBASE_TRACE_MASK;
  385. }
  386. dev_dbg(kbdev->dev, "TRACE_END");
  387. spin_unlock_irqrestore(&kbdev->trace_lock, flags);
  388. KBASE_TRACE_CLEAR(kbdev);
  389. }
  390. static void kbasep_trace_hook_wrapper(void *param)
  391. {
  392. struct kbase_device *kbdev = (struct kbase_device *)param;
  393. kbasep_trace_dump(kbdev);
  394. }
  395. void kbasep_trace_debugfs_init(struct kbase_device *kbdev)
  396. {
  397. }
  398. #else /* KBASE_TRACE_ENABLE */
  399. static int kbasep_trace_init(struct kbase_device *kbdev)
  400. {
  401. CSTD_UNUSED(kbdev);
  402. return 0;
  403. }
  404. static void kbasep_trace_term(struct kbase_device *kbdev)
  405. {
  406. CSTD_UNUSED(kbdev);
  407. }
  408. static void kbasep_trace_hook_wrapper(void *param)
  409. {
  410. CSTD_UNUSED(param);
  411. }
  412. void kbasep_trace_dump(struct kbase_device *kbdev)
  413. {
  414. CSTD_UNUSED(kbdev);
  415. }
  416. #endif /* KBASE_TRACE_ENABLE */