2 * Tegra GK20A GPU Debugger/Profiler Driver
4 * Copyright (c) 2013-2014, NVIDIA CORPORATION. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/file.h>
21 #include <linux/cdev.h>
22 #include <linux/uaccess.h>
23 #include <linux/nvhost.h>
24 #include <linux/nvhost_dbg_gpu_ioctl.h>
28 #include "dbg_gpu_gk20a.h"
29 #include "regops_gk20a.h"
30 #include "hw_therm_gk20a.h"
32 struct dbg_gpu_session_ops dbg_gpu_session_ops_gk20a = {
33 .exec_reg_ops = exec_regops_gk20a,
36 /* silly allocator - just increment session id */
37 static atomic_t session_id = ATOMIC_INIT(0);
38 static int generate_session_id(void)
40 return atomic_add_return(1, &session_id);
43 static int alloc_session(struct dbg_session_gk20a **_dbg_s)
45 struct dbg_session_gk20a *dbg_s;
48 gk20a_dbg(gpu_dbg_fn | gpu_dbg_gpu_dbg, "");
50 dbg_s = kzalloc(sizeof(*dbg_s), GFP_KERNEL);
54 dbg_s->id = generate_session_id();
55 dbg_s->ops = &dbg_gpu_session_ops_gk20a;
60 int gk20a_dbg_gpu_do_dev_open(struct inode *inode, struct file *filp, bool is_profiler)
62 struct dbg_session_gk20a *dbg_session;
65 struct platform_device *pdev;
71 g = container_of(inode->i_cdev,
72 struct gk20a, dbg.cdev);
74 g = container_of(inode->i_cdev,
75 struct gk20a, prof.cdev);
79 gk20a_dbg(gpu_dbg_fn | gpu_dbg_gpu_dbg, "dbg session: %s", dev_name(dev));
81 err = alloc_session(&dbg_session);
85 filp->private_data = dbg_session;
86 dbg_session->pdev = pdev;
87 dbg_session->dev = dev;
89 dbg_session->is_profiler = is_profiler;
90 dbg_session->is_pg_disabled = false;
92 INIT_LIST_HEAD(&dbg_session->dbg_s_list_node);
93 init_waitqueue_head(&dbg_session->dbg_events.wait_queue);
94 dbg_session->dbg_events.events_enabled = false;
95 dbg_session->dbg_events.num_pending_events = 0;
100 /* used in scenarios where the debugger session can take just the inter-session
101 * lock for performance, but the profiler session must take the per-gpu lock
102 * since it might not have an associated channel. */
103 static void gk20a_dbg_session_mutex_lock(struct dbg_session_gk20a *dbg_s)
105 if (dbg_s->is_profiler)
106 mutex_lock(&dbg_s->g->dbg_sessions_lock);
108 mutex_lock(&dbg_s->ch->dbg_s_lock);
111 static void gk20a_dbg_session_mutex_unlock(struct dbg_session_gk20a *dbg_s)
113 if (dbg_s->is_profiler)
114 mutex_unlock(&dbg_s->g->dbg_sessions_lock);
116 mutex_unlock(&dbg_s->ch->dbg_s_lock);
119 static void gk20a_dbg_gpu_events_enable(struct dbg_session_gk20a *dbg_s)
121 gk20a_dbg(gpu_dbg_fn | gpu_dbg_gpu_dbg, "");
123 gk20a_dbg_session_mutex_lock(dbg_s);
125 dbg_s->dbg_events.events_enabled = true;
126 dbg_s->dbg_events.num_pending_events = 0;
128 gk20a_dbg_session_mutex_unlock(dbg_s);
131 static void gk20a_dbg_gpu_events_disable(struct dbg_session_gk20a *dbg_s)
133 gk20a_dbg(gpu_dbg_fn | gpu_dbg_gpu_dbg, "");
135 gk20a_dbg_session_mutex_lock(dbg_s);
137 dbg_s->dbg_events.events_enabled = false;
138 dbg_s->dbg_events.num_pending_events = 0;
140 gk20a_dbg_session_mutex_unlock(dbg_s);
143 static void gk20a_dbg_gpu_events_clear(struct dbg_session_gk20a *dbg_s)
145 gk20a_dbg(gpu_dbg_fn | gpu_dbg_gpu_dbg, "");
147 gk20a_dbg_session_mutex_lock(dbg_s);
149 if (dbg_s->dbg_events.events_enabled &&
150 dbg_s->dbg_events.num_pending_events > 0)
151 dbg_s->dbg_events.num_pending_events--;
153 gk20a_dbg_session_mutex_unlock(dbg_s);
156 static int gk20a_dbg_gpu_events_ctrl(struct dbg_session_gk20a *dbg_s,
157 struct nvhost_dbg_gpu_events_ctrl_args *args)
161 gk20a_dbg(gpu_dbg_fn | gpu_dbg_gpu_dbg, "dbg events ctrl cmd %d", args->cmd);
164 gk20a_err(dev_from_gk20a(dbg_s->g),
165 "no channel bound to dbg session\n");
170 case NVHOST_DBG_GPU_EVENTS_CTRL_CMD_ENABLE:
171 gk20a_dbg_gpu_events_enable(dbg_s);
174 case NVHOST_DBG_GPU_EVENTS_CTRL_CMD_DISABLE:
175 gk20a_dbg_gpu_events_disable(dbg_s);
178 case NVHOST_DBG_GPU_EVENTS_CTRL_CMD_CLEAR:
179 gk20a_dbg_gpu_events_clear(dbg_s);
183 gk20a_err(dev_from_gk20a(dbg_s->g),
184 "unrecognized dbg gpu events ctrl cmd: 0x%x",
193 unsigned int gk20a_dbg_gpu_dev_poll(struct file *filep, poll_table *wait)
195 unsigned int mask = 0;
196 struct dbg_session_gk20a *dbg_s = filep->private_data;
198 gk20a_dbg(gpu_dbg_fn | gpu_dbg_gpu_dbg, "");
200 poll_wait(filep, &dbg_s->dbg_events.wait_queue, wait);
202 gk20a_dbg_session_mutex_lock(dbg_s);
204 if (dbg_s->dbg_events.events_enabled &&
205 dbg_s->dbg_events.num_pending_events > 0) {
206 gk20a_dbg(gpu_dbg_gpu_dbg, "found pending event on session id %d",
208 gk20a_dbg(gpu_dbg_gpu_dbg, "%d events pending",
209 dbg_s->dbg_events.num_pending_events);
210 mask = (POLLPRI | POLLIN);
213 gk20a_dbg_session_mutex_unlock(dbg_s);
218 int gk20a_dbg_gpu_dev_open(struct inode *inode, struct file *filp)
220 gk20a_dbg(gpu_dbg_fn | gpu_dbg_gpu_dbg, "");
221 return gk20a_dbg_gpu_do_dev_open(inode, filp, false /* not profiler */);
224 int gk20a_prof_gpu_dev_open(struct inode *inode, struct file *filp)
226 gk20a_dbg(gpu_dbg_fn | gpu_dbg_gpu_dbg, "");
227 return gk20a_dbg_gpu_do_dev_open(inode, filp, true /* is profiler */);
230 void gk20a_dbg_gpu_post_events(struct channel_gk20a *ch)
232 struct dbg_session_gk20a *dbg_s;
234 gk20a_dbg(gpu_dbg_fn | gpu_dbg_gpu_dbg, "");
236 /* guard against the session list being modified */
237 mutex_lock(&ch->dbg_s_lock);
239 list_for_each_entry(dbg_s, &ch->dbg_s_list, dbg_s_list_node) {
240 if (dbg_s->dbg_events.events_enabled) {
241 gk20a_dbg(gpu_dbg_gpu_dbg, "posting event on session id %d",
243 gk20a_dbg(gpu_dbg_gpu_dbg, "%d events pending",
244 dbg_s->dbg_events.num_pending_events);
246 dbg_s->dbg_events.num_pending_events++;
248 wake_up_interruptible_all(&dbg_s->dbg_events.wait_queue);
252 mutex_unlock(&ch->dbg_s_lock);
256 static int dbg_set_powergate(struct dbg_session_gk20a *dbg_s,
259 static int dbg_unbind_channel_gk20a(struct dbg_session_gk20a *dbg_s)
261 struct channel_gk20a *ch_gk20a = dbg_s->ch;
262 struct gk20a *g = dbg_s->g;
264 gk20a_dbg(gpu_dbg_fn | gpu_dbg_gpu_dbg, "");
266 /* wasn't bound to start with ? */
268 gk20a_dbg(gpu_dbg_gpu_dbg | gpu_dbg_fn, "not bound already?");
272 mutex_lock(&g->dbg_sessions_lock);
273 mutex_lock(&ch_gk20a->dbg_s_lock);
281 list_del_init(&dbg_s->dbg_s_list_node);
283 mutex_unlock(&ch_gk20a->dbg_s_lock);
284 mutex_unlock(&g->dbg_sessions_lock);
289 int gk20a_dbg_gpu_dev_release(struct inode *inode, struct file *filp)
291 struct dbg_session_gk20a *dbg_s = filp->private_data;
292 struct gk20a *g = dbg_s->g;
294 gk20a_dbg(gpu_dbg_gpu_dbg | gpu_dbg_fn, "%s", dev_name(dbg_s->dev));
296 /* unbind if it was bound */
298 dbg_unbind_channel_gk20a(dbg_s);
300 /* Powergate enable is called here as possibility of dbg_session
301 * which called powergate disable ioctl, to be killed without calling
302 * powergate enable ioctl
304 mutex_lock(&g->dbg_sessions_lock);
305 dbg_set_powergate(dbg_s, NVHOST_DBG_GPU_POWERGATE_MODE_ENABLE);
306 mutex_unlock(&g->dbg_sessions_lock);
312 static int dbg_bind_channel_gk20a(struct dbg_session_gk20a *dbg_s,
313 struct nvhost_dbg_gpu_bind_channel_args *args)
317 struct channel_gk20a *ch;
319 gk20a_dbg(gpu_dbg_fn|gpu_dbg_gpu_dbg, "%s fd=%d",
320 dev_name(dbg_s->dev), args->channel_fd);
322 if (args->channel_fd == ~0)
323 return dbg_unbind_channel_gk20a(dbg_s);
325 /* even though get_file_channel is doing this it releases it as well */
326 /* by holding it here we'll keep it from disappearing while the
327 * debugger is in session */
328 f = fget(args->channel_fd);
332 ch = gk20a_get_channel_from_file(args->channel_fd);
334 gk20a_dbg_fn("no channel found for fd");
340 gk20a_dbg_fn("%s hwchid=%d", dev_name(dbg_s->dev), ch->hw_chid);
342 mutex_lock(&g->dbg_sessions_lock);
343 mutex_lock(&ch->dbg_s_lock);
347 list_add(&dbg_s->dbg_s_list_node, &dbg_s->ch->dbg_s_list);
351 mutex_unlock(&ch->dbg_s_lock);
352 mutex_unlock(&g->dbg_sessions_lock);
356 static int nvhost_ioctl_channel_reg_ops(struct dbg_session_gk20a *dbg_s,
357 struct nvhost_dbg_gpu_exec_reg_ops_args *args);
359 static int nvhost_ioctl_powergate_gk20a(struct dbg_session_gk20a *dbg_s,
360 struct nvhost_dbg_gpu_powergate_args *args);
362 static int nvhost_dbg_gpu_ioctl_smpc_ctxsw_mode(struct dbg_session_gk20a *dbg_s,
363 struct nvhost_dbg_gpu_smpc_ctxsw_mode_args *args);
365 long gk20a_dbg_gpu_dev_ioctl(struct file *filp, unsigned int cmd,
368 struct dbg_session_gk20a *dbg_s = filp->private_data;
369 struct gk20a *g = get_gk20a(dbg_s->pdev);
370 u8 buf[NVHOST_DBG_GPU_IOCTL_MAX_ARG_SIZE];
373 gk20a_dbg(gpu_dbg_fn | gpu_dbg_gpu_dbg, "");
375 if ((_IOC_TYPE(cmd) != NVHOST_DBG_GPU_IOCTL_MAGIC) ||
376 (_IOC_NR(cmd) == 0) ||
377 (_IOC_NR(cmd) > NVHOST_DBG_GPU_IOCTL_LAST))
380 BUG_ON(_IOC_SIZE(cmd) > NVHOST_DBG_GPU_IOCTL_MAX_ARG_SIZE);
382 if (_IOC_DIR(cmd) & _IOC_WRITE) {
383 if (copy_from_user(buf, (void __user *)arg, _IOC_SIZE(cmd)))
387 if (!g->gr.sw_ready) {
388 err = gk20a_busy(g->dev);
396 case NVHOST_DBG_GPU_IOCTL_BIND_CHANNEL:
397 err = dbg_bind_channel_gk20a(dbg_s,
398 (struct nvhost_dbg_gpu_bind_channel_args *)buf);
399 gk20a_dbg(gpu_dbg_gpu_dbg, "ret=%d", err);
402 case NVHOST_DBG_GPU_IOCTL_REG_OPS:
403 err = nvhost_ioctl_channel_reg_ops(dbg_s,
404 (struct nvhost_dbg_gpu_exec_reg_ops_args *)buf);
405 gk20a_dbg(gpu_dbg_gpu_dbg, "ret=%d", err);
408 case NVHOST_DBG_GPU_IOCTL_POWERGATE:
409 err = nvhost_ioctl_powergate_gk20a(dbg_s,
410 (struct nvhost_dbg_gpu_powergate_args *)buf);
411 gk20a_dbg(gpu_dbg_gpu_dbg, "ret=%d", err);
414 case NVHOST_DBG_GPU_IOCTL_EVENTS_CTRL:
415 err = gk20a_dbg_gpu_events_ctrl(dbg_s,
416 (struct nvhost_dbg_gpu_events_ctrl_args *)buf);
419 case NVHOST_DBG_GPU_IOCTL_SMPC_CTXSW_MODE:
420 err = nvhost_dbg_gpu_ioctl_smpc_ctxsw_mode(dbg_s,
421 (struct nvhost_dbg_gpu_smpc_ctxsw_mode_args *)buf);
425 gk20a_err(dev_from_gk20a(g),
426 "unrecognized dbg gpu ioctl cmd: 0x%x",
432 if ((err == 0) && (_IOC_DIR(cmd) & _IOC_READ))
433 err = copy_to_user((void __user *)arg,
434 buf, _IOC_SIZE(cmd));
439 /* In order to perform a context relative op the context has
440 * to be created already... which would imply that the
441 * context switch mechanism has already been put in place.
442 * So by the time we perform such an opertation it should always
443 * be possible to query for the appropriate context offsets, etc.
445 * But note: while the dbg_gpu bind requires the a channel fd,
446 * it doesn't require an allocated gr/compute obj at that point...
448 static bool gr_context_info_available(struct dbg_session_gk20a *dbg_s,
453 mutex_lock(&gr->ctx_mutex);
454 err = !gr->ctx_vars.golden_image_initialized;
455 mutex_unlock(&gr->ctx_mutex);
462 static int nvhost_ioctl_channel_reg_ops(struct dbg_session_gk20a *dbg_s,
463 struct nvhost_dbg_gpu_exec_reg_ops_args *args)
466 struct device *dev = dbg_s->dev;
467 struct gk20a *g = get_gk20a(dbg_s->pdev);
468 struct nvhost_dbg_gpu_reg_op *ops;
469 u64 ops_size = sizeof(ops[0]) * args->num_ops;
471 gk20a_dbg_fn("%d ops, total size %llu", args->num_ops, ops_size);
474 gk20a_err(dev, "can't call reg_ops on an unbound debugger session");
478 if (!dbg_s->is_profiler && !dbg_s->ch) {
479 gk20a_err(dev, "bind a channel before regops for a debugging session");
483 /* be sure that ctx info is in place */
484 if (!gr_context_info_available(dbg_s, &g->gr)) {
485 gk20a_err(dev, "gr context data not available\n");
489 ops = kzalloc(ops_size, GFP_KERNEL);
491 gk20a_err(dev, "Allocating memory failed!");
495 gk20a_dbg_fn("Copying regops from userspace");
497 if (copy_from_user(ops, (void *)(uintptr_t)args->ops, ops_size)) {
498 dev_err(dev, "copy_from_user failed!");
503 /* since exec_reg_ops sends methods to the ucode, it must take the
504 * global gpu lock to protect against mixing methods from debug sessions
505 * on other channels */
506 mutex_lock(&g->dbg_sessions_lock);
508 err = dbg_s->ops->exec_reg_ops(dbg_s, ops, args->num_ops);
510 mutex_unlock(&g->dbg_sessions_lock);
513 gk20a_err(dev, "dbg regops failed");
517 gk20a_dbg_fn("Copying result to userspace");
519 if (copy_to_user((void *)(uintptr_t)args->ops, ops, ops_size)) {
520 dev_err(dev, "copy_to_user failed!");
530 static int dbg_set_powergate(struct dbg_session_gk20a *dbg_s,
534 struct gk20a *g = get_gk20a(dbg_s->pdev);
536 /* This function must be called with g->dbg_sessions_lock held */
538 gk20a_dbg(gpu_dbg_fn|gpu_dbg_gpu_dbg, "%s powergate mode = %d",
539 dev_name(dbg_s->dev), powermode);
542 case NVHOST_DBG_GPU_POWERGATE_MODE_DISABLE:
543 /* save off current powergate, clk state.
544 * set gpu module's can_powergate = 0.
545 * set gpu module's clk to max.
546 * while *a* debug session is active there will be no power or
547 * clocking state changes allowed from mainline code (but they
550 /* Allow powergate disable if the current dbg_session doesn't
551 * call a powergate disable ioctl and the global
552 * powergating_disabled_refcount is zero
555 if ((dbg_s->is_pg_disabled == false) &&
556 (g->dbg_powergating_disabled_refcount++ == 0)) {
558 gk20a_dbg(gpu_dbg_gpu_dbg | gpu_dbg_fn, "module busy");
560 err = gk20a_busy(dbg_s->pdev);
564 /*do elpg disable before clock gating disable*/
565 gk20a_pmu_disable_elpg(g);
566 g->ops.clock_gating.slcg_gr_load_gating_prod(g,
568 g->ops.clock_gating.slcg_perf_load_gating_prod(g,
570 gr_gk20a_init_blcg_mode(g, BLCG_RUN, ENGINE_GR_GK20A);
572 g->elcg_enabled = false;
573 gr_gk20a_init_elcg_mode(g, ELCG_RUN, ENGINE_GR_GK20A);
574 gr_gk20a_init_elcg_mode(g, ELCG_RUN, ENGINE_CE2_GK20A);
578 dbg_s->is_pg_disabled = true;
581 case NVHOST_DBG_GPU_POWERGATE_MODE_ENABLE:
582 /* restore (can) powergate, clk state */
583 /* release pending exceptions to fault/be handled as usual */
584 /*TBD: ordering of these? */
586 /* Re-enabling powergate as no other sessions want
587 * powergate disabled and the current dbg-sessions had
588 * requested the powergate disable through ioctl
590 if (dbg_s->is_pg_disabled &&
591 --g->dbg_powergating_disabled_refcount == 0) {
593 g->elcg_enabled = true;
594 gr_gk20a_init_elcg_mode(g, ELCG_AUTO, ENGINE_GR_GK20A);
595 gr_gk20a_init_elcg_mode(g, ELCG_AUTO, ENGINE_CE2_GK20A);
596 gr_gk20a_init_blcg_mode(g, BLCG_AUTO, ENGINE_GR_GK20A);
598 g->ops.clock_gating.slcg_gr_load_gating_prod(g,
600 g->ops.clock_gating.slcg_perf_load_gating_prod(g,
603 gk20a_pmu_enable_elpg(g);
605 gk20a_dbg(gpu_dbg_gpu_dbg | gpu_dbg_fn, "module idle");
606 gk20a_idle(dbg_s->pdev);
610 dbg_s->is_pg_disabled = false;
614 gk20a_err(dev_from_gk20a(g),
615 "unrecognized dbg gpu powergate mode: 0x%x",
621 gk20a_dbg(gpu_dbg_fn|gpu_dbg_gpu_dbg, "%s powergate mode = %d done",
622 dev_name(dbg_s->dev), powermode);
626 static int nvhost_ioctl_powergate_gk20a(struct dbg_session_gk20a *dbg_s,
627 struct nvhost_dbg_gpu_powergate_args *args)
630 struct gk20a *g = get_gk20a(dbg_s->pdev);
631 gk20a_dbg_fn("%s powergate mode = %d",
632 dev_name(dbg_s->dev), args->mode);
634 mutex_lock(&g->dbg_sessions_lock);
635 err = dbg_set_powergate(dbg_s, args->mode);
636 mutex_unlock(&g->dbg_sessions_lock);
640 static int nvhost_dbg_gpu_ioctl_smpc_ctxsw_mode(struct dbg_session_gk20a *dbg_s,
641 struct nvhost_dbg_gpu_smpc_ctxsw_mode_args *args)
644 struct gk20a *g = get_gk20a(dbg_s->pdev);
645 struct channel_gk20a *ch_gk20a;
647 gk20a_dbg_fn("%s smpc ctxsw mode = %d",
648 dev_name(dbg_s->dev), args->mode);
650 /* Take the global lock, since we'll be doing global regops */
651 mutex_lock(&g->dbg_sessions_lock);
653 ch_gk20a = dbg_s->ch;
656 gk20a_err(dev_from_gk20a(dbg_s->g),
657 "no bound channel for smpc ctxsw mode update\n");
662 err = gr_gk20a_update_smpc_ctxsw_mode(g, ch_gk20a,
663 args->mode == NVHOST_DBG_GPU_SMPC_CTXSW_MODE_CTXSW);
665 gk20a_err(dev_from_gk20a(dbg_s->g),
666 "error (%d) during smpc ctxsw mode update\n", err);
669 /* The following regops are a hack/war to make up for the fact that we
670 * just scribbled into the ctxsw image w/o really knowing whether
671 * it was already swapped out in/out once or not, etc.
674 struct nvhost_dbg_gpu_reg_op ops[4];
676 for (i = 0; i < ARRAY_SIZE(ops); i++) {
677 ops[i].op = NVHOST_DBG_GPU_REG_OP_WRITE_32;
678 ops[i].type = NVHOST_DBG_GPU_REG_OP_TYPE_GR_CTX;
679 ops[i].status = NVHOST_DBG_GPU_REG_OP_STATUS_SUCCESS;
681 ops[i].and_n_mask_lo = 0;
682 ops[i].and_n_mask_hi = 0;
684 /* gr_pri_gpcs_tpcs_sm_dsm_perf_counter_control_sel1_r();*/
685 ops[0].offset = 0x00419e08;
686 ops[0].value_lo = 0x1d;
688 /* gr_pri_gpcs_tpcs_sm_dsm_perf_counter_control5_r(); */
689 ops[1].offset = 0x00419e58;
690 ops[1].value_lo = 0x1;
692 /* gr_pri_gpcs_tpcs_sm_dsm_perf_counter_control3_r(); */
693 ops[2].offset = 0x00419e68;
694 ops[2].value_lo = 0xaaaa;
696 /* gr_pri_gpcs_tpcs_sm_dsm_perf_counter4_control_r(); */
697 ops[3].offset = 0x00419f40;
698 ops[3].value_lo = 0x18;
700 err = dbg_s->ops->exec_reg_ops(dbg_s, ops, ARRAY_SIZE(ops));
704 mutex_unlock(&g->dbg_sessions_lock);