GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / gpu / drm / nouveau / nvkm / subdev / secboot / gp102.c
1 /*
2  * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22
23 #include "acr.h"
24 #include "gm200.h"
25
26 #include "ls_ucode.h"
27 #include "hs_ucode.h"
28 #include <subdev/mc.h>
29 #include <subdev/timer.h>
30 #include <engine/falcon.h>
31 #include <engine/nvdec.h>
32
33 static bool
34 gp102_secboot_scrub_required(struct nvkm_secboot *sb)
35 {
36         struct nvkm_subdev *subdev = &sb->subdev;
37         struct nvkm_device *device = subdev->device;
38         u32 reg;
39
40         nvkm_wr32(device, 0x100cd0, 0x2);
41         reg = nvkm_rd32(device, 0x100cd0);
42
43         return (reg & BIT(4));
44 }
45
46 static int
47 gp102_run_secure_scrub(struct nvkm_secboot *sb)
48 {
49         struct nvkm_subdev *subdev = &sb->subdev;
50         struct nvkm_device *device = subdev->device;
51         struct nvkm_engine *engine;
52         struct nvkm_falcon *falcon;
53         void *scrub_image;
54         struct fw_bin_header *hsbin_hdr;
55         struct hsf_fw_header *fw_hdr;
56         struct hsf_load_header *lhdr;
57         void *scrub_data;
58         int ret;
59
60         nvkm_debug(subdev, "running VPR scrubber binary on NVDEC...\n");
61
62         engine = nvkm_engine_ref(&device->nvdec->engine);
63         if (IS_ERR(engine))
64                 return PTR_ERR(engine);
65         falcon = device->nvdec->falcon;
66
67         nvkm_falcon_get(falcon, &sb->subdev);
68
69         scrub_image = hs_ucode_load_blob(subdev, falcon, "nvdec/scrubber");
70         if (IS_ERR(scrub_image))
71                 return PTR_ERR(scrub_image);
72
73         nvkm_falcon_reset(falcon);
74         nvkm_falcon_bind_context(falcon, NULL);
75
76         hsbin_hdr = scrub_image;
77         fw_hdr = scrub_image + hsbin_hdr->header_offset;
78         lhdr = scrub_image + fw_hdr->hdr_offset;
79         scrub_data = scrub_image + hsbin_hdr->data_offset;
80
81         nvkm_falcon_load_imem(falcon, scrub_data, lhdr->non_sec_code_off,
82                               lhdr->non_sec_code_size,
83                               lhdr->non_sec_code_off >> 8, 0, false);
84         nvkm_falcon_load_imem(falcon, scrub_data + lhdr->apps[0],
85                               ALIGN(lhdr->apps[0], 0x100),
86                               lhdr->apps[1],
87                               lhdr->apps[0] >> 8, 0, true);
88         nvkm_falcon_load_dmem(falcon, scrub_data + lhdr->data_dma_base, 0,
89                               lhdr->data_size, 0);
90
91         kfree(scrub_image);
92
93         nvkm_falcon_set_start_addr(falcon, 0x0);
94         nvkm_falcon_start(falcon);
95
96         ret = nvkm_falcon_wait_for_halt(falcon, 500);
97         if (ret < 0) {
98                 nvkm_error(subdev, "failed to run VPR scrubber binary!\n");
99                 ret = -ETIMEDOUT;
100                 goto end;
101         }
102
103         /* put nvdec in clean state - without reset it will remain in HS mode */
104         nvkm_falcon_reset(falcon);
105
106         if (gp102_secboot_scrub_required(sb)) {
107                 nvkm_error(subdev, "VPR scrubber binary failed!\n");
108                 ret = -EINVAL;
109                 goto end;
110         }
111
112         nvkm_debug(subdev, "VPR scrub successfully completed\n");
113
114 end:
115         nvkm_falcon_put(falcon, &sb->subdev);
116         nvkm_engine_unref(&engine);
117         return ret;
118 }
119
120 static int
121 gp102_secboot_run_blob(struct nvkm_secboot *sb, struct nvkm_gpuobj *blob,
122                        struct nvkm_falcon *falcon)
123 {
124         int ret;
125
126         /* make sure the VPR region is unlocked */
127         if (gp102_secboot_scrub_required(sb)) {
128                 ret = gp102_run_secure_scrub(sb);
129                 if (ret)
130                         return ret;
131         }
132
133         return gm200_secboot_run_blob(sb, blob, falcon);
134 }
135
136 const struct nvkm_secboot_func
137 gp102_secboot = {
138         .dtor = gm200_secboot_dtor,
139         .oneinit = gm200_secboot_oneinit,
140         .fini = gm200_secboot_fini,
141         .run_blob = gp102_secboot_run_blob,
142 };
143
144 int
145 gp102_secboot_new(struct nvkm_device *device, int index,
146                   struct nvkm_secboot **psb)
147 {
148         int ret;
149         struct gm200_secboot *gsb;
150         struct nvkm_acr *acr;
151
152         acr = acr_r367_new(NVKM_SECBOOT_FALCON_SEC2,
153                            BIT(NVKM_SECBOOT_FALCON_FECS) |
154                            BIT(NVKM_SECBOOT_FALCON_GPCCS) |
155                            BIT(NVKM_SECBOOT_FALCON_SEC2));
156         if (IS_ERR(acr))
157                 return PTR_ERR(acr);
158
159         gsb = kzalloc(sizeof(*gsb), GFP_KERNEL);
160         if (!gsb) {
161                 psb = NULL;
162                 return -ENOMEM;
163         }
164         *psb = &gsb->base;
165
166         ret = nvkm_secboot_ctor(&gp102_secboot, acr, device, index, &gsb->base);
167         if (ret)
168                 return ret;
169
170         return 0;
171 }
172
173 /*(DEBLOBBED)*/