summaryrefslogtreecommitdiffstats
path: root/drivers/gpu/nvgpu/common/linux/module.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/gpu/nvgpu/common/linux/module.c')
-rw-r--r--drivers/gpu/nvgpu/common/linux/module.c1277
1 files changed, 1277 insertions, 0 deletions
diff --git a/drivers/gpu/nvgpu/common/linux/module.c b/drivers/gpu/nvgpu/common/linux/module.c
new file mode 100644
index 00000000..76b9d4db
--- /dev/null
+++ b/drivers/gpu/nvgpu/common/linux/module.c
@@ -0,0 +1,1277 @@
1/*
2 * GK20A Graphics
3 *
4 * Copyright (c) 2011-2017, NVIDIA CORPORATION. All rights reserved.
5 *
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.
9 *
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
13 * more details.
14 *
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/>.
17 */
18
19#include <linux/module.h>
20#include <linux/of.h>
21#include <linux/of_device.h>
22#include <linux/of_platform.h>
23#include <linux/interrupt.h>
24#include <linux/pm_runtime.h>
25#include <linux/reset.h>
26#include <linux/platform/tegra/common.h>
27#include <uapi/linux/nvgpu.h>
28#include <dt-bindings/soc/gm20b-fuse.h>
29#include <dt-bindings/soc/gp10b-fuse.h>
30
31#include <soc/tegra/fuse.h>
32
33#include <nvgpu/dma.h>
34#include <nvgpu/kmem.h>
35#include <nvgpu/nvgpu_common.h>
36#include <nvgpu/soc.h>
37#include <nvgpu/enabled.h>
38#include <nvgpu/debug.h>
39#include <nvgpu/ctxsw_trace.h>
40
41#include "platform_gk20a.h"
42#include "sysfs.h"
43#include "vgpu/vgpu.h"
44#include "scale.h"
45#include "pci.h"
46#include "module.h"
47#include "intr.h"
48#include "cde.h"
49#include "ioctl.h"
50#ifdef CONFIG_TEGRA_19x_GPU
51#include "nvgpu_gpuid_t19x.h"
52#ifdef CONFIG_TEGRA_GR_VIRTUALIZATION
53#include "vgpu/vgpu_t19x.h"
54#endif
55#endif
56
57#include "os_linux.h"
58#include "cde_gm20b.h"
59#include "cde_gp10b.h"
60#include "ctxsw_trace.h"
61#include "driver_common.h"
62
63#define CLASS_NAME "nvidia-gpu"
64/* TODO: Change to e.g. "nvidia-gpu%s" once we have symlinks in place. */
65
66#define GK20A_WAIT_FOR_IDLE_MS 2000
67
68#define CREATE_TRACE_POINTS
69#include <trace/events/gk20a.h>
70
71void gk20a_busy_noresume(struct gk20a *g)
72{
73 pm_runtime_get_noresume(dev_from_gk20a(g));
74}
75
76int gk20a_busy(struct gk20a *g)
77{
78 struct nvgpu_os_linux *l = nvgpu_os_linux_from_gk20a(g);
79 int ret = 0;
80 struct device *dev;
81
82 if (!g)
83 return -ENODEV;
84
85 atomic_inc(&g->usage_count.atomic_var);
86
87 down_read(&l->busy_lock);
88
89 if (!gk20a_can_busy(g)) {
90 ret = -ENODEV;
91 atomic_dec(&g->usage_count.atomic_var);
92 goto fail;
93 }
94
95 dev = dev_from_gk20a(g);
96
97 if (pm_runtime_enabled(dev)) {
98 /* Increment usage count and attempt to resume device */
99 ret = pm_runtime_get_sync(dev);
100 if (ret < 0) {
101 /* Mark suspended so runtime pm will retry later */
102 pm_runtime_set_suspended(dev);
103 pm_runtime_put_noidle(dev);
104 atomic_dec(&g->usage_count.atomic_var);
105 goto fail;
106 }
107 } else {
108 nvgpu_mutex_acquire(&g->poweron_lock);
109 if (!g->power_on) {
110 ret = gk20a_gpu_is_virtual(dev) ?
111 vgpu_pm_finalize_poweron(dev)
112 : gk20a_pm_finalize_poweron(dev);
113 if (ret) {
114 atomic_dec(&g->usage_count.atomic_var);
115 nvgpu_mutex_release(&g->poweron_lock);
116 goto fail;
117 }
118 }
119 nvgpu_mutex_release(&g->poweron_lock);
120 }
121
122fail:
123 up_read(&l->busy_lock);
124
125 return ret < 0 ? ret : 0;
126}
127
128void gk20a_idle_nosuspend(struct gk20a *g)
129{
130 pm_runtime_put_noidle(dev_from_gk20a(g));
131}
132
133void gk20a_idle(struct gk20a *g)
134{
135 struct device *dev;
136
137 atomic_dec(&g->usage_count.atomic_var);
138
139 dev = dev_from_gk20a(g);
140
141 if (!(dev && gk20a_can_busy(g)))
142 return;
143
144 if (pm_runtime_enabled(dev)) {
145 pm_runtime_mark_last_busy(dev);
146 pm_runtime_put_sync_autosuspend(dev);
147 }
148}
149
150/*
151 * Undoes gk20a_lockout_registers().
152 */
153static int gk20a_restore_registers(struct gk20a *g)
154{
155 struct nvgpu_os_linux *l = nvgpu_os_linux_from_gk20a(g);
156
157 l->regs = l->regs_saved;
158 l->bar1 = l->bar1_saved;
159
160#ifdef CONFIG_TEGRA_19x_GPU
161 t19x_restore_registers(g);
162#endif
163
164 return 0;
165}
166
167static int nvgpu_init_os_linux_ops(struct nvgpu_os_linux *l)
168{
169 struct gk20a *g = &l->g;
170 u32 ver = g->params.gpu_arch + g->params.gpu_impl;
171
172 switch (ver) {
173 case GK20A_GPUID_GM20B:
174 case GK20A_GPUID_GM20B_B:
175 l->ops.cde = gm20b_cde_ops.cde;
176 break;
177 case NVGPU_GPUID_GP10B:
178 l->ops.cde = gp10b_cde_ops.cde;
179 break;
180 default:
181 /* CDE is optional, so today ignoring unknown chip is fine */
182 break;
183 }
184
185 return 0;
186}
187
188int gk20a_pm_finalize_poweron(struct device *dev)
189{
190 struct gk20a *g = get_gk20a(dev);
191 struct nvgpu_os_linux *l = nvgpu_os_linux_from_gk20a(g);
192 struct gk20a_platform *platform = gk20a_get_platform(dev);
193 int err, nice_value;
194
195 gk20a_dbg_fn("");
196
197 if (g->power_on)
198 return 0;
199
200 trace_gk20a_finalize_poweron(dev_name(dev));
201
202 /* Increment platform power refcount */
203 if (platform->busy) {
204 err = platform->busy(dev);
205 if (err < 0) {
206 nvgpu_err(g, "failed to poweron platform dependency");
207 return err;
208 }
209 }
210
211 err = gk20a_restore_registers(g);
212 if (err)
213 return err;
214
215 nice_value = task_nice(current);
216 set_user_nice(current, -20);
217
218 /* Enable interrupt workqueue */
219 if (!l->nonstall_work_queue) {
220 l->nonstall_work_queue = alloc_workqueue("%s",
221 WQ_HIGHPRI, 1, "mc_nonstall");
222 INIT_WORK(&l->nonstall_fn_work, nvgpu_intr_nonstall_cb);
223 }
224
225 err = gk20a_finalize_poweron(g);
226 set_user_nice(current, nice_value);
227 if (err)
228 goto done;
229
230 trace_gk20a_finalize_poweron_done(dev_name(dev));
231
232 err = nvgpu_init_os_linux_ops(l);
233 if (err)
234 goto done;
235
236 enable_irq(g->irq_stall);
237 if (g->irq_stall != g->irq_nonstall)
238 enable_irq(g->irq_nonstall);
239 g->irqs_enabled = 1;
240
241 gk20a_scale_resume(dev_from_gk20a(g));
242
243 if (platform->has_cde)
244 gk20a_init_cde_support(l);
245
246 err = gk20a_sched_ctrl_init(g);
247 if (err) {
248 nvgpu_err(g, "failed to init sched control");
249 return err;
250 }
251
252done:
253 if (err)
254 g->power_on = false;
255
256 return err;
257}
258
259/*
260 * Locks out the driver from accessing GPU registers. This prevents access to
261 * thse registers after the GPU has been clock or power gated. This should help
262 * find annoying bugs where register reads and writes are silently dropped
263 * after the GPU has been turned off. On older chips these reads and writes can
264 * also lock the entire CPU up.
265 */
266static int gk20a_lockout_registers(struct gk20a *g)
267{
268 struct nvgpu_os_linux *l = nvgpu_os_linux_from_gk20a(g);
269
270 l->regs = NULL;
271 l->bar1 = NULL;
272
273#ifdef CONFIG_TEGRA_19x_GPU
274 t19x_lockout_registers(g);
275#endif
276
277 return 0;
278}
279
280static int gk20a_pm_prepare_poweroff(struct device *dev)
281{
282 struct gk20a *g = get_gk20a(dev);
283 struct nvgpu_os_linux *l = nvgpu_os_linux_from_gk20a(g);
284 int ret = 0;
285 struct gk20a_platform *platform = gk20a_get_platform(dev);
286 bool irqs_enabled;
287
288 gk20a_dbg_fn("");
289
290 nvgpu_mutex_acquire(&g->poweroff_lock);
291
292 if (!g->power_on)
293 goto done;
294
295 /* disable IRQs and wait for completion */
296 irqs_enabled = g->irqs_enabled;
297 if (irqs_enabled) {
298 disable_irq(g->irq_stall);
299 if (g->irq_stall != g->irq_nonstall)
300 disable_irq(g->irq_nonstall);
301 g->irqs_enabled = 0;
302 }
303
304 gk20a_scale_suspend(dev);
305
306 gk20a_cde_suspend(l);
307
308 ret = gk20a_prepare_poweroff(g);
309 if (ret)
310 goto error;
311
312 /* Decrement platform power refcount */
313 if (platform->idle)
314 platform->idle(dev);
315
316 /* Stop CPU from accessing the GPU registers. */
317 gk20a_lockout_registers(g);
318
319 nvgpu_mutex_release(&g->poweroff_lock);
320 return 0;
321
322error:
323 /* re-enabled IRQs if previously enabled */
324 if (irqs_enabled) {
325 enable_irq(g->irq_stall);
326 if (g->irq_stall != g->irq_nonstall)
327 enable_irq(g->irq_nonstall);
328 g->irqs_enabled = 1;
329 }
330
331 gk20a_scale_resume(dev);
332done:
333 nvgpu_mutex_release(&g->poweroff_lock);
334
335 return ret;
336}
337
338static struct of_device_id tegra_gk20a_of_match[] = {
339#ifdef CONFIG_TEGRA_GK20A
340 { .compatible = "nvidia,tegra210-gm20b",
341 .data = &gm20b_tegra_platform },
342#ifdef CONFIG_ARCH_TEGRA_18x_SOC
343 { .compatible = "nvidia,tegra186-gp10b",
344 .data = &gp10b_tegra_platform },
345#endif
346#ifdef CONFIG_TEGRA_19x_GPU
347 { .compatible = TEGRA_19x_GPU_COMPAT_TEGRA,
348 .data = &t19x_gpu_tegra_platform },
349#ifdef CONFIG_TEGRA_GR_VIRTUALIZATION
350 { .compatible = TEGRA_19x_VGPU_COMPAT_TEGRA,
351 .data = &t19x_vgpu_tegra_platform },
352#endif
353#endif
354#ifdef CONFIG_TEGRA_GR_VIRTUALIZATION
355 { .compatible = "nvidia,tegra124-gk20a-vgpu",
356 .data = &vgpu_tegra_platform },
357#endif
358#endif
359
360 { },
361};
362
363#ifdef CONFIG_PM
364/**
365 * __gk20a_do_idle() - force the GPU to idle and railgate
366 *
367 * In success, this call MUST be balanced by caller with __gk20a_do_unidle()
368 *
369 * Acquires two locks : &l->busy_lock and &platform->railgate_lock
370 * In success, we hold these locks and return
371 * In failure, we release these locks and return
372 */
373int __gk20a_do_idle(struct gk20a *g, bool force_reset)
374{
375 struct nvgpu_os_linux *l = nvgpu_os_linux_from_gk20a(g);
376 struct device *dev = dev_from_gk20a(g);
377 struct gk20a_platform *platform = dev_get_drvdata(dev);
378 struct nvgpu_timeout timeout;
379 int ref_cnt;
380 int target_ref_cnt = 0;
381 bool is_railgated;
382 int err = 0;
383
384 /*
385 * Hold back deterministic submits and changes to deterministic
386 * channels - this must be outside the power busy locks.
387 */
388 gk20a_channel_deterministic_idle(g);
389
390 /* acquire busy lock to block other busy() calls */
391 down_write(&l->busy_lock);
392
393 /* acquire railgate lock to prevent unrailgate in midst of do_idle() */
394 nvgpu_mutex_acquire(&platform->railgate_lock);
395
396 /* check if it is already railgated ? */
397 if (platform->is_railgated(dev))
398 return 0;
399
400 /*
401 * release railgate_lock, prevent suspend by incrementing usage counter,
402 * re-acquire railgate_lock
403 */
404 nvgpu_mutex_release(&platform->railgate_lock);
405 pm_runtime_get_sync(dev);
406
407 /*
408 * One refcount taken in this API
409 * If User disables rail gating, we take one more
410 * extra refcount
411 */
412 if (g->user_railgate_disabled)
413 target_ref_cnt = 2;
414 else
415 target_ref_cnt = 1;
416 nvgpu_mutex_acquire(&platform->railgate_lock);
417
418 nvgpu_timeout_init(g, &timeout, GK20A_WAIT_FOR_IDLE_MS,
419 NVGPU_TIMER_CPU_TIMER);
420
421 /* check and wait until GPU is idle (with a timeout) */
422 do {
423 nvgpu_usleep_range(1000, 1100);
424 ref_cnt = atomic_read(&dev->power.usage_count);
425 } while (ref_cnt != target_ref_cnt && !nvgpu_timeout_expired(&timeout));
426
427 if (ref_cnt != target_ref_cnt) {
428 nvgpu_err(g, "failed to idle - refcount %d != target_ref_cnt",
429 ref_cnt);
430 goto fail_drop_usage_count;
431 }
432
433 /* check if global force_reset flag is set */
434 force_reset |= platform->force_reset_in_do_idle;
435
436 nvgpu_timeout_init(g, &timeout, GK20A_WAIT_FOR_IDLE_MS,
437 NVGPU_TIMER_CPU_TIMER);
438
439 if (g->can_railgate && !force_reset) {
440 /*
441 * Case 1 : GPU railgate is supported
442 *
443 * if GPU is now idle, we will have only one ref count,
444 * drop this ref which will rail gate the GPU
445 */
446 pm_runtime_put_sync(dev);
447
448 /* add sufficient delay to allow GPU to rail gate */
449 nvgpu_msleep(g->railgate_delay);
450
451 /* check in loop if GPU is railgated or not */
452 do {
453 nvgpu_usleep_range(1000, 1100);
454 is_railgated = platform->is_railgated(dev);
455 } while (!is_railgated && !nvgpu_timeout_expired(&timeout));
456
457 if (is_railgated) {
458 return 0;
459 } else {
460 nvgpu_err(g, "failed to idle in timeout");
461 goto fail_timeout;
462 }
463 } else {
464 /*
465 * Case 2 : GPU railgate is not supported or we explicitly
466 * do not want to depend on runtime PM
467 *
468 * if GPU is now idle, call prepare_poweroff() to save the
469 * state and then do explicit railgate
470 *
471 * __gk20a_do_unidle() needs to unrailgate, call
472 * finalize_poweron(), and then call pm_runtime_put_sync()
473 * to balance the GPU usage counter
474 */
475
476 /* Save the GPU state */
477 err = gk20a_pm_prepare_poweroff(dev);
478 if (err)
479 goto fail_drop_usage_count;
480
481 /* railgate GPU */
482 platform->railgate(dev);
483
484 nvgpu_udelay(10);
485
486 g->forced_reset = true;
487 return 0;
488 }
489
490fail_drop_usage_count:
491 pm_runtime_put_noidle(dev);
492fail_timeout:
493 nvgpu_mutex_release(&platform->railgate_lock);
494 up_write(&l->busy_lock);
495 gk20a_channel_deterministic_unidle(g);
496 return -EBUSY;
497}
498
499/**
500 * gk20a_do_idle() - wrap up for __gk20a_do_idle() to be called
501 * from outside of GPU driver
502 *
503 * In success, this call MUST be balanced by caller with gk20a_do_unidle()
504 */
505static int gk20a_do_idle(void *_g)
506{
507 struct gk20a *g = (struct gk20a *)_g;
508
509 return __gk20a_do_idle(g, true);
510}
511
512/**
513 * __gk20a_do_unidle() - unblock all the tasks blocked by __gk20a_do_idle()
514 */
515int __gk20a_do_unidle(struct gk20a *g)
516{
517 struct nvgpu_os_linux *l = nvgpu_os_linux_from_gk20a(g);
518 struct device *dev = dev_from_gk20a(g);
519 struct gk20a_platform *platform = dev_get_drvdata(dev);
520 int err;
521
522 if (g->forced_reset) {
523 /*
524 * If we did a forced-reset/railgate
525 * then unrailgate the GPU here first
526 */
527 platform->unrailgate(dev);
528
529 /* restore the GPU state */
530 err = gk20a_pm_finalize_poweron(dev);
531 if (err)
532 return err;
533
534 /* balance GPU usage counter */
535 pm_runtime_put_sync(dev);
536
537 g->forced_reset = false;
538 }
539
540 /* release the lock and open up all other busy() calls */
541 nvgpu_mutex_release(&platform->railgate_lock);
542 up_write(&l->busy_lock);
543
544 gk20a_channel_deterministic_unidle(g);
545
546 return 0;
547}
548
549/**
550 * gk20a_do_unidle() - wrap up for __gk20a_do_unidle()
551 */
552static int gk20a_do_unidle(void *_g)
553{
554 struct gk20a *g = (struct gk20a *)_g;
555
556 return __gk20a_do_unidle(g);
557}
558#endif
559
560static void __iomem *gk20a_ioremap_resource(struct platform_device *dev, int i,
561 struct resource **out)
562{
563 struct resource *r = platform_get_resource(dev, IORESOURCE_MEM, i);
564
565 if (!r)
566 return NULL;
567 if (out)
568 *out = r;
569 return devm_ioremap_resource(&dev->dev, r);
570}
571
572static irqreturn_t gk20a_intr_isr_stall(int irq, void *dev_id)
573{
574 struct gk20a *g = dev_id;
575
576 return nvgpu_intr_stall(g);
577}
578
579static irqreturn_t gk20a_intr_isr_nonstall(int irq, void *dev_id)
580{
581 struct gk20a *g = dev_id;
582
583 return nvgpu_intr_nonstall(g);
584}
585
586static irqreturn_t gk20a_intr_thread_stall(int irq, void *dev_id)
587{
588 struct gk20a *g = dev_id;
589
590 return nvgpu_intr_thread_stall(g);
591}
592
593void gk20a_remove_support(struct gk20a *g)
594{
595 struct nvgpu_os_linux *l = nvgpu_os_linux_from_gk20a(g);
596
597 tegra_unregister_idle_unidle(gk20a_do_idle);
598
599 nvgpu_kfree(g, g->dbg_regops_tmp_buf);
600
601 if (g->pmu.remove_support)
602 g->pmu.remove_support(&g->pmu);
603
604 if (g->gr.remove_support)
605 g->gr.remove_support(&g->gr);
606
607 if (g->mm.remove_ce_support)
608 g->mm.remove_ce_support(&g->mm);
609
610 if (g->fifo.remove_support)
611 g->fifo.remove_support(&g->fifo);
612
613 if (g->mm.remove_support)
614 g->mm.remove_support(&g->mm);
615
616 if (g->sim.remove_support)
617 g->sim.remove_support(&g->sim);
618
619 /* free mappings to registers, etc */
620
621 if (l->regs) {
622 iounmap(l->regs);
623 l->regs = NULL;
624 }
625 if (l->bar1) {
626 iounmap(l->bar1);
627 l->bar1 = NULL;
628 }
629
630#ifdef CONFIG_TEGRA_19x_GPU
631 t19x_remove_support(g);
632#endif
633}
634
635static int gk20a_init_support(struct platform_device *dev)
636{
637 int err = 0;
638 struct gk20a *g = get_gk20a(&dev->dev);
639 struct nvgpu_os_linux *l = nvgpu_os_linux_from_gk20a(g);
640
641 tegra_register_idle_unidle(gk20a_do_idle, gk20a_do_unidle, g);
642
643 l->regs = gk20a_ioremap_resource(dev, GK20A_BAR0_IORESOURCE_MEM,
644 &l->reg_mem);
645 if (IS_ERR(l->regs)) {
646 nvgpu_err(g, "failed to remap gk20a registers");
647 err = PTR_ERR(l->regs);
648 goto fail;
649 }
650
651 l->bar1 = gk20a_ioremap_resource(dev, GK20A_BAR1_IORESOURCE_MEM,
652 &l->bar1_mem);
653 if (IS_ERR(l->bar1)) {
654 nvgpu_err(g, "failed to remap gk20a bar1");
655 err = PTR_ERR(l->bar1);
656 goto fail;
657 }
658
659 if (nvgpu_platform_is_simulation(g)) {
660 g->sim.g = g;
661 g->sim.regs = gk20a_ioremap_resource(dev,
662 GK20A_SIM_IORESOURCE_MEM,
663 &g->sim.reg_mem);
664 if (IS_ERR(g->sim.regs)) {
665 nvgpu_err(g, "failed to remap gk20a sim regs");
666 err = PTR_ERR(g->sim.regs);
667 goto fail;
668 }
669
670 err = gk20a_init_sim_support(g);
671 if (err)
672 goto fail;
673 }
674
675#ifdef CONFIG_TEGRA_19x_GPU
676 t19x_init_support(g);
677#endif
678
679 return 0;
680
681fail:
682 return err;
683}
684
685static int gk20a_pm_railgate(struct device *dev)
686{
687 struct gk20a_platform *platform = dev_get_drvdata(dev);
688 int ret = 0;
689 struct gk20a *g = get_gk20a(dev);
690#ifdef CONFIG_DEBUG_FS
691 g->pstats.last_rail_gate_start = jiffies;
692
693 if (g->pstats.railgating_cycle_count >= 1)
694 g->pstats.total_rail_ungate_time_ms =
695 g->pstats.total_rail_ungate_time_ms +
696 jiffies_to_msecs(g->pstats.last_rail_gate_start -
697 g->pstats.last_rail_ungate_complete);
698#endif
699
700 if (platform->railgate)
701 ret = platform->railgate(dev);
702 if (ret) {
703 nvgpu_err(g, "failed to railgate platform, err=%d", ret);
704 return ret;
705 }
706
707#ifdef CONFIG_DEBUG_FS
708 g->pstats.last_rail_gate_complete = jiffies;
709#endif
710 ret = tegra_fuse_clock_disable();
711 if (ret)
712 nvgpu_err(g, "failed to disable tegra fuse clock, err=%d", ret);
713
714 return ret;
715}
716
717static int gk20a_pm_unrailgate(struct device *dev)
718{
719 struct gk20a_platform *platform = dev_get_drvdata(dev);
720 int ret = 0;
721 struct gk20a *g = get_gk20a(dev);
722
723 ret = tegra_fuse_clock_enable();
724 if (ret) {
725 nvgpu_err(g, "failed to enable tegra fuse clock, err=%d", ret);
726 return ret;
727 }
728#ifdef CONFIG_DEBUG_FS
729 g->pstats.last_rail_ungate_start = jiffies;
730 if (g->pstats.railgating_cycle_count >= 1)
731 g->pstats.total_rail_gate_time_ms =
732 g->pstats.total_rail_gate_time_ms +
733 jiffies_to_msecs(g->pstats.last_rail_ungate_start -
734 g->pstats.last_rail_gate_complete);
735
736 g->pstats.railgating_cycle_count++;
737#endif
738
739 trace_gk20a_pm_unrailgate(dev_name(dev));
740
741 if (platform->unrailgate) {
742 nvgpu_mutex_acquire(&platform->railgate_lock);
743 ret = platform->unrailgate(dev);
744 nvgpu_mutex_release(&platform->railgate_lock);
745 }
746
747#ifdef CONFIG_DEBUG_FS
748 g->pstats.last_rail_ungate_complete = jiffies;
749#endif
750
751 return ret;
752}
753
754/*
755 * Remove association of the driver with OS interrupt handler
756 */
757void nvgpu_free_irq(struct gk20a *g)
758{
759 struct device *dev = dev_from_gk20a(g);
760
761 devm_free_irq(dev, g->irq_stall, g);
762 if (g->irq_stall != g->irq_nonstall)
763 devm_free_irq(dev, g->irq_nonstall, g);
764}
765
766/*
767 * Idle the GPU in preparation of shutdown/remove.
768 * gk20a_driver_start_unload() does not idle the GPU, but instead changes the SW
769 * state to prevent further activity on the driver SW side.
770 * On driver removal quiesce() should be called after start_unload()
771 */
772int nvgpu_quiesce(struct gk20a *g)
773{
774 int err;
775 struct device *dev = dev_from_gk20a(g);
776
777 if (g->power_on) {
778 err = gk20a_wait_for_idle(g);
779 if (err) {
780 nvgpu_err(g, "failed to idle GPU, err=%d", err);
781 return err;
782 }
783
784 err = gk20a_fifo_disable_all_engine_activity(g, true);
785 if (err) {
786 nvgpu_err(g,
787 "failed to disable engine activity, err=%d",
788 err);
789 return err;
790 }
791
792 err = gk20a_fifo_wait_engine_idle(g);
793 if (err) {
794 nvgpu_err(g, "failed to idle engines, err=%d",
795 err);
796 return err;
797 }
798 }
799
800 if (gk20a_gpu_is_virtual(dev))
801 err = vgpu_pm_prepare_poweroff(dev);
802 else
803 err = gk20a_pm_prepare_poweroff(dev);
804
805 if (err)
806 nvgpu_err(g, "failed to prepare for poweroff, err=%d",
807 err);
808
809 return err;
810}
811
812static void gk20a_pm_shutdown(struct platform_device *pdev)
813{
814 struct gk20a_platform *platform = platform_get_drvdata(pdev);
815 struct gk20a *g = platform->g;
816 int err;
817
818 nvgpu_info(g, "shutting down");
819
820 /* vgpu has nothing to clean up currently */
821 if (gk20a_gpu_is_virtual(&pdev->dev))
822 return;
823
824 if (!g->power_on)
825 goto finish;
826
827 gk20a_driver_start_unload(g);
828
829 /* If GPU is already railgated,
830 * just prevent more requests, and return */
831 if (platform->is_railgated && platform->is_railgated(&pdev->dev)) {
832 __pm_runtime_disable(&pdev->dev, false);
833 nvgpu_info(g, "already railgated, shut down complete");
834 return;
835 }
836
837 /* Prevent more requests by disabling Runtime PM */
838 __pm_runtime_disable(&pdev->dev, false);
839
840 err = nvgpu_quiesce(g);
841 if (err)
842 goto finish;
843
844 err = gk20a_pm_railgate(&pdev->dev);
845 if (err)
846 nvgpu_err(g, "failed to railgate, err=%d", err);
847
848finish:
849 nvgpu_info(g, "shut down complete");
850}
851
852#ifdef CONFIG_PM
853static int gk20a_pm_runtime_resume(struct device *dev)
854{
855 int err = 0;
856
857 err = gk20a_pm_unrailgate(dev);
858 if (err)
859 goto fail;
860
861 if (gk20a_gpu_is_virtual(dev))
862 err = vgpu_pm_finalize_poweron(dev);
863 else
864 err = gk20a_pm_finalize_poweron(dev);
865 if (err)
866 goto fail_poweron;
867
868 return 0;
869
870fail_poweron:
871 gk20a_pm_railgate(dev);
872fail:
873 return err;
874}
875
876static int gk20a_pm_runtime_suspend(struct device *dev)
877{
878 int err = 0;
879
880 if (gk20a_gpu_is_virtual(dev))
881 err = vgpu_pm_prepare_poweroff(dev);
882 else
883 err = gk20a_pm_prepare_poweroff(dev);
884 if (err)
885 goto fail;
886
887 err = gk20a_pm_railgate(dev);
888 if (err)
889 goto fail_railgate;
890
891 return 0;
892
893fail_railgate:
894 gk20a_pm_finalize_poweron(dev);
895fail:
896 pm_runtime_mark_last_busy(dev);
897 return err;
898}
899
900static int gk20a_pm_suspend(struct device *dev)
901{
902 struct gk20a_platform *platform = dev_get_drvdata(dev);
903 struct gk20a *g = get_gk20a(dev);
904 int ret = 0;
905 int idle_usage_count = g->user_railgate_disabled ? 1 : 0;
906
907 if (!g->power_on)
908 return 0;
909
910 if (nvgpu_atomic_read(&g->usage_count) > idle_usage_count)
911 return -EBUSY;
912
913 ret = gk20a_pm_runtime_suspend(dev);
914 if (ret)
915 return ret;
916
917 if (platform->suspend)
918 platform->suspend(dev);
919
920 g->suspended = true;
921
922 return 0;
923}
924
925static int gk20a_pm_resume(struct device *dev)
926{
927 struct gk20a *g = get_gk20a(dev);
928 int ret = 0;
929
930 if (!g->suspended)
931 return 0;
932
933 ret = gk20a_pm_runtime_resume(dev);
934
935 g->suspended = false;
936
937 return ret;
938}
939
940static const struct dev_pm_ops gk20a_pm_ops = {
941 .runtime_resume = gk20a_pm_runtime_resume,
942 .runtime_suspend = gk20a_pm_runtime_suspend,
943 .resume = gk20a_pm_resume,
944 .suspend = gk20a_pm_suspend,
945};
946#endif
947
948static int gk20a_pm_init(struct device *dev)
949{
950 struct gk20a *g = get_gk20a(dev);
951 int err = 0;
952
953 gk20a_dbg_fn("");
954
955 /* Initialise pm runtime */
956 if (g->railgate_delay) {
957 pm_runtime_set_autosuspend_delay(dev,
958 g->railgate_delay);
959 pm_runtime_use_autosuspend(dev);
960 }
961
962 if (g->can_railgate) {
963 pm_runtime_enable(dev);
964 if (!pm_runtime_enabled(dev))
965 gk20a_pm_unrailgate(dev);
966 else
967 gk20a_pm_railgate(dev);
968 } else {
969 __pm_runtime_disable(dev, false);
970 gk20a_pm_unrailgate(dev);
971 }
972
973 return err;
974}
975
976/*
977 * Start the process for unloading the driver. Set NVGPU_DRIVER_IS_DYING.
978 */
979void gk20a_driver_start_unload(struct gk20a *g)
980{
981 struct nvgpu_os_linux *l = nvgpu_os_linux_from_gk20a(g);
982
983 gk20a_dbg(gpu_dbg_shutdown, "Driver is now going down!\n");
984
985 down_write(&l->busy_lock);
986 __nvgpu_set_enabled(g, NVGPU_DRIVER_IS_DYING, true);
987 /* GR SW ready needs to be invalidated at this time with the busy lock
988 * held to prevent a racing condition on the gr/mm code */
989 g->gr.sw_ready = false;
990 up_write(&l->busy_lock);
991
992 if (g->is_virtual)
993 return;
994
995 gk20a_wait_for_idle(g);
996
997 nvgpu_wait_for_deferred_interrupts(g);
998 gk20a_channel_cancel_pending_sema_waits(g);
999
1000 if (l->nonstall_work_queue) {
1001 cancel_work_sync(&l->nonstall_fn_work);
1002 destroy_workqueue(l->nonstall_work_queue);
1003 l->nonstall_work_queue = NULL;
1004 }
1005}
1006
1007static inline void set_gk20a(struct platform_device *pdev, struct gk20a *gk20a)
1008{
1009 gk20a_get_platform(&pdev->dev)->g = gk20a;
1010}
1011
1012static int nvgpu_read_fuse_overrides(struct gk20a *g)
1013{
1014 struct device_node *np = dev_from_gk20a(g)->of_node;
1015 u32 *fuses;
1016 int count, i;
1017
1018 if (!np) /* may be pcie device */
1019 return 0;
1020
1021 count = of_property_count_elems_of_size(np, "fuse-overrides", 8);
1022 if (count <= 0)
1023 return count;
1024
1025 fuses = nvgpu_kmalloc(g, sizeof(u32) * count * 2);
1026 if (!fuses)
1027 return -ENOMEM;
1028 of_property_read_u32_array(np, "fuse-overrides", fuses, count * 2);
1029 for (i = 0; i < count; i++) {
1030 u32 fuse, value;
1031
1032 fuse = fuses[2 * i];
1033 value = fuses[2 * i + 1];
1034 switch (fuse) {
1035 case GM20B_FUSE_OPT_TPC_DISABLE:
1036 g->tpc_fs_mask_user = ~value;
1037 break;
1038#ifdef CONFIG_ARCH_TEGRA_18x_SOC
1039 case GP10B_FUSE_OPT_ECC_EN:
1040 g->gr.t18x.fecs_feature_override_ecc_val = value;
1041 break;
1042#endif
1043 default:
1044 nvgpu_err(g, "ignore unknown fuse override %08x", fuse);
1045 break;
1046 }
1047 }
1048
1049 nvgpu_kfree(g, fuses);
1050
1051 return 0;
1052}
1053
1054static int gk20a_probe(struct platform_device *dev)
1055{
1056 struct nvgpu_os_linux *l;
1057 struct gk20a *gk20a;
1058 int err;
1059 struct gk20a_platform *platform = NULL;
1060
1061 if (dev->dev.of_node) {
1062 const struct of_device_id *match;
1063
1064 match = of_match_device(tegra_gk20a_of_match, &dev->dev);
1065 if (match)
1066 platform = (struct gk20a_platform *)match->data;
1067 } else
1068 platform = (struct gk20a_platform *)dev->dev.platform_data;
1069
1070 if (!platform) {
1071 dev_err(&dev->dev, "no platform data\n");
1072 return -ENODATA;
1073 }
1074
1075 gk20a_dbg_fn("");
1076
1077 platform_set_drvdata(dev, platform);
1078
1079 if (gk20a_gpu_is_virtual(&dev->dev))
1080 return vgpu_probe(dev);
1081
1082 l = kzalloc(sizeof(*l), GFP_KERNEL);
1083 if (!l) {
1084 dev_err(&dev->dev, "couldn't allocate gk20a support");
1085 return -ENOMEM;
1086 }
1087
1088 gk20a = &l->g;
1089 nvgpu_init_gk20a(gk20a);
1090 set_gk20a(dev, gk20a);
1091 l->dev = &dev->dev;
1092 gk20a->log_mask = NVGPU_DEFAULT_DBG_MASK;
1093
1094 nvgpu_kmem_init(gk20a);
1095
1096 err = nvgpu_init_enabled_flags(gk20a);
1097 if (err)
1098 return err;
1099
1100 if (nvgpu_platform_is_simulation(gk20a))
1101 __nvgpu_set_enabled(gk20a, NVGPU_IS_FMODEL, true);
1102
1103 gk20a->irq_stall = platform_get_irq(dev, 0);
1104 gk20a->irq_nonstall = platform_get_irq(dev, 1);
1105 if (gk20a->irq_stall < 0 || gk20a->irq_nonstall < 0)
1106 return -ENXIO;
1107
1108 err = devm_request_threaded_irq(&dev->dev,
1109 gk20a->irq_stall,
1110 gk20a_intr_isr_stall,
1111 gk20a_intr_thread_stall,
1112 0, "gk20a_stall", gk20a);
1113 if (err) {
1114 dev_err(&dev->dev,
1115 "failed to request stall intr irq @ %d\n",
1116 gk20a->irq_stall);
1117 return err;
1118 }
1119 err = devm_request_irq(&dev->dev,
1120 gk20a->irq_nonstall,
1121 gk20a_intr_isr_nonstall,
1122 0, "gk20a_nonstall", gk20a);
1123 if (err) {
1124 dev_err(&dev->dev,
1125 "failed to request non-stall intr irq @ %d\n",
1126 gk20a->irq_nonstall);
1127 return err;
1128 }
1129 disable_irq(gk20a->irq_stall);
1130 if (gk20a->irq_stall != gk20a->irq_nonstall)
1131 disable_irq(gk20a->irq_nonstall);
1132
1133 err = gk20a_init_support(dev);
1134 if (err)
1135 return err;
1136
1137 err = nvgpu_read_fuse_overrides(gk20a);
1138
1139#ifdef CONFIG_RESET_CONTROLLER
1140 platform->reset_control = devm_reset_control_get(&dev->dev, NULL);
1141 if (IS_ERR(platform->reset_control))
1142 platform->reset_control = NULL;
1143#endif
1144
1145 err = nvgpu_probe(gk20a, "gpu.0", INTERFACE_NAME, &nvgpu_class);
1146 if (err)
1147 return err;
1148
1149 err = gk20a_pm_init(&dev->dev);
1150 if (err) {
1151 dev_err(&dev->dev, "pm init failed");
1152 return err;
1153 }
1154
1155 gk20a->mm.has_physical_mode = !nvgpu_is_hypervisor_mode(gk20a);
1156
1157 return 0;
1158}
1159
1160int nvgpu_remove(struct device *dev, struct class *class)
1161{
1162 struct gk20a *g = get_gk20a(dev);
1163 struct nvgpu_os_linux *l = nvgpu_os_linux_from_gk20a(g);
1164 struct gk20a_platform *platform = gk20a_get_platform(dev);
1165 int err;
1166
1167 gk20a_dbg_fn("");
1168
1169 err = nvgpu_quiesce(g);
1170 WARN(err, "gpu failed to idle during driver removal");
1171
1172 if (nvgpu_mem_is_valid(&g->syncpt_mem))
1173 nvgpu_dma_free(g, &g->syncpt_mem);
1174
1175 if (platform->has_cde)
1176 gk20a_cde_destroy(l);
1177
1178#ifdef CONFIG_GK20A_CTXSW_TRACE
1179 gk20a_ctxsw_trace_cleanup(g);
1180#endif
1181
1182 gk20a_sched_ctrl_cleanup(g);
1183
1184 if (IS_ENABLED(CONFIG_GK20A_DEVFREQ))
1185 gk20a_scale_exit(dev);
1186
1187#ifdef CONFIG_ARCH_TEGRA_18x_SOC
1188 nvgpu_clk_arb_cleanup_arbiter(g);
1189#endif
1190
1191 gk20a_user_deinit(dev, class);
1192
1193 gk20a_debug_deinit(g);
1194
1195 nvgpu_remove_sysfs(dev);
1196
1197 if (platform->secure_buffer.destroy)
1198 platform->secure_buffer.destroy(g,
1199 &platform->secure_buffer);
1200
1201 if (pm_runtime_enabled(dev))
1202 pm_runtime_disable(dev);
1203
1204 if (platform->remove)
1205 platform->remove(dev);
1206
1207 gk20a_dbg_fn("removed");
1208
1209 return err;
1210}
1211
1212static int __exit gk20a_remove(struct platform_device *pdev)
1213{
1214 int err;
1215 struct device *dev = &pdev->dev;
1216 struct gk20a *g = get_gk20a(dev);
1217
1218 if (gk20a_gpu_is_virtual(dev))
1219 return vgpu_remove(pdev);
1220
1221 err = nvgpu_remove(dev, &nvgpu_class);
1222
1223 set_gk20a(pdev, NULL);
1224 gk20a_put(g);
1225
1226 return err;
1227}
1228
1229static struct platform_driver gk20a_driver = {
1230 .probe = gk20a_probe,
1231 .remove = __exit_p(gk20a_remove),
1232 .shutdown = gk20a_pm_shutdown,
1233 .driver = {
1234 .owner = THIS_MODULE,
1235 .name = "gk20a",
1236 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1237#ifdef CONFIG_OF
1238 .of_match_table = tegra_gk20a_of_match,
1239#endif
1240#ifdef CONFIG_PM
1241 .pm = &gk20a_pm_ops,
1242#endif
1243 .suppress_bind_attrs = true,
1244 }
1245};
1246
1247struct class nvgpu_class = {
1248 .owner = THIS_MODULE,
1249 .name = CLASS_NAME,
1250};
1251
1252static int __init gk20a_init(void)
1253{
1254
1255 int ret;
1256
1257 ret = class_register(&nvgpu_class);
1258 if (ret)
1259 return ret;
1260
1261 ret = nvgpu_pci_init();
1262 if (ret)
1263 return ret;
1264
1265 return platform_driver_register(&gk20a_driver);
1266}
1267
1268static void __exit gk20a_exit(void)
1269{
1270 nvgpu_pci_exit();
1271 platform_driver_unregister(&gk20a_driver);
1272 class_unregister(&nvgpu_class);
1273}
1274
1275MODULE_LICENSE("GPL v2");
1276module_init(gk20a_init);
1277module_exit(gk20a_exit);