1/*
2 * Copyright (c) 2014-2015 ARM Limited
3 * All rights reserved
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * Authors: Andreas Sandberg
18 */
19
20#include <libnomali/nomali.h>
21#include <inttypes.h>
22
23#include "nomali_test_helpers.h"
24#include "../lib/mali_midg_regmap.h"
25
26static void
27on_int(nomali_handle_t h, void *usr, nomali_int_t intno, int set)
28{
29    test_diag("on_int: intno: %i, set: %i", intno, set);
30    *(int*)usr = !!set;
31}
32
33static void
34test_gpu_int(nomali_handle_t h)
35{
36    int int_triggered = 0;
37    nomali_callback_t int_callback = {
38        .type = NOMALI_CALLBACK_INT,
39        .usr = &int_triggered,
40        .func.interrupt = on_int,
41    };
42
43    nomali_callback_t int_null_callback = {
44        .type = NOMALI_CALLBACK_INT,
45        .usr = NULL,
46        .func.interrupt = NULL,
47    };
48
49    /*
50     * Raise an interrupt without callbacks
51     */
52    E_NOMALI_BAIL(nomali_reg_write(h,
53                                   GPU_CONTROL_REG(GPU_IRQ_CLEAR),
54                                   GPU_IRQ_REG_ALL));
55
56    E_NOMALI_BAIL(nomali_reg_write(h, GPU_CONTROL_REG(GPU_IRQ_MASK),
57                                   GPU_FAULT));
58
59    E_NOMALI_BAIL(nomali_reg_write(h, GPU_CONTROL_REG(GPU_IRQ_RAWSTAT),
60                                   GPU_FAULT));
61
62    E_NOMALI_BAIL(nomali_reg_write(h,
63                                   GPU_CONTROL_REG(GPU_IRQ_CLEAR),
64                                   GPU_IRQ_REG_ALL));
65
66    /*
67     * Register callbacks and raise interrupt again.
68     */
69    E_NOMALI_BAIL(nomali_set_callback(h, &int_callback));
70    if (int_triggered != 0) {
71        test_diag("Got spurious interrupt\n");
72        test_fail("gpu_int");
73    }
74
75    E_NOMALI_BAIL(nomali_reg_write(h, GPU_CONTROL_REG(GPU_IRQ_RAWSTAT),
76                                   GPU_FAULT));
77    if (int_triggered == 1) {
78        test_ok("gpu_int");
79    } else {
80        test_fail("gpu_int");
81    }
82    int_triggered = 0;
83
84
85    /*
86     * Register mask interrupts and raise interrupt again.
87     */
88    E_NOMALI_BAIL(nomali_reg_write(h,
89                                   GPU_CONTROL_REG(GPU_IRQ_CLEAR),
90                                   GPU_IRQ_REG_ALL));
91    E_NOMALI_BAIL(nomali_reg_write(h, GPU_CONTROL_REG(GPU_IRQ_MASK),
92                                   0));
93    E_NOMALI_BAIL(nomali_reg_write(h, GPU_CONTROL_REG(GPU_IRQ_RAWSTAT),
94                                   GPU_FAULT));
95    if (int_triggered == 0) {
96        test_ok("gpu_int_masked");
97    } else {
98        test_fail("gpu_int_maked");
99    }
100    E_NOMALI_BAIL(nomali_reg_write(h,
101                                   GPU_CONTROL_REG(GPU_IRQ_CLEAR),
102                                   GPU_IRQ_REG_ALL));
103    E_NOMALI_BAIL(nomali_set_callback(h, &int_null_callback));
104}
105
106int
107main(int argc, char **argv)
108{
109    const nomali_config_t cfg = {
110        .type = NOMALI_GPU_T60X,
111        .ver_maj = 0,
112        .ver_min = 1,
113        .ver_status = 0,
114    };
115
116    nomali_handle_t h;
117
118    E_NOMALI_BAIL(nomali_create(&h, &cfg));
119
120    test_gpu_int(h);
121
122    E_NOMALI_BAIL(nomali_destroy(h));
123
124    return 0;
125}
126